From 2ba5eb66d0899563764e5ae01d8078720367a296 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 9 Nov 2020 11:10:11 +0000 Subject: [PATCH 1/3] feat: add cov support --- .github/workflows/main.yml | 2 +- cli.js | 17 +++++++-- package.json | 8 +++- readme.md | 76 ++++++++++++++++++++------------------ src/runner.js | 37 +++++++++++++++++++ src/utils.js | 2 +- yarn.lock | 31 ++++++++++++++++ 7 files changed, 130 insertions(+), 43 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b0670581..a3440830 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,7 +24,7 @@ jobs: - 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 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 gstreamer1.0-libav sudo apt-get install xvfb - run: yarn - run: yarn lint diff --git a/cli.js b/cli.js index 212d6694..cbf3dc8f 100755 --- a/cli.js +++ b/cli.js @@ -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. @@ -102,6 +106,10 @@ Usage node: { type: 'boolean', default: true + }, + cov: { + type: 'boolean', + default: false } } }); @@ -132,7 +140,8 @@ const runnerOptions = () => { 'extensions', 'assets', 'before', - 'node' + 'node', + 'cov' ]; if (!localFlags.includes(key)) { @@ -153,7 +162,6 @@ let Runner = null; if (cli.flags.runner === 'zora') { Runner = ZoraRunner; } - if (cli.flags.runner === 'benchmark') { Runner = BenchmarkRunner; } @@ -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) { diff --git a/package.json b/package.json index 403b56e5..c31c1893 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,11 @@ "tape", "benchmark", "benchmark.js", - "zora" + "zora", + "coverage", + "istanbul", + "nyc", + "code coverage" ], "dependencies": { "camelcase": "^6.2.0", @@ -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" }, diff --git a/readme.md b/readme.md index e77951e7..03de2e16 100644 --- a/readme.md +++ b/readme.md @@ -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 ``` diff --git a/src/runner.js b/src/runner.js index ba3add54..e9d663b7 100644 --- a/src/runner.js +++ b/src/runner.js @@ -2,6 +2,7 @@ 'use strict'; const fs = require('fs'); +const { writeFile, mkdir } = require('fs/promises'); const path = require('path'); const webpack = require('webpack'); const getPort = require('get-port'); @@ -10,7 +11,10 @@ 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 envPaths = require('env-paths')('playwright-test'); const { redirectConsole, getPw, compile } = require('./utils'); @@ -20,6 +24,7 @@ const defaultOptions = { incognito: false, extension: false, debug: false, + cov: false, files: [], runnerOptions: {}, webpackConfig: {} @@ -124,6 +129,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); @@ -294,6 +303,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(); @@ -307,6 +343,7 @@ class Runner { }); await serverClose; + // eslint-disable-next-line unicorn/no-process-exit process.exit(fail ? 1 : 0); } diff --git a/src/utils.js b/src/utils.js index d5bf7bc4..b3b93bf4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 diff --git a/yarn.lock b/yarn.lock index a6a90905..0c3d8d38 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" @@ -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" From dc362a9f024b75cb4948ec250d55b46fe0bfb2d0 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 9 Nov 2020 11:18:04 +0000 Subject: [PATCH 2/3] fix: fs requires --- src/runner.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/runner.js b/src/runner.js index e9d663b7..ad07c359 100644 --- a/src/runner.js +++ b/src/runner.js @@ -2,7 +2,7 @@ 'use strict'; const fs = require('fs'); -const { writeFile, mkdir } = require('fs/promises'); +const { promisify } = require('util'); const path = require('path'); const webpack = require('webpack'); const getPort = require('get-port'); @@ -15,6 +15,9 @@ 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'); From 05315fe1927e0b4da5784eea7dd85344414c90a1 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 9 Nov 2020 11:27:34 +0000 Subject: [PATCH 3/3] fix: use pw action in the ci --- .github/workflows/main.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a3440830..01c507b6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 gstreamer1.0-libav - sudo apt-get install xvfb + - uses: microsoft/playwright-github-action@v1 - run: yarn - run: yarn lint - run: xvfb-run -a yarn test