From 1e96da2bb68203a5e972645df51ed0aa47ccaa8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BE=8A=E9=B9=BF?= Date: Thu, 31 Mar 2022 16:28:10 +0800 Subject: [PATCH] feat: support c8 report (#172) * feat: support c8 report --- README.md | 6 +- bin/c8.js | 5 + lib/cmd/cov.js | 46 +++-- package.json | 4 +- .../app/assets/index.js | 3 + .../app/assets/subdir/home.js | 3 + .../app/public/bar.js | 3 + .../app/public/subdir/home.js | 3 + .../app/view/subdir/home.js | 3 + .../config/config.default.js | 3 + .../config/config.prod.js | 3 + .../config/plugin.js | 3 + .../test-files-c8-report-only/config/proxy.js | 3 + .../test-files-c8-report-only/docs/home.js | 3 + .../test-files-c8-report-only/example/foo.js | 3 + .../test-files-c8-report-only/examples/foo.js | 3 + .../test-files-c8-report-only/ignore/a.js | 3 + .../test-files-c8-report-only/lib/a.js | 8 + .../test-files-c8-report-only/mocks/foo.js | 3 + .../mocks_data/foo/foo.js | 3 + .../test-files-c8-report-only/package.json | 6 + .../test-files-c8-report-only/test/a.js | 9 + .../test-files-c8-report-only/test/a.test.js | 15 ++ .../test/b/b.test.js | 5 + .../test-files-c8-report-only/test/fail.js | 7 + .../test/ignore.test.js | 10 + .../test/no-timeouts.test.js | 7 + .../test/power-assert-fail.js | 9 + .../test-files-c8/app/assets/index.js | 3 + .../test-files-c8/app/assets/subdir/home.js | 3 + test/fixtures/test-files-c8/app/public/bar.js | 3 + .../test-files-c8/app/public/subdir/home.js | 3 + .../test-files-c8/app/view/subdir/home.js | 3 + .../test-files-c8/config/config.default.js | 3 + .../test-files-c8/config/config.prod.js | 3 + test/fixtures/test-files-c8/config/plugin.js | 3 + test/fixtures/test-files-c8/config/proxy.js | 3 + test/fixtures/test-files-c8/docs/home.js | 3 + test/fixtures/test-files-c8/example/foo.js | 3 + test/fixtures/test-files-c8/examples/foo.js | 3 + test/fixtures/test-files-c8/ignore/a.js | 3 + test/fixtures/test-files-c8/lib/a.js | 8 + test/fixtures/test-files-c8/mocks/foo.js | 3 + .../test-files-c8/mocks_data/foo/foo.js | 3 + test/fixtures/test-files-c8/package.json | 6 + test/fixtures/test-files-c8/test/a.js | 9 + test/fixtures/test-files-c8/test/a.test.js | 15 ++ test/fixtures/test-files-c8/test/b/b.test.js | 5 + test/fixtures/test-files-c8/test/fail.js | 7 + .../test-files-c8/test/ignore.test.js | 10 + .../test-files-c8/test/no-timeouts.test.js | 7 + .../test-files-c8/test/power-assert-fail.js | 9 + test/lib/cmd/cov-c8-report.test.js | 181 ++++++++++++++++++ test/ts.test.js | 3 +- 54 files changed, 468 insertions(+), 19 deletions(-) create mode 100755 bin/c8.js create mode 100644 test/fixtures/test-files-c8-report-only/app/assets/index.js create mode 100644 test/fixtures/test-files-c8-report-only/app/assets/subdir/home.js create mode 100644 test/fixtures/test-files-c8-report-only/app/public/bar.js create mode 100644 test/fixtures/test-files-c8-report-only/app/public/subdir/home.js create mode 100644 test/fixtures/test-files-c8-report-only/app/view/subdir/home.js create mode 100644 test/fixtures/test-files-c8-report-only/config/config.default.js create mode 100644 test/fixtures/test-files-c8-report-only/config/config.prod.js create mode 100644 test/fixtures/test-files-c8-report-only/config/plugin.js create mode 100644 test/fixtures/test-files-c8-report-only/config/proxy.js create mode 100644 test/fixtures/test-files-c8-report-only/docs/home.js create mode 100644 test/fixtures/test-files-c8-report-only/example/foo.js create mode 100644 test/fixtures/test-files-c8-report-only/examples/foo.js create mode 100644 test/fixtures/test-files-c8-report-only/ignore/a.js create mode 100644 test/fixtures/test-files-c8-report-only/lib/a.js create mode 100644 test/fixtures/test-files-c8-report-only/mocks/foo.js create mode 100644 test/fixtures/test-files-c8-report-only/mocks_data/foo/foo.js create mode 100644 test/fixtures/test-files-c8-report-only/package.json create mode 100644 test/fixtures/test-files-c8-report-only/test/a.js create mode 100644 test/fixtures/test-files-c8-report-only/test/a.test.js create mode 100644 test/fixtures/test-files-c8-report-only/test/b/b.test.js create mode 100644 test/fixtures/test-files-c8-report-only/test/fail.js create mode 100644 test/fixtures/test-files-c8-report-only/test/ignore.test.js create mode 100644 test/fixtures/test-files-c8-report-only/test/no-timeouts.test.js create mode 100644 test/fixtures/test-files-c8-report-only/test/power-assert-fail.js create mode 100644 test/fixtures/test-files-c8/app/assets/index.js create mode 100644 test/fixtures/test-files-c8/app/assets/subdir/home.js create mode 100644 test/fixtures/test-files-c8/app/public/bar.js create mode 100644 test/fixtures/test-files-c8/app/public/subdir/home.js create mode 100644 test/fixtures/test-files-c8/app/view/subdir/home.js create mode 100644 test/fixtures/test-files-c8/config/config.default.js create mode 100644 test/fixtures/test-files-c8/config/config.prod.js create mode 100644 test/fixtures/test-files-c8/config/plugin.js create mode 100644 test/fixtures/test-files-c8/config/proxy.js create mode 100644 test/fixtures/test-files-c8/docs/home.js create mode 100644 test/fixtures/test-files-c8/example/foo.js create mode 100644 test/fixtures/test-files-c8/examples/foo.js create mode 100644 test/fixtures/test-files-c8/ignore/a.js create mode 100644 test/fixtures/test-files-c8/lib/a.js create mode 100644 test/fixtures/test-files-c8/mocks/foo.js create mode 100644 test/fixtures/test-files-c8/mocks_data/foo/foo.js create mode 100644 test/fixtures/test-files-c8/package.json create mode 100644 test/fixtures/test-files-c8/test/a.js create mode 100644 test/fixtures/test-files-c8/test/a.test.js create mode 100644 test/fixtures/test-files-c8/test/b/b.test.js create mode 100644 test/fixtures/test-files-c8/test/fail.js create mode 100644 test/fixtures/test-files-c8/test/ignore.test.js create mode 100644 test/fixtures/test-files-c8/test/no-timeouts.test.js create mode 100644 test/fixtures/test-files-c8/test/power-assert-fail.js create mode 100644 test/lib/cmd/cov-c8-report.test.js diff --git a/README.md b/README.md index 99e6d046..1b183511 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ TEST_TIMEOUT=2000 egg-bin test ### cov -Using [nyc] to run code coverage, it support all test params above. +Using [nyc] or [c8] to run code coverage, it support all test params above. Coverage reporter will output text-summary, json and lcov. @@ -177,6 +177,10 @@ You can pass any mocha argv. > - when same key exists in `.nycrc` and cmd instruments, nyc prefers instrument. > - egg-bin have some default instruments passed to nyc like `-r` and `--temp-directory` > - `egg-bin cov --nyc="-r teamcity -r text"` +- `--c8` c8 instruments passthrough. you can use this to overwrite egg-bin's default c8 instruments and add additional ones. + > - egg-bin have some default instruments passed to c8 like `-r` and `--temp-directory` + > - `egg-bin cov --c8="-r teamcity -r text" --c8-report=true` +- `--c8-report` use c8 to report coverage not nyc, c8 uses native V8 coverage, make sure you're running Node.js >= 10.12.0, default to `false`. - also support all test params above. diff --git a/bin/c8.js b/bin/c8.js new file mode 100755 index 00000000..b71f04a8 --- /dev/null +++ b/bin/c8.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +'use strict'; + +require('c8/bin/c8'); diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index b4c1fa34..61fc514e 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -30,6 +30,16 @@ class CovCommand extends Command { type: 'string', default: '--temp-directory ./node_modules/.nyc_output -r text-summary -r json-summary -r json -r lcov', }, + c8: { + description: 'c8 instruments passthrough', + type: 'string', + default: '--temp-directory ./node_modules/.c8_output -r text-summary -r json-summary -r json -r lcov', + }, + 'c8-report': { + description: 'use c8 to report coverage, default to false', + type: 'boolean', + default: false, + }, }; // you can add ignore dirs here @@ -68,12 +78,6 @@ class CovCommand extends Command { this.addExclude(exclude); } - const nycCli = require.resolve('nyc/bin/nyc.js'); - const coverageDir = path.join(cwd, 'coverage'); - yield rimraf(coverageDir); - const outputDir = path.join(cwd, 'node_modules/.nyc_output'); - yield rimraf(outputDir); - const opt = { cwd, execArgv, @@ -87,12 +91,19 @@ class CovCommand extends Command { if (context.argv.typescript) { opt.env.SPAWN_WRAP_SHIM_ROOT = path.join(cwd, 'node_modules'); } - - // save coverage-xxxx.json to $PWD/coverage + let cli = require.resolve('nyc/bin/nyc.js'); + let outputDir = path.join(cwd, 'node_modules/.nyc_output'); + if (argv['c8-report']) { + cli = require.resolve('c8/bin/c8.js'); + outputDir = path.join(cwd, 'node_modules/.c8_output'); + } + yield rimraf(outputDir); + const coverageDir = path.join(cwd, 'coverage'); + yield rimraf(coverageDir); const covArgs = yield this.getCovArgs(context); if (!covArgs) return; debug('covArgs: %j', covArgs); - yield this.helper.forkNode(nycCli, covArgs, opt); + yield this.helper.forkNode(cli, covArgs, opt); } /** @@ -102,7 +113,6 @@ class CovCommand extends Command { addExclude(exclude) { this[EXCLUDES].add(exclude); } - /** * get coverage args * @param {Object} context - { cwd, argv, ...} @@ -121,20 +131,24 @@ class CovCommand extends Command { this.addExclude('**/*.d.ts'); } - // nyc args passthrough - const nycArgs = context.argv.nyc; + // nyc or c8 args passthrough + let passthroughArgs = context.argv.nyc; + if (context.argv['c8-report']) { + passthroughArgs = context.argv.c8; + context.argv['c8-report'] = undefined; + } context.argv.nyc = undefined; - if (nycArgs) { - covArgs = covArgs.concat(nycArgs.split(' ')); + context.argv.c8 = undefined; + if (passthroughArgs) { + covArgs = covArgs.concat(passthroughArgs.split(' ')); } - for (const exclude of this[EXCLUDES]) { covArgs.push('-x'); covArgs.push(exclude); } - covArgs.push(require.resolve('mocha/bin/_mocha')); const testArgs = yield this.formatTestArgs(context); if (!testArgs) return; + covArgs.push(require.resolve('mocha/bin/_mocha')); covArgs = covArgs.concat(testArgs); return covArgs; } diff --git a/package.json b/package.json index 9afa1d5e..f6d70b7f 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "bin": { "egg-bin": "bin/egg-bin.js", "mocha": "bin/mocha.js", - "ets": "bin/ets.js" + "ets": "bin/ets.js", + "c8": "bin/c8.js" }, "dependencies": { "chalk": "^4.1.1", @@ -25,6 +26,7 @@ "mocha": "^6.0.2", "mz-modules": "^2.1.0", "nyc": "^13.3.0", + "c8": "^7.11.0", "power-assert": "^1.6.1", "semver": "^7.3.5", "source-map-support": "^0.5.19", diff --git a/test/fixtures/test-files-c8-report-only/app/assets/index.js b/test/fixtures/test-files-c8-report-only/app/assets/index.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/app/assets/index.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/app/assets/subdir/home.js b/test/fixtures/test-files-c8-report-only/app/assets/subdir/home.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/app/assets/subdir/home.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/app/public/bar.js b/test/fixtures/test-files-c8-report-only/app/public/bar.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/app/public/bar.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/app/public/subdir/home.js b/test/fixtures/test-files-c8-report-only/app/public/subdir/home.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/app/public/subdir/home.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/app/view/subdir/home.js b/test/fixtures/test-files-c8-report-only/app/view/subdir/home.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/app/view/subdir/home.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/config/config.default.js b/test/fixtures/test-files-c8-report-only/config/config.default.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/config/config.default.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/config/config.prod.js b/test/fixtures/test-files-c8-report-only/config/config.prod.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/config/config.prod.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/config/plugin.js b/test/fixtures/test-files-c8-report-only/config/plugin.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/config/plugin.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/config/proxy.js b/test/fixtures/test-files-c8-report-only/config/proxy.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/config/proxy.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/docs/home.js b/test/fixtures/test-files-c8-report-only/docs/home.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/docs/home.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/example/foo.js b/test/fixtures/test-files-c8-report-only/example/foo.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/example/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/examples/foo.js b/test/fixtures/test-files-c8-report-only/examples/foo.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/examples/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/ignore/a.js b/test/fixtures/test-files-c8-report-only/ignore/a.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/ignore/a.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/lib/a.js b/test/fixtures/test-files-c8-report-only/lib/a.js new file mode 100644 index 00000000..446f37e1 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/lib/a.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = condition => { + if (condition) { + return 'a'; + } + return 'b'; +}; diff --git a/test/fixtures/test-files-c8-report-only/mocks/foo.js b/test/fixtures/test-files-c8-report-only/mocks/foo.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/mocks/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/mocks_data/foo/foo.js b/test/fixtures/test-files-c8-report-only/mocks_data/foo/foo.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/mocks_data/foo/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8-report-only/package.json b/test/fixtures/test-files-c8-report-only/package.json new file mode 100644 index 00000000..ee78010c --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/package.json @@ -0,0 +1,6 @@ +{ + "name": "test-files", + "files": [ + "lib" + ] +} \ No newline at end of file diff --git a/test/fixtures/test-files-c8-report-only/test/a.js b/test/fixtures/test-files-c8-report-only/test/a.js new file mode 100644 index 00000000..4349dee7 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/test/a.js @@ -0,0 +1,9 @@ +'use strict'; + +const a = require('../lib/a'); + +describe('a.js', () => { + it('should success', () => { + a(true); + }); +}); diff --git a/test/fixtures/test-files-c8-report-only/test/a.test.js b/test/fixtures/test-files-c8-report-only/test/a.test.js new file mode 100644 index 00000000..d1829307 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/test/a.test.js @@ -0,0 +1,15 @@ +'use strict'; + +const fs = require('fs'); +const a = require('../lib/a'); + +describe('a.test.js', () => { + it('should success', () => { + a(true); + }); + + it('should show tmp', () => { + const tmpdir = process.env.TMPDIR; + console.log(tmpdir, fs.existsSync(tmpdir)); + }); +}); diff --git a/test/fixtures/test-files-c8-report-only/test/b/b.test.js b/test/fixtures/test-files-c8-report-only/test/b/b.test.js new file mode 100644 index 00000000..d3ce29d9 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/test/b/b.test.js @@ -0,0 +1,5 @@ +'use strict'; + +describe('b/b.test.js', () => { + it('should success', () => {}); +}); diff --git a/test/fixtures/test-files-c8-report-only/test/fail.js b/test/fixtures/test-files-c8-report-only/test/fail.js new file mode 100644 index 00000000..f0b3ab53 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/test/fail.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('fail.js', () => { + it('should fail', () => { + throw new Error('fail.js throw'); + }); +}); diff --git a/test/fixtures/test-files-c8-report-only/test/ignore.test.js b/test/fixtures/test-files-c8-report-only/test/ignore.test.js new file mode 100644 index 00000000..7a899f13 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/test/ignore.test.js @@ -0,0 +1,10 @@ +'use strict'; + +const assert = require('assert'); +const a = require('../ignore/a'); + +describe('ignore.test.js', () => { + it('should success', () => { + assert(a === ''); + }); +}); diff --git a/test/fixtures/test-files-c8-report-only/test/no-timeouts.test.js b/test/fixtures/test-files-c8-report-only/test/no-timeouts.test.js new file mode 100644 index 00000000..1b485902 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/test/no-timeouts.test.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('no-timeouts.test.js', () => { + it('should success', function() { + console.log(`timeout: ${this.timeout()}`); + }); +}); diff --git a/test/fixtures/test-files-c8-report-only/test/power-assert-fail.js b/test/fixtures/test-files-c8-report-only/test/power-assert-fail.js new file mode 100644 index 00000000..6dfccb32 --- /dev/null +++ b/test/fixtures/test-files-c8-report-only/test/power-assert-fail.js @@ -0,0 +1,9 @@ +'use strict'; + +const assert = require('power-assert'); + +describe('power-assert-fail.js', () => { + it('should fail', () => { + assert(1 === 2); + }); +}); diff --git a/test/fixtures/test-files-c8/app/assets/index.js b/test/fixtures/test-files-c8/app/assets/index.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/app/assets/index.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/app/assets/subdir/home.js b/test/fixtures/test-files-c8/app/assets/subdir/home.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/app/assets/subdir/home.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/app/public/bar.js b/test/fixtures/test-files-c8/app/public/bar.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/app/public/bar.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/app/public/subdir/home.js b/test/fixtures/test-files-c8/app/public/subdir/home.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/app/public/subdir/home.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/app/view/subdir/home.js b/test/fixtures/test-files-c8/app/view/subdir/home.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/app/view/subdir/home.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/config/config.default.js b/test/fixtures/test-files-c8/config/config.default.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/config/config.default.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/config/config.prod.js b/test/fixtures/test-files-c8/config/config.prod.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/config/config.prod.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/config/plugin.js b/test/fixtures/test-files-c8/config/plugin.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/config/plugin.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/config/proxy.js b/test/fixtures/test-files-c8/config/proxy.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/config/proxy.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/docs/home.js b/test/fixtures/test-files-c8/docs/home.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/docs/home.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/example/foo.js b/test/fixtures/test-files-c8/example/foo.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/example/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/examples/foo.js b/test/fixtures/test-files-c8/examples/foo.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/examples/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/ignore/a.js b/test/fixtures/test-files-c8/ignore/a.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/ignore/a.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/lib/a.js b/test/fixtures/test-files-c8/lib/a.js new file mode 100644 index 00000000..446f37e1 --- /dev/null +++ b/test/fixtures/test-files-c8/lib/a.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = condition => { + if (condition) { + return 'a'; + } + return 'b'; +}; diff --git a/test/fixtures/test-files-c8/mocks/foo.js b/test/fixtures/test-files-c8/mocks/foo.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/mocks/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/mocks_data/foo/foo.js b/test/fixtures/test-files-c8/mocks_data/foo/foo.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files-c8/mocks_data/foo/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files-c8/package.json b/test/fixtures/test-files-c8/package.json new file mode 100644 index 00000000..ee78010c --- /dev/null +++ b/test/fixtures/test-files-c8/package.json @@ -0,0 +1,6 @@ +{ + "name": "test-files", + "files": [ + "lib" + ] +} \ No newline at end of file diff --git a/test/fixtures/test-files-c8/test/a.js b/test/fixtures/test-files-c8/test/a.js new file mode 100644 index 00000000..4349dee7 --- /dev/null +++ b/test/fixtures/test-files-c8/test/a.js @@ -0,0 +1,9 @@ +'use strict'; + +const a = require('../lib/a'); + +describe('a.js', () => { + it('should success', () => { + a(true); + }); +}); diff --git a/test/fixtures/test-files-c8/test/a.test.js b/test/fixtures/test-files-c8/test/a.test.js new file mode 100644 index 00000000..d1829307 --- /dev/null +++ b/test/fixtures/test-files-c8/test/a.test.js @@ -0,0 +1,15 @@ +'use strict'; + +const fs = require('fs'); +const a = require('../lib/a'); + +describe('a.test.js', () => { + it('should success', () => { + a(true); + }); + + it('should show tmp', () => { + const tmpdir = process.env.TMPDIR; + console.log(tmpdir, fs.existsSync(tmpdir)); + }); +}); diff --git a/test/fixtures/test-files-c8/test/b/b.test.js b/test/fixtures/test-files-c8/test/b/b.test.js new file mode 100644 index 00000000..d3ce29d9 --- /dev/null +++ b/test/fixtures/test-files-c8/test/b/b.test.js @@ -0,0 +1,5 @@ +'use strict'; + +describe('b/b.test.js', () => { + it('should success', () => {}); +}); diff --git a/test/fixtures/test-files-c8/test/fail.js b/test/fixtures/test-files-c8/test/fail.js new file mode 100644 index 00000000..f0b3ab53 --- /dev/null +++ b/test/fixtures/test-files-c8/test/fail.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('fail.js', () => { + it('should fail', () => { + throw new Error('fail.js throw'); + }); +}); diff --git a/test/fixtures/test-files-c8/test/ignore.test.js b/test/fixtures/test-files-c8/test/ignore.test.js new file mode 100644 index 00000000..7a899f13 --- /dev/null +++ b/test/fixtures/test-files-c8/test/ignore.test.js @@ -0,0 +1,10 @@ +'use strict'; + +const assert = require('assert'); +const a = require('../ignore/a'); + +describe('ignore.test.js', () => { + it('should success', () => { + assert(a === ''); + }); +}); diff --git a/test/fixtures/test-files-c8/test/no-timeouts.test.js b/test/fixtures/test-files-c8/test/no-timeouts.test.js new file mode 100644 index 00000000..1b485902 --- /dev/null +++ b/test/fixtures/test-files-c8/test/no-timeouts.test.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('no-timeouts.test.js', () => { + it('should success', function() { + console.log(`timeout: ${this.timeout()}`); + }); +}); diff --git a/test/fixtures/test-files-c8/test/power-assert-fail.js b/test/fixtures/test-files-c8/test/power-assert-fail.js new file mode 100644 index 00000000..6dfccb32 --- /dev/null +++ b/test/fixtures/test-files-c8/test/power-assert-fail.js @@ -0,0 +1,9 @@ +'use strict'; + +const assert = require('power-assert'); + +describe('power-assert-fail.js', () => { + it('should fail', () => { + assert(1 === 2); + }); +}); diff --git a/test/lib/cmd/cov-c8-report.test.js b/test/lib/cmd/cov-c8-report.test.js new file mode 100644 index 00000000..69895b24 --- /dev/null +++ b/test/lib/cmd/cov-c8-report.test.js @@ -0,0 +1,181 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const assert = require('assert'); +const coffee = require('coffee'); +const mm = require('mm'); +const rimraf = require('mz-modules/rimraf'); + +describe('test/lib/cmd/cov-c8-report.test.js', () => { + if (parseInt(process.versions.node.split('.')[0]) < 10) { + console.log('skip test c8 report when node version < 10'); + return; + } + const eggBin = require.resolve('../../../bin/egg-bin.js'); + const cwd = path.join(__dirname, '../../fixtures/test-files-c8'); + + beforeEach(() => rimraf(path.join(cwd, 'coverage'))); + afterEach(mm.restore); + + function assertCoverage(cwd) { + assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); + assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-summary.json'))); + assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); + assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); + } + + it('should success when c8-report', function* () { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + const child = coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) + // .debug() + .expect('stdout', /should success/) + .expect('stdout', /a\.test\.js/) + .expect('stdout', /b[\/|\\]b\.test\.js/) + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); + yield child.expect('code', 0).end(); + assertCoverage(cwd); + }); + + it('should exit when not test files', done => { + coffee.fork(eggBin, [ 'cov', '--c8-report=true', 'test/**/*.nth.js' ], { cwd }) + // .debug() + .expect('stdout', /No test files found/) + .expect('code', 0) + .end(done); + }); + + it('should hotfixSpawnWrap success on mock windows', function* () { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + const child = coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) + // .debug() + .beforeScript(path.join(__dirname, 'mock-win32.js')) + .expect('stdout', /should success/) + .expect('stdout', /a\.test\.js/) + .expect('stdout', /b[\/|\\]b\.test\.js/) + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); + yield child.expect('code', 0).end(); + assertCoverage(cwd); + }); + + it('should success with COV_EXCLUDES', function* () { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + mm(process.env, 'COV_EXCLUDES', 'ignore/*'); + const child = coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) + // .debug() + .expect('stdout', /should success/) + .expect('stdout', /a\.test\.js/) + .expect('stdout', /b[\/|\\]b\.test\.js/) + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + yield child.expect('code', 0).end(); + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + }); + + it('should success with -x to ignore one dirs', function* () { + const child = coffee.fork(eggBin, [ 'cov', '--c8-report=true', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }) + // .debug() + .expect('stdout', /should success/) + .expect('stdout', /a\.test\.js/) + .expect('stdout', /b[\/|\\]b\.test\.js/) + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + + + yield child.expect('code', 0).end(); + + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + + }); + + it('should success with -x to ignore multi dirs', function* () { + const child = coffee.fork(eggBin, [ 'cov', '--c8-report=true', '-x', 'ignore2/*', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }) + // .debug() + .expect('stdout', /should success/) + .expect('stdout', /a\.test\.js/) + .expect('stdout', /b[\/|\\]b\.test\.js/) + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + yield child.expect('code', 0).end(); + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + + }); + + it('should fail when test fail', done => { + mm(process.env, 'TESTS', 'test/fail.js'); + coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) + // .debug() + .expect('stdout', /1\) should fail/) + .expect('stdout', /1 failing/) + .expect('code', 1) + .end(done); + }); + + it('should fail when test fail with power-assert', done => { + mm(process.env, 'TESTS', 'test/power-assert-fail.js'); + coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) + // .debug() + .expect('stdout', /1\) should fail/) + .expect('stdout', /1 failing/) + .expect('stdout', /assert\(1 === 2\)/) + .expect('code', 1) + .end(done); + }); + + it('should warn when require intelli-espower-loader', done => { + mm(process.env, 'TESTS', 'test/power-assert-fail.js'); + coffee.fork(eggBin, [ 'cov', '--c8-report=true', '-r', 'intelli-espower-loader' ], { cwd }) + // .debug() + .expect('stderr', /manually require `intelli-espower-loader`/) + .expect('stdout', /1\) should fail/) + .expect('stdout', /1 failing/) + .expect('stdout', /assert\(1 === 2\)/) + .expect('code', 1) + .end(done); + }); + + it('should run cov when no test files', function* () { + mm(process.env, 'TESTS', 'noexist.js'); + const cwd = path.join(__dirname, '../../fixtures/prerequire'); + yield coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) + // .debug() + .expect('code', 0) + .end(); + }); + + it('should set EGG_BIN_PREREQUIRE', function* () { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + const cwd = path.join(__dirname, '../../fixtures/prerequire'); + yield coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) + // .debug() + .coverage(false) + .expect('stdout', /EGG_BIN_PREREQUIRE undefined/) + .expect('code', 0) + .end(); + + yield coffee.fork(eggBin, [ 'cov', '--c8-report=true', '--prerequire' ], { cwd }) + // .debug() + .coverage(false) + .expect('stdout', /EGG_BIN_PREREQUIRE true/) + .expect('code', 0) + .end(); + }); + + it('should passthrough c8 args', done => { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + coffee.fork(eggBin, [ 'cov', '--c8-report=true', '--c8=-r teamcity -r text' ], { cwd }) + // .debug() + .expect('stdout', /should success/) + .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/) + .expect('stdout', /##teamcity\[blockClosed name='Code Coverage Summary'\]/) + .end(done); + }); +}); diff --git a/test/ts.test.js b/test/ts.test.js index 5008b0c2..3714a859 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -356,6 +356,7 @@ describe('test/ts.test.js', () => { }); it('should load egg-ts-helper with dts flag', () => { + fs.mkdirSync(path.join(cwd, 'typings')); return coffee.fork(eggBin, [ 'dev', '--dts' ], { cwd }) // .debug() .expect('stdout', /application log/) @@ -366,9 +367,9 @@ describe('test/ts.test.js', () => { }); it('should load egg-ts-helper with egg.declarations = true', () => { + fs.mkdirSync(path.join(cwd, 'typings')); pkgJson.egg.declarations = true; fs.writeFileSync(path.resolve(cwd, './package.json'), JSON.stringify(pkgJson, null, 2)); - return coffee.fork(eggBin, [ 'dev' ], { cwd }) // .debug() .expect('stdout', /application log/)