Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cov support #87

Merged
merged 3 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ jobs:
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- name: Install required packages
run: |
sudo apt-get update
sudo apt-get install libgbm-dev libwoff1 libopus0 libwebp6 libwebpdemux2 libenchant1c2a libgudev-1.0-0 libsecret-1-0 libhyphen0 libgdk-pixbuf2.0-0 libegl1 libgles2 libevent-2.1-6 libnotify4 libxslt1.1
sudo apt-get install xvfb
- uses: microsoft/playwright-github-action@v1
- run: yarn
- run: yarn lint
- run: xvfb-run -a yarn test
Expand Down
17 changes: 13 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ Usage
--assets Assets to be served by the http server. [Default: process.cwd()]
--cwd Current directory. [Default: process.cwd()]
--extensions File extensions allowed in the bundle. [Default: js,cjs,mjs]
--cov Enable code coverage in instanbul format. Outputs '.nyc_output/out.json'.
Examples
$ playwright-test test.js --runner tape
$ playwright-test test/**/*.spec.js --debug
$ playwright-test test/**/*.spec.js --browser webkit -mode worker --incognito --debug

$ playwright-text benchmark.js --runner benchmark
$ playwright-test benchmark.js --runner benchmark
# Use benchmark.js to run your benchmark see playwright-test/mocks/benchmark.js for an example.

$ playwright-test test --cov && npx nyc report --reporter=html
# Enable code coverage in istanbul format which can be used by nyc.

$ playwright-test test/**/*.spec.js --debug --before ./mocks/before.js
# Run before.js in a separate tab check ./mocks/before.js for an example. Important: You need to call \`self.pwTestController.beforeEnd()\`, if you want the main tab to wait for the before script.

Expand Down Expand Up @@ -102,6 +106,10 @@ Usage
node: {
type: 'boolean',
default: true
},
cov: {
type: 'boolean',
default: false
}
}
});
Expand Down Expand Up @@ -132,7 +140,8 @@ const runnerOptions = () => {
'extensions',
'assets',
'before',
'node'
'node',
'cov'
];

if (!localFlags.includes(key)) {
Expand All @@ -153,7 +162,6 @@ let Runner = null;
if (cli.flags.runner === 'zora') {
Runner = ZoraRunner;
}

if (cli.flags.runner === 'benchmark') {
Runner = BenchmarkRunner;
}
Expand All @@ -174,7 +182,8 @@ const runner = new Runner({
extension: cli.flags.extension,
runnerOptions: runnerOptions(),
before: cli.flags.before,
node: cli.flags.node
node: cli.flags.node,
cov: cli.flags.cov
});

if (cli.flags.watch) {
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
"tape",
"benchmark",
"benchmark.js",
"zora"
"zora",
"coverage",
"istanbul",
"nyc",
"code coverage"
],
"dependencies": {
"camelcase": "^6.2.0",
Expand All @@ -45,11 +49,13 @@
"meow": "^8.0.0",
"merge-options": "^3.0.3",
"ora": "^5.1.0",
"p-each-series": "^2.1.0",
"playwright-core": "1.5.2",
"polka": "^0.5.2",
"resolve-cwd": "^3.0.0",
"sirv": "^1.0.7",
"tempy": "^1.0.0",
"v8-to-istanbul": "^7.0.0",
"webpack": "^4.42.1",
"webpack-merge": "^4.2.2"
},
Expand Down
76 changes: 40 additions & 36 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,46 @@ $ npm install playwright-test
## Usage

```console
$ playwright-test [input]

Options
--runner Test runner. Options: mocha, tape, benchmark and zora. [Default: mocha]
--watch, -w Watch files for changes and re-run tests.
--browser, -b Browser to run tests. Options: chromium, firefox, webkit. [Default: chromium]
--debug, -d Debug mode, keeps browser window open.
--mode, -m Run mode. Options: main, worker. [Default: main]
--incognito Use incognito window to run tests.
--extension Use extension background_page to run tests.
--before Full path to a script to be executed before, on a separate tab.
--assets Assets to be served by the http server. [Default: process.cwd()]
--cwd Current directory. [Default: process.cwd()]
--extensions File extensions allowed in the bundle. [Default: js,cjs,mjs]

Examples
$ playwright-test test.js --runner tape
$ playwright-test test/**/*.spec.js --debug
$ playwright-test test/**/*.spec.js --browser webkit --mode worker --incognito --debug

$ playwright-text benchmark.js --runner benchmark
# Use benchmark.js to run your benchmark see playwright-test/mocks/benchmark.js for an example.

$ playwright-test test/**/*.spec.js --debug --before ./mocks/before.js
# Run before.js in a separate tab check ./mocks/before.js for an example. Important: You need to call \`self.pwTestController.beforeEnd()\`, if you want the main tab to wait for the before script.

Extra arguments
All arguments passed to the cli not listed above will be fowarded to the runner.
To send a `false` flag use --no-bail.
$ playwright-test test.js --runner mocha --bail --grep 'should fail'

Check https://mochajs.org/api/mocha for `mocha` options or `npx mocha --help`.

Notes
DEBUG environmental variable is properly redirected to the browser. If you use 'debug' package for logs the following example will work as you expect.
$ DEBUG:app playwright-test test.js
Usage
$ playwright-test [input]

Options
--runner Test runner. Options: mocha, tape, benchmark and zora. [Default: mocha]
--watch, -w Watch files for changes and re-run tests.
--browser, -b Browser to run tests. Options: chromium, firefox, webkit. [Default: chromium]
--debug, -d Debug mode, keeps browser window open.
--mode, -m Run mode. Options: main, worker. [Default: main]
--incognito Use incognito window to run tests.
--extension Use extension background_page to run tests.
--before Full path to a script to be loaded on a separate tab.
--assets Assets to be served by the http server. [Default: process.cwd()]
--cwd Current directory. [Default: process.cwd()]
--extensions File extensions allowed in the bundle. [Default: js,cjs,mjs]
--cov Enable code coverage in instanbul format. Outputs '.nyc_output/out.json'.
Examples
$ playwright-test test.js --runner tape
$ playwright-test test/**/*.spec.js --debug
$ playwright-test test/**/*.spec.js --browser webkit -mode worker --incognito --debug

$ playwright-test benchmark.js --runner benchmark
# Use benchmark.js to run your benchmark see playwright-test/mocks/benchmark.js for an example.

$ playwright-test test --cov && npx nyc report --reporter=html
# Enable code coverage in istanbul format which can be used by nyc.

$ playwright-test test/**/*.spec.js --debug --before ./mocks/before.js
# Run before.js in a separate tab check ./mocks/before.js for an example. Important: You need to call \`self.pwTestController.beforeEnd()\`, if you want the main tab to wait for the before script.

Extra arguments
All arguments passed to the cli not listed above will be fowarded to the runner.
To send a \`false\` flag use --no-bail.
$ playwright-test test.js --runner mocha --bail --grep 'should fail'

Check https://mochajs.org/api/mocha for \`mocha\` options or \`npx mocha --help\`.

Notes
DEBUG environmental variable is properly redirected to the browser. If you use 'debug' package for logging the following example will work as you expect.
$ DEBUG:app playwright-test test.js
```


Expand Down
40 changes: 40 additions & 0 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const fs = require('fs');
const { promisify } = require('util');
const path = require('path');
const webpack = require('webpack');
const getPort = require('get-port');
Expand All @@ -10,7 +11,13 @@ const kleur = require('kleur');
const tempy = require('tempy');
const polka = require('polka');
const sirv = require('sirv');
const V8ToIstanbul = require('v8-to-istanbul');
const merge = require('merge-options');
const pEachSeries = require('p-each-series');

const writeFile = promisify(fs.writeFile);
const mkdir = promisify(fs.mkdir);

// const envPaths = require('env-paths')('playwright-test');
const { redirectConsole, getPw, compile } = require('./utils');

Expand All @@ -20,6 +27,7 @@ const defaultOptions = {
incognito: false,
extension: false,
debug: false,
cov: false,
files: [],
runnerOptions: {},
webpackConfig: {}
Expand Down Expand Up @@ -124,6 +132,10 @@ class Runner {
await this.page.goto(this.url);
}

if (this.options.cov) {
await this.page.coverage.startJSCoverage();
}

this.page.on('error', (err) => {
console.error('\n', kleur.red(err));
this.stop(true);
Expand Down Expand Up @@ -294,6 +306,33 @@ class Runner {
return;
}
this.stopped = true;
if (this.options.cov) {
const coverage = await this.page.coverage.stopJSCoverage();
const entries = {};

await pEachSeries(coverage, async (entry) => {
const filePath = path.normalize(entry.url).replace('file:', '');

// remove test files
if (this.options.files.includes(filePath)) {
return;
}

// remove random stuff
if (!fs.existsSync(filePath) || entry.url.includes('node_modules') || !entry.url.includes(process.cwd())) {
return;
}
const converter = new V8ToIstanbul(filePath, 0, { source: entry.source });

await converter.load();
converter.applyCoverage(entry.functions);
const instanbul = converter.toIstanbul();

entries[filePath] = instanbul[filePath];
});
await mkdir(path.join(process.cwd(), '.nyc_output'), { recursive: true });
await writeFile(path.join(process.cwd(), '.nyc_output', 'out.json'), JSON.stringify(entries));
}

await this.context.close();

Expand All @@ -307,6 +346,7 @@ class Runner {
});

await serverClose;

// eslint-disable-next-line unicorn/no-process-exit
process.exit(fail ? 1 : 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const getPw = async (browserName) => {
const { installBrowsersWithProgressBar } = require('playwright-core/lib/install/installer');
const { Playwright } = require('playwright-core/lib/server/playwright');
const { setupInProcess } = require('playwright-core/lib/inprocess');
const browsers = JSON.parse(fs.readFileSync('./node_modules/playwright-core/browsers.json').toString());
const browsers = require('playwright-core/browsers.json');

browsers.browsers[0].download = true; // chromium
browsers.browsers[1].download = true; // firefox
Expand Down
31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@
resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.0.tgz#9140779736aa2655635ee756e2467d787cfe8a2a"
integrity sha512-c3Xy026kOF7QOTn00hbIllV1dLR9hG9NkSrLQgCVs8NF6sBU+VGWjD3wLPhmh1TYAc7ugCFsvHYMN4VcBN1U1A==

"@types/istanbul-lib-coverage@^2.0.1":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.3.tgz#4ba8ddb720221f432e443bd5f9117fd22cfd4762"
integrity sha512-sz7iLqvVUg1gIedBOvlkxPlc8/uVzyS5OwGz1cKjXzkl3FpL3al0crU8YGU1WoHkxn0Wxbw5tyi6hvzJKNzFsw==

"@types/keyv@*", "@types/keyv@^3.1.1":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@types/keyv/-/keyv-3.1.1.tgz#e45a45324fca9dab716ab1230ee249c9fb52cfa7"
Expand Down Expand Up @@ -1656,6 +1661,13 @@ conventional-commits-parser@^3.2.0:
through2 "^4.0.0"
trim-off-newlines "^1.0.0"

convert-source-map@^1.6.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.7.0.tgz#17a2cb882d7f77d3490585e2ce6c524424a3a442"
integrity sha512-4FJkXzKXEDB1snCFZlLP4gpC3JILicCpGbzG9f9G7tGqGCzETQ2hWPrcinA9oU4wtf2biUaEH5065UnMeR33oA==
dependencies:
safe-buffer "~5.1.1"

copy-concurrently@^1.0.0:
version "1.0.5"
resolved "https://registry.yarnpkg.com/copy-concurrently/-/copy-concurrently-1.0.5.tgz#92297398cae34937fcafd6ec8139c18051f0b5e0"
Expand Down Expand Up @@ -5170,6 +5182,11 @@ p-defer@^1.0.0:
resolved "https://registry.yarnpkg.com/p-defer/-/p-defer-1.0.0.tgz#9f6eb182f6c9aa8cd743004a7d4f96b196b0fb0c"
integrity sha1-n26xgvbJqozXQwBKfU+WsZaw+ww=

p-each-series@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/p-each-series/-/p-each-series-2.1.0.tgz#961c8dd3f195ea96c747e636b262b800a6b1af48"
integrity sha512-ZuRs1miPT4HrjFa+9fRfOFXxGJfORgelKV9f9nNOWw2gl6gVsRaVDOQP0+MI0G0wGKns1Yacsu0GjOFbTK0JFQ==

p-event@^4.0.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/p-event/-/p-event-4.2.0.tgz#af4b049c8acd91ae81083ebd1e6f5cae2044c1b5"
Expand Down Expand Up @@ -6491,6 +6508,11 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

source-map@^0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

spdx-correct@^3.0.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.1.tgz#dece81ac9c1e6713e5f7d1b6f17d468fa53d89a9"
Expand Down Expand Up @@ -7339,6 +7361,15 @@ uvu@^0.4.0:
sade "^1.7.3"
totalist "^2.0.0"

v8-to-istanbul@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/v8-to-istanbul/-/v8-to-istanbul-7.0.0.tgz#b4fe00e35649ef7785a9b7fcebcea05f37c332fc"
integrity sha512-fLL2rFuQpMtm9r8hrAV2apXX/WqHJ6+IC4/eQVdMDGBUgH/YMV4Gv3duk3kjmyg6uiQWBAA9nJwue4iJUOkHeA==
dependencies:
"@types/istanbul-lib-coverage" "^2.0.1"
convert-source-map "^1.6.0"
source-map "^0.7.3"

validate-npm-package-license@^3.0.1:
version "3.0.4"
resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz#fc91f6b9c7ba15c857f4cb2c5defeec39d4f410a"
Expand Down