get "ReferenceError: playwright is not defined" when using playwright.config.js - playwright

My very first day trying Playwright and I am having trouble using config. My steps:
init project and install
npm init
npm i -D #playwright/test --save
npx playwright install
create tests/foo.spec.js
const { test, expect } = require('#playwright/test');
test('basic test', async ({ page }) => {
await page.goto('https://playwright.dev/');
const title = page.locator('.navbar__inner .navbar__title');
await expect(title).toHaveText('Playwright');
});
run it
$ npx playwright test
✓ tests/foo.spec.js:3:1 › basic test (3s)
Create playwright.config.js
// #ts-check
const { devices } = require('#playwright/test');
/** #type {import('#playwright/test').PlaywrightTestConfig} */
const config = {
use: {
headless: false,
viewport: { width: 1280, height: 720 },
ignoreHTTPSErrors: true,
video: 'on-first-retry',
},
};
module.exports = config;
run again and get error
$ npx playwright test
Using config at /Users/me/git/left/playwright/playwright.config.js
ReferenceError: playwright is not defined
at Object.<anonymous> (/Users/me/git/left/playwright/playwright.config.js:1:1)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
...
(/Users/me/git/left/playwright/node_modules/#playwright/test/lib/test/cli.js:80:7)
Thank you for helping. My env.
playwright#1.0.0 /Users/me/git/left/playwright
└── #playwright/test#1.15.0
$ npm -v
7.18.1
$ node -v
v16.4.2

Related

Using <style lang="scss"> in vue component gives error

I am trying to use vue js in rails.
Everything works, except when I tried to use <style> inside .vue component
The exact error is:
./app/javascript/layouts/dashboard.vue?vue&type=style&index=0&lang=scss& (./node_modules/css-loader/dist/cjs.js!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/sass-loader/dist/cjs.js??ref--1-2!./node_modules/style-loader/dist!./node_modules/css-loader/dist/cjs.js??ref--5-1!./node_modules/postcss-loader/src??ref--5-2!./node_modules/sass-loader/dist/cjs.js??ref--5-3!./node_modules/vue-loader/lib??vue-loader-options!./app/javascript/layouts/dashboard.vue?vue&type=style&index=0&lang=scss&)
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Expected newline.
My environment.js file
const { environment } = require('#rails/webpacker')
const { VueLoaderPlugin } = require('vue-loader')
const vueLoader = require('./loaders/vueLoader')
const vuetifyLoader = require('./loaders/vuetifyLoader')
environment.plugins.prepend('VueLoaderPlugin', new VueLoaderPlugin())
environment.loaders.prepend('vue', vueLoader)
environment.loaders.prepend('vuetify', vuetifyLoader)
const resolver = {
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
}
}
environment.config.merge(resolver)
module.exports = environment
VuetifyLoader.js file
module.exports = {
test: /\.s(c|a)ss$/,
use: [
'vue-style-loader',
'css-loader',
{
loader: 'sass-loader',
// Requires sass-loader#^7.0.0
options: {
implementation: require('sass'),
fiber: require('fibers'),
indentedSyntax: true // optional
},
// Requires sass-loader#^8.0.0
options: {
implementation: require('sass'),
sassOptions: {
fiber: require('fibers'),
indentedSyntax: true // optional
},
},
},
],
}
install these two plugins.
npm install --save node-sass
npm install --save sass-loader
So, the problem was with fiber and indentedSyntax. After removing those two, everything works as expected. I was getting lots of error related to scss like
like
expected new line
in sass files inside node_modules. I don't know, why vuetify recommends to use fiber in sass loader.

is anyone integrated videojs-record with angular 7?

I am trying to record video with audio using videojs-record and my application is in angular 7. I have followed their wiki. Here is the link below
https://github.com/collab-project/videojs-record/wiki/Angular
but this does not work for me.
here is the error what I am getting
ERROR in ./node_modules/videojs-record/dist/videojs.record.js
Module not found: Error: Can't resolve 'RecordRTC' in '/path/to/project/root/node_modules/videojs-record/dist'
ERROR in ./node_modules/videojs-record/dist/videojs.record.js
Module not found: Error: Can't resolve 'videojs' in '/path/to/project/root/node_modules/videojs-record/dist'
Here is my code and my configuration for videojs in video-recorder.component.ts
import { Component, OnInit, OnDestroy, ElementRef, Input } from '#angular/core';
import videojs from 'video.js';
import * as adapter from 'webrtc-adapter/out/adapter_no_global.js';
import * as RecordRTC from 'recordrtc';
// register videojs-record plugin with this import
import * as Record from 'videojs-record/dist/videojs.record.js';
#Component({
selector: 'app-video-recorder',
templateUrl: './video-recorder.component.html',
styleUrls: ['./video-recorder.component.scss']
})
export class VideoRecorderComponent implements OnInit, OnDestroy {
// reference to the element itself: used to access events and methods
private _elementRef: ElementRef;
// index to create unique ID for component
#Input() idx: string;
private config: any;
private player: any;
private plugin: any;
// constructor initializes our declared vars
constructor(elementRef: ElementRef) {
this.player = false;
// save reference to plugin (so it initializes)
this.plugin = Record;
// video.js configuration
this.config = {
controls: true,
autoplay: false,
fluid: false,
loop: false,
width: 320,
height: 240,
controlBar: {
volumePanel: false
},
plugins: {
// configure videojs-record plugin
record: {
audio: false,
video: true,
debug: true
}
}
};
}
ngOnInit() {}
// use ngAfterViewInit to make sure we initialize the videojs element
// after the component template itself has been rendered
ngAfterViewInit() {
// ID with which to access the template's video element
let el = 'video_' + this.idx;
// setup the player via the unique element ID
this.player = videojs(document.getElementById(el), this.config, () => {
console.log('player ready! id:', el);
// print version information at startup
var msg = 'Using video.js ' + videojs.VERSION +
' with videojs-record ' + videojs.getPluginVersion('record') +
' and recordrtc ' + RecordRTC.version;
videojs.log(msg);
});
// device is ready
this.player.on('deviceReady', () => {
console.log('device is ready!');
});
// user clicked the record button and started recording
this.player.on('startRecord', () => {
console.log('started recording!');
});
// user completed recording and stream is available
this.player.on('finishRecord', () => {
// recordedData is a blob object containing the recorded data that
// can be downloaded by the user, stored on server etc.
console.log('finished recording: ', this.player.recordedData);
});
// error handling
this.player.on('error', (element, error) => {
console.warn(error);
});
this.player.on('deviceError', () => {
console.error('device error:', this.player.deviceErrorCode);
});
}
// use ngOnDestroy to detach event handlers and remove the player
ngOnDestroy() {
if (this.player) {
this.player.dispose();
this.player = false;
}
}
}
and here is my video-recorder.component.html
<video id="video_{{idx}}" class="video-js vjs-default-skin" playsinline></video>
Below information may help to figure it out the issue.
Angular CLI: 7.2.3
Node: 10.15.1
OS: linux x64
Angular: 7.2.2
... common, compiler, core, forms, language-service
... platform-browser, platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
#angular-devkit/architect 0.12.3
#angular-devkit/build-angular 0.12.3
#angular-devkit/build-optimizer 0.12.3
#angular-devkit/build-webpack 0.12.3
#angular-devkit/core 7.2.3
#angular-devkit/schematics 7.2.3
#angular/animations 7.2.7
#angular/cdk 7.3.0
#angular/cli 7.2.3
#angular/compiler-cli 7.2.7
#ngtools/webpack 7.2.3
#schematics/angular 7.2.3
#schematics/update 0.12.3
rxjs 6.3.3
typescript 3.2.4
I am new to angular. So any help on this will be appreciated. Thanks in advance.
No worries guys, I have fixed it by myself. After doing some research I came to know that as I was using angular cli to serve and build so I have used ngx-build-plus (as ng eject is deprecated in angular 7 and will be removed from angular 8) to execute webpack config using angular cli. This webpack config was missing before. This may help someone that's why just shared. Thank you.
you can't use in that way. if you are using angular cli to serve or build then you have to create a partial webpack config file and serve or build it through angular cli. You should follow below things.
Please visit below link and install the package and follow the instruction to configure your stuffs.
https://www.npmjs.com/package/ngx-build-plus
Your webpack.partial.js should look like
const webpack = require('webpack');
module.exports = {
resolve: {
alias: {
// place your config
}
},
plugins: [
// place your config
],
}
and scripts in package.json file should look like
"scripts": {
"ng": "ng",
"start": "ng serve --extra-webpack-config webpack.partial.js",
"build": "ng build --extra-webpack-config webpack.partial.js",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"build:prod": "ng build --prod --extra-webpack-config webpack.partial.js",
"build:stage": "ng build --prod -c staging --extra-webpack-config webpack.partial.js",
"build:dev": "ng build -c development --extra-webpack-config webpack.partial.js"
},
Then you can serve your app using npm start
To build you use npm run build:dev || npm run build:stage || npm run build:prod
based on the environment.

Rollup with gulp and typescript outputs Cannot find module 'tslib/tslib.es6.js'

I followed the typescript gulp in the doc :https://rollupjs.org/guide/en#gulp
here is my gulp file :
const gulp = require("gulp");
const rollup = require("rollup");
const rollupTypescript = require("rollup-plugin-typescript");
gulp.task("build", () => {
return rollup
.rollup({
input: "./main.ts",
plugins: [rollupTypescript()]
})
.then(bundle => {
return bundle.write({
file: "./main.js",
format: "umd",
name: "library",
sourcemap: true
});
});
});
Error: Cannot find module 'tslib/tslib.es6.js' from 'C:\projets\Tests\rollup\node_modules\rollup-plugin-typescript\dist'
at Function.module.exports [as sync] (C:\projets\Tests\rollup\node_modules\resolve\lib\sync.js:58:15)
at typescript (C:\projets\Tests\rollup\node_modules\rollup-plugin-typescript\dist\rollup-plugin-typescript.cjs.js:109:29)
at gulp.task (C:\projets\Tests\rollup\gulpfile.js:9:17)
at taskWrapper (C:\projets\Tests\rollup\node_modules\undertaker\lib\set-task.js:13:15)
at bound (domain.js:301:14)
at runBound (domain.js:314:12)
at asyncRunner (C:\projets\Tests\rollup\node_modules\async-done\index.js:55:18)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Anyone knows why this is happening is this a bug with rollup or am I doing something wrong?
You need tslib, which is a peer dependency of rollup-plugin-typescript.
npm i -D tslib

Call yeoman generator from code with options

I created a yeoman generator with user interaction, that can be called in the terminal like (after running npm link):
yo mygenerator --name test --path /test/path --project testproject
Now I want to include this generator in my vscode extension.
How can I call the yo generator from my typescript code when the generator when the generator is added as a package.json dependency?
So something like (pseudo code)
import { yo } from 'yeoman';
import mygenerator; // added as a dependency via package.json
const options = {
name: 'test',
path: '/test/path',
project: 'testproject',
};
yo.exec(mygenerator, options, () => {
console.log('yeoman finished')
});
Is something like this possible?
Here is a solution for that:
const env = yeoman.createEnv();
const generatorPath = '../node_modules/generator-name/generators/app/index.js';
env.getByPath(generatorDir);
env.on('error', (err: any) => {
// handle error
});
const options = {
env,
'option1': option1,
'option2': option2,
};
try {
await env.run('name', options);
} catch (err) {
// handle error
}

Jest and Bower Module loading in jest tests

Lets say I have a project that uses bower, grunt, bowerify(with shim) and since I love Jest so much I want to test with that. How in the world do I get jest to see my browserify shim modules when it runs tests. I use grunt, to kick off the npm test command.
Here is my package.json file.
"browser": {
"jquery": "./bower_components/jquery/dist/jquery.js",
"foundation": "./bower_components/foundation/js/foundation/foundation.js",
"fastclick": "./bower_components/fastclick/lib/fastclick.js",
"greensock-tm": "./bower_components/gsap/src/uncompressed/TweenMax.js",
"greensock-css": "./bower_components/gsap/src/uncompressed/plugins/CSSPlugin.js",
"greensock-time": "./bower_components/gsap/src/uncompressed/TimelineMax.js",
"scrollmagic": "./bower_components/ScrollMagic/js/jquery.scrollmagic.js",
"handlebars": "./bower_components/handlebars/handlebars.runtime.js"
},
"browserify-shim": {
"jquery": "$",
"greensock-css": "CSSPlugin",
"fastclick": "FastClick",
"greensock-tm": "TweenMax",
"greensock-time": "TimelineMax",
"scrollmagic": "ScrollMagic",
"foundation": "foundation",
"handlebars": "Handlebars"
},
"browserify": {
"transform": [
"browserify-shim"
]
},
Right now I almost have this worked out by doing this in my grunt file before I run the test.
grunt.registerTask("shimBowerForTests",function(){
var readJson = require('read-package-json');
var fs = require('fs');
var remapify = require('remapify');
readJson('./package.json', console.error, false, function (er, data) {
if (er) {
throw "There was an error reading the file";
}
var packages = data.browser;
var browserify = require('browserify');
for (var key in packages){
var b = browserify();
var wstream = fs.createWriteStream("devjs/test/modules/"+key+'.js');
b.add(packages[key]);
b.bundle().pipe(wstream);
}
});
});
and.
exec: {
jestTest: {
command: 'cp -r devjs/modules devjs/test/modules && npm test'
}
}
The problem is that using browserify so combine everything for the browser works great with my setup and I can require my shimmed modules like this.
require('jquery') //example but in the jest cli the test fail because they can find the module unless I somehow prefix it with ./, like so require('./jquery')
I'm guessing that the problem is that you've only installed your shimmed modules with bower. If you want them to work in node/jest, you'll have to install them with npm as well. Then just make sure Jest isn't mocking anything in the node_modules directory, and it should find all the required modules in there as long as the names match up.
Your Jest config in package.json should look like:
"jest": {
"unmockedModulePathPatterns": [
"./node_modules"
]
}
And then just download all the dependencies.
npm install jquery --save-dev
UPDATE
Instead of using my below solution you should opt for using Karma,karma browserify. I have converted the below solution into using karma and it is working much much better.
----------------------OLD ANSWER
What I actually did to solve this was, used the Jest source preprocessor to rewrite the require statement to look for a module in a certain directory in my /tests/ folder that I have created using grunt. The Folder contains the files listed in my browserify-shim, browser section of the package.json file.
EDIT: Here is how I shim bower, I made this script in the Gruntfile.js that puts all the bower modules and any commonjs modules that I need into an accessible directory.
grunt.registerTask("shimBowerForTests", function() {
var readJson = require('read-package-json');
var fs = require('fs');
readJson('./package.json', console.error, false, function(er, data) {
if (er) {
throw "There was an error reading the file";
}
var packages = data.browser;
var shim = data['browserify-shim'];
var browserify = require('browserify');
var exclude = ["jquery.maskedinput", "jquery"];
for (var key in packages) {
var b = browserify();
var wstream = fs.createWriteStream("devjs/test/modules/" + key + '.js');
if (shim[key] !== undefined && exclude.indexOf(key) === -1) {
b.add(packages[key]);
b.bundle().pipe(wstream);
} else {
var rstream = fs.createReadStream(packages[key]);
rstream.pipe(wstream);
}
}
});
});
Then in the Jest pre processor file I do this.
module.exports = {
process: function(src, path) {
var src2= src.replace(/require\([\"\']([^\.\'\"]+)[\"\']\)/g, "require(\'../modules/$1\')");
src2= src2.replace(/jest\.dontMock\([\"\']([^\.\'\"]+)[\"\']\)/g, "jest.dontMock(\'../modules/$1\')");
return src2;
}
};

Resources