From 9094d4eeb05f6049fdc38ce68974095d2cfc780c Mon Sep 17 00:00:00 2001 From: onlylovermb Date: Tue, 29 Mar 2022 21:38:08 +0800 Subject: [PATCH 1/8] feat: support c8 report --- lib/cmd/cov.js | 73 ++++-- package.json | 1 + .../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-only.test.js | 98 ++++++++ test/lib/cmd/cov-c8-report.test.js | 213 ++++++++++++++++++ 52 files changed, 607 insertions(+), 20 deletions(-) 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-only.test.js create mode 100644 test/lib/cmd/cov-c8-report.test.js diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index b4c1fa34..b955d0ba 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -30,6 +30,19 @@ 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 ./coverage/tmp -r text-summary -r json-summary -r json -r lcov', + }, + 'c8-report': { + description: 'run test && use c8 to report coverage', + type: 'boolean', + }, + 'c8-report-only': { + description: 'no run test, only use c8 to report coverage only', + type: 'boolean', + }, }; // you can add ignore dirs here @@ -68,12 +81,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 +94,32 @@ 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 - const covArgs = yield this.getCovArgs(context); - if (!covArgs) return; - debug('covArgs: %j', covArgs); - yield this.helper.forkNode(nycCli, covArgs, opt); + if (argv['c8-report-only']) { + const c8Cli = require.resolve('c8/bin/c8.js'); + const covArgs = yield this.getCovArgs(context); + covArgs.unshift('report'); + yield this.helper.forkNode(c8Cli, covArgs, opt); + } else if (argv['c8-report']) { + // save coverage-xxxx.json to $PWD/coverage + const c8Cli = require.resolve('c8/bin/c8.js'); + 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(c8Cli, covArgs, opt); + } else { + 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); + // save coverage-xxxx.json to $PWD/coverage + const covArgs = yield this.getCovArgs(context); + if (!covArgs) return; + debug('covArgs: %j', covArgs); + yield this.helper.forkNode(nycCli, covArgs, opt); + } } /** @@ -102,7 +129,6 @@ class CovCommand extends Command { addExclude(exclude) { this[EXCLUDES].add(exclude); } - /** * get coverage args * @param {Object} context - { cwd, argv, ...} @@ -121,21 +147,28 @@ 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'] || context.argv['c8-report-only']) { + 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 = covArgs.concat(testArgs); + if (!context.argv['c8-report-only']) { + covArgs.push(require.resolve('mocha/bin/_mocha')); + covArgs = covArgs.concat(testArgs); + } + context.argv['c8-report-only'] = undefined; return covArgs; } } diff --git a/package.json b/package.json index 9afa1d5e..79f76540 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,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-only.test.js b/test/lib/cmd/cov-c8-report-only.test.js new file mode 100644 index 00000000..50695fc9 --- /dev/null +++ b/test/lib/cmd/cov-c8-report-only.test.js @@ -0,0 +1,98 @@ +'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-only.test.js', () => { + const eggBin = require.resolve('../../../bin/egg-bin.js'); + const cwd = path.join(__dirname, '../../fixtures/test-files-c8-report-only'); + + beforeEach(async () => { + rimraf(path.join(cwd, 'coverage')) + mm(process.env, 'TESTS', 'test/**/*.test.js'); + mm(process.env, 'NYC_CWD', cwd); + mm(process.env, 'NODE_V8_COVERAGE', 'coverage/tmp'); + const test = coffee.fork(eggBin, ['test'], { cwd }) + await test.expect('code', 0).end(); + }); + 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-only', function* () { + const child = coffee.fork(eggBin, ['cov', '--c8-report-only=true'], { cwd }).debug(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); + } + yield child.expect('code', 0).end(); + // only test on npm run test + console.log(process.env.NYC_ROOT_ID); + if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); + }); + + it('should success with COV_EXCLUDES', function* () { + mm(process.env, 'COV_EXCLUDES', 'ignore/*'); + const child = coffee.fork(eggBin, ['cov', '--c8-report-only=true'], { cwd }) + .debug() + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + } + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + 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-only=true', '-x', 'ignore/', 'test/**/*.test.js'], { cwd }) + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + } + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + 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-only=true', '-x', 'ignore2/*', '-x', 'ignore/', 'test/**/*.test.js'], { cwd }) + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + } + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + } + }); + + it('should passthrough c8 args', done => { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + mm(process.env, 'NYC_CWD', cwd); + coffee.fork(eggBin, ['cov', '--c8-report-only=true', '--c8=-r teamcity -r text'], { cwd }) + .debug() + .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/) + .expect('stdout', /##teamcity\[blockClosed name='Code Coverage Summary'\]/) + .end(done); + }); +}); 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..56d90b39 --- /dev/null +++ b/test/lib/cmd/cov-c8-report.test.js @@ -0,0 +1,213 @@ +'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', () => { + 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'); + mm(process.env, 'NYC_CWD', cwd); + 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/); + + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements/); + } + + yield child.expect('code', 0).end(); + // only test on npm run test + console.log(process.env.NYC_ROOT_ID); + if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); + }); + + it('should exit when not test files', done => { + mm(process.env, 'NYC_CWD', cwd); + 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'); + mm(process.env, 'NYC_CWD', cwd); + 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/); + + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); + } + + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) 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/); + + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + } + + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + 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/); + + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + } + + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + 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/); + + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + } + + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + 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'); + mm(process.env, 'NYC_CWD', cwd); + 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); + }); +}); From 898fe94ba94d833168c4a705e7a2c44f643d50fc Mon Sep 17 00:00:00 2001 From: onlylovermb Date: Tue, 29 Mar 2022 21:50:31 +0800 Subject: [PATCH 2/8] style: eslint --- test/lib/cmd/cov-c8-report-only.test.js | 162 ++++++++++++------------ 1 file changed, 81 insertions(+), 81 deletions(-) diff --git a/test/lib/cmd/cov-c8-report-only.test.js b/test/lib/cmd/cov-c8-report-only.test.js index 50695fc9..9d3dff9c 100644 --- a/test/lib/cmd/cov-c8-report-only.test.js +++ b/test/lib/cmd/cov-c8-report-only.test.js @@ -8,91 +8,91 @@ const mm = require('mm'); const rimraf = require('mz-modules/rimraf'); describe('test/lib/cmd/cov-c8-report-only.test.js', () => { - const eggBin = require.resolve('../../../bin/egg-bin.js'); - const cwd = path.join(__dirname, '../../fixtures/test-files-c8-report-only'); + const eggBin = require.resolve('../../../bin/egg-bin.js'); + const cwd = path.join(__dirname, '../../fixtures/test-files-c8-report-only'); - beforeEach(async () => { - rimraf(path.join(cwd, 'coverage')) - mm(process.env, 'TESTS', 'test/**/*.test.js'); - mm(process.env, 'NYC_CWD', cwd); - mm(process.env, 'NODE_V8_COVERAGE', 'coverage/tmp'); - const test = coffee.fork(eggBin, ['test'], { cwd }) - await test.expect('code', 0).end(); - }); - 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'))); - } + beforeEach(async () => { + rimraf(path.join(cwd, 'coverage')); + mm(process.env, 'TESTS', 'test/**/*.test.js'); + mm(process.env, 'NYC_CWD', cwd); + mm(process.env, 'NODE_V8_COVERAGE', 'coverage/tmp'); + const test = coffee.fork(eggBin, [ 'test' ], { cwd }); + await test.expect('code', 0).end(); + }); + 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-only', function* () { - const child = coffee.fork(eggBin, ['cov', '--c8-report-only=true'], { cwd }).debug(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); - } - yield child.expect('code', 0).end(); - // only test on npm run test - console.log(process.env.NYC_ROOT_ID); - if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); - }); + it('should success when c8-report-only', function* () { + const child = coffee.fork(eggBin, [ 'cov', '--c8-report-only=true' ], { cwd }).debug(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); + } + yield child.expect('code', 0).end(); + // only test on npm run test + console.log(process.env.NYC_ROOT_ID); + if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); + }); - it('should success with COV_EXCLUDES', function* () { - mm(process.env, 'COV_EXCLUDES', 'ignore/*'); - const child = coffee.fork(eggBin, ['cov', '--c8-report-only=true'], { cwd }) - .debug() - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - } - yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - assertCoverage(cwd); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); - } - }); + it('should success with COV_EXCLUDES', function* () { + mm(process.env, 'COV_EXCLUDES', 'ignore/*'); + const child = coffee.fork(eggBin, [ 'cov', '--c8-report-only=true' ], { cwd }) + .debug(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + } + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + 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-only=true', '-x', 'ignore/', 'test/**/*.test.js'], { cwd }) - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - } - yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - 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-only=true', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + } + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + 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-only=true', '-x', 'ignore2/*', '-x', 'ignore/', 'test/**/*.test.js'], { cwd }) - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - } - yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - 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-only=true', '-x', 'ignore2/*', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); + } + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + } + }); - it('should passthrough c8 args', done => { - mm(process.env, 'TESTS', 'test/**/*.test.js'); - mm(process.env, 'NYC_CWD', cwd); - coffee.fork(eggBin, ['cov', '--c8-report-only=true', '--c8=-r teamcity -r text'], { cwd }) - .debug() - .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/) - .expect('stdout', /##teamcity\[blockClosed name='Code Coverage Summary'\]/) - .end(done); - }); + it('should passthrough c8 args', done => { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + mm(process.env, 'NYC_CWD', cwd); + coffee.fork(eggBin, [ 'cov', '--c8-report-only=true', '--c8=-r teamcity -r text' ], { cwd }) + .debug() + .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/) + .expect('stdout', /##teamcity\[blockClosed name='Code Coverage Summary'\]/) + .end(done); + }); }); From 1cb272b90760da0e2557efd9e7a6dba7033aa5e1 Mon Sep 17 00:00:00 2001 From: onlylovermb Date: Wed, 30 Mar 2022 20:02:55 +0800 Subject: [PATCH 3/8] fix: --temp-directory=node_modules/.c8_output & export c8 --- bin/c8.js | 5 ++ lib/cmd/cov.js | 6 +- test/lib/cmd/cov-c8-report-only.test.js | 66 +++++---------- test/lib/cmd/cov-c8-report.test.js | 108 ++++++++---------------- 4 files changed, 67 insertions(+), 118 deletions(-) create mode 100644 bin/c8.js diff --git a/bin/c8.js b/bin/c8.js new file mode 100644 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 b955d0ba..0e9f88bf 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -33,14 +33,14 @@ class CovCommand extends Command { c8: { description: 'c8 instruments passthrough', type: 'string', - default: '--temp-directory ./coverage/tmp -r text-summary -r json-summary -r json -r lcov', + default: '--temp-directory ./node_modules/.c8_output -r text-summary -r json-summary -r json -r lcov', }, 'c8-report': { description: 'run test && use c8 to report coverage', type: 'boolean', }, 'c8-report-only': { - description: 'no run test, only use c8 to report coverage only', + description: 'no run test, only use c8 to report coverage', type: 'boolean', }, }; @@ -104,6 +104,8 @@ class CovCommand extends Command { const c8Cli = require.resolve('c8/bin/c8.js'); const coverageDir = path.join(cwd, 'coverage'); yield rimraf(coverageDir); + const outputDir = path.join(cwd, 'node_modules/.nyc_output'); + yield rimraf(outputDir); const covArgs = yield this.getCovArgs(context); if (!covArgs) return; debug('covArgs: %j', covArgs); diff --git a/test/lib/cmd/cov-c8-report-only.test.js b/test/lib/cmd/cov-c8-report-only.test.js index 9d3dff9c..3b9b0f9f 100644 --- a/test/lib/cmd/cov-c8-report-only.test.js +++ b/test/lib/cmd/cov-c8-report-only.test.js @@ -14,8 +14,7 @@ describe('test/lib/cmd/cov-c8-report-only.test.js', () => { beforeEach(async () => { rimraf(path.join(cwd, 'coverage')); mm(process.env, 'TESTS', 'test/**/*.test.js'); - mm(process.env, 'NYC_CWD', cwd); - mm(process.env, 'NODE_V8_COVERAGE', 'coverage/tmp'); + mm(process.env, 'NODE_V8_COVERAGE', 'node_modules/.c8_output'); const test = coffee.fork(eggBin, [ 'test' ], { cwd }); await test.expect('code', 0).end(); }); @@ -28,69 +27,46 @@ describe('test/lib/cmd/cov-c8-report-only.test.js', () => { } it('should success when c8-report-only', function* () { - const child = coffee.fork(eggBin, [ 'cov', '--c8-report-only=true' ], { cwd }).debug(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); - } + const child = coffee.fork(eggBin, [ 'cov', '--c8-report-only=true' ], { cwd }) + // .debug() + .expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); yield child.expect('code', 0).end(); - // only test on npm run test - console.log(process.env.NYC_ROOT_ID); - if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); + assertCoverage(cwd); }); it('should success with COV_EXCLUDES', function* () { mm(process.env, 'COV_EXCLUDES', 'ignore/*'); const child = coffee.fork(eggBin, [ 'cov', '--c8-report-only=true' ], { cwd }) - .debug(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - } + // .debug() + .expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - assertCoverage(cwd); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); - } + 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-only=true', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - } + const child = coffee.fork(eggBin, [ 'cov', '--c8-report-only=true', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }) + .expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - assertCoverage(cwd); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); - } + 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-only=true', '-x', 'ignore2/*', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - } + const child = coffee.fork(eggBin, [ 'cov', '--c8-report-only=true', '-x', 'ignore2/*', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }) + .expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - assertCoverage(cwd); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); - } + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); }); it('should passthrough c8 args', done => { mm(process.env, 'TESTS', 'test/**/*.test.js'); - mm(process.env, 'NYC_CWD', cwd); coffee.fork(eggBin, [ 'cov', '--c8-report-only=true', '--c8=-r teamcity -r text' ], { cwd }) - .debug() + // .debug() .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/) .expect('stdout', /##teamcity\[blockClosed name='Code Coverage Summary'\]/) .end(done); diff --git a/test/lib/cmd/cov-c8-report.test.js b/test/lib/cmd/cov-c8-report.test.js index 56d90b39..a1baaa16 100644 --- a/test/lib/cmd/cov-c8-report.test.js +++ b/test/lib/cmd/cov-c8-report.test.js @@ -23,29 +23,21 @@ describe('test/lib/cmd/cov-c8-report.test.js', () => { it('should success when c8-report', function* () { mm(process.env, 'TESTS', 'test/**/*.test.js'); - mm(process.env, 'NYC_CWD', cwd); const child = coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) - // .debug() + // .debug() .expect('stdout', /should success/) .expect('stdout', /a\.test\.js/) .expect('stdout', /b[\/|\\]b\.test\.js/) - .notExpect('stdout', /a.js/); - - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements/); - } - + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements/); yield child.expect('code', 0).end(); - // only test on npm run test - console.log(process.env.NYC_ROOT_ID); - if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); + assertCoverage(cwd); }); it('should exit when not test files', done => { mm(process.env, 'NYC_CWD', cwd); coffee.fork(eggBin, [ 'cov', '--c8-report=true', 'test/**/*.nth.js' ], { cwd }) - // .debug() + // .debug() .expect('stdout', /No test files found/) .expect('code', 0) .end(done); @@ -53,97 +45,71 @@ describe('test/lib/cmd/cov-c8-report.test.js', () => { it('should hotfixSpawnWrap success on mock windows', function* () { mm(process.env, 'TESTS', 'test/**/*.test.js'); - mm(process.env, 'NYC_CWD', cwd); const child = coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) - // .debug() + // .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/); - - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); - } - + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); + 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() + // .debug() .expect('stdout', /should success/) .expect('stdout', /a\.test\.js/) .expect('stdout', /b[\/|\\]b\.test\.js/) - .notExpect('stdout', /a.js/); - - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - } - + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - assertCoverage(cwd); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); - } + 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() + // .debug() .expect('stdout', /should success/) .expect('stdout', /a\.test\.js/) .expect('stdout', /b[\/|\\]b\.test\.js/) - .notExpect('stdout', /a.js/); + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - } yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - assertCoverage(cwd); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); - } + + 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() + // .debug() .expect('stdout', /should success/) .expect('stdout', /a\.test\.js/) .expect('stdout', /b[\/|\\]b\.test\.js/) - .notExpect('stdout', /a.js/); - - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); - } - + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 100% \( 8[\/|\\]8 \)/); yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - assertCoverage(cwd); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); - } + 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() + // .debug() .expect('stdout', /1\) should fail/) .expect('stdout', /1 failing/) .expect('code', 1) @@ -153,7 +119,7 @@ describe('test/lib/cmd/cov-c8-report.test.js', () => { 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() + // .debug() .expect('stdout', /1\) should fail/) .expect('stdout', /1 failing/) .expect('stdout', /assert\(1 === 2\)/) @@ -164,7 +130,7 @@ describe('test/lib/cmd/cov-c8-report.test.js', () => { 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() + // .debug() .expect('stderr', /manually require `intelli-espower-loader`/) .expect('stdout', /1\) should fail/) .expect('stdout', /1 failing/) @@ -177,7 +143,7 @@ describe('test/lib/cmd/cov-c8-report.test.js', () => { mm(process.env, 'TESTS', 'noexist.js'); const cwd = path.join(__dirname, '../../fixtures/prerequire'); yield coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) - // .debug() + // .debug() .expect('code', 0) .end(); }); @@ -186,14 +152,14 @@ describe('test/lib/cmd/cov-c8-report.test.js', () => { mm(process.env, 'TESTS', 'test/**/*.test.js'); const cwd = path.join(__dirname, '../../fixtures/prerequire'); yield coffee.fork(eggBin, [ 'cov', '--c8-report=true' ], { cwd }) - // .debug() + // .debug() .coverage(false) .expect('stdout', /EGG_BIN_PREREQUIRE undefined/) .expect('code', 0) .end(); yield coffee.fork(eggBin, [ 'cov', '--c8-report=true', '--prerequire' ], { cwd }) - // .debug() + // .debug() .coverage(false) .expect('stdout', /EGG_BIN_PREREQUIRE true/) .expect('code', 0) @@ -204,7 +170,7 @@ describe('test/lib/cmd/cov-c8-report.test.js', () => { mm(process.env, 'TESTS', 'test/**/*.test.js'); mm(process.env, 'NYC_CWD', cwd); coffee.fork(eggBin, [ 'cov', '--c8-report=true', '--c8=-r teamcity -r text' ], { cwd }) - // .debug() + // .debug() .expect('stdout', /should success/) .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/) .expect('stdout', /##teamcity\[blockClosed name='Code Coverage Summary'\]/) From 6d606a0dd13e9926d8325b3fe57be1e4274a2ea4 Mon Sep 17 00:00:00 2001 From: onlylovermb Date: Thu, 31 Mar 2022 10:43:15 +0800 Subject: [PATCH 4/8] doc: fix readme.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 99e6d046..ba3b66b8 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,11 @@ 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-report` use c8 to report coverage not nyc, default to `false`. +- `--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-only` no run test, only use c8 to report coverage if you have run test and set node env `NODE_V8_COVERAGE`, default to `false`. - also support all test params above. From 309d246471aac992bf7bf8eec5d602c3e3ddf5c6 Mon Sep 17 00:00:00 2001 From: onlylovermb Date: Thu, 31 Mar 2022 11:03:48 +0800 Subject: [PATCH 5/8] fix: package.json --- bin/c8.js | 0 package.json | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) mode change 100644 => 100755 bin/c8.js diff --git a/bin/c8.js b/bin/c8.js old mode 100644 new mode 100755 diff --git a/package.json b/package.json index 79f76540..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", From 01a062cac3d0a3596c08139ddb7d4bf019ef1a02 Mon Sep 17 00:00:00 2001 From: onlylovermb Date: Thu, 31 Mar 2022 12:19:47 +0800 Subject: [PATCH 6/8] fix: delete c8-report-only --- README.md | 3 +- lib/cmd/cov.js | 55 ++++++------------ test/lib/cmd/cov-c8-report-only.test.js | 74 ------------------------- test/lib/cmd/cov-c8-report.test.js | 4 +- 4 files changed, 19 insertions(+), 117 deletions(-) delete mode 100644 test/lib/cmd/cov-c8-report-only.test.js diff --git a/README.md b/README.md index ba3b66b8..1b183511 100644 --- a/README.md +++ b/README.md @@ -177,11 +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-report` use c8 to report coverage not nyc, default to `false`. - `--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-only` no run test, only use c8 to report coverage if you have run test and set node env `NODE_V8_COVERAGE`, default to `false`. +- `--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/lib/cmd/cov.js b/lib/cmd/cov.js index 0e9f88bf..61fc514e 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -36,12 +36,9 @@ class CovCommand extends Command { default: '--temp-directory ./node_modules/.c8_output -r text-summary -r json-summary -r json -r lcov', }, 'c8-report': { - description: 'run test && use c8 to report coverage', - type: 'boolean', - }, - 'c8-report-only': { - description: 'no run test, only use c8 to report coverage', + description: 'use c8 to report coverage, default to false', type: 'boolean', + default: false, }, }; @@ -94,34 +91,19 @@ class CovCommand extends Command { if (context.argv.typescript) { opt.env.SPAWN_WRAP_SHIM_ROOT = path.join(cwd, 'node_modules'); } - if (argv['c8-report-only']) { - const c8Cli = require.resolve('c8/bin/c8.js'); - const covArgs = yield this.getCovArgs(context); - covArgs.unshift('report'); - yield this.helper.forkNode(c8Cli, covArgs, opt); - } else if (argv['c8-report']) { - // save coverage-xxxx.json to $PWD/coverage - const c8Cli = require.resolve('c8/bin/c8.js'); - const coverageDir = path.join(cwd, 'coverage'); - yield rimraf(coverageDir); - const outputDir = path.join(cwd, 'node_modules/.nyc_output'); - yield rimraf(outputDir); - const covArgs = yield this.getCovArgs(context); - if (!covArgs) return; - debug('covArgs: %j', covArgs); - yield this.helper.forkNode(c8Cli, covArgs, opt); - } else { - 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); - // save coverage-xxxx.json to $PWD/coverage - const covArgs = yield this.getCovArgs(context); - if (!covArgs) return; - debug('covArgs: %j', covArgs); - yield this.helper.forkNode(nycCli, covArgs, opt); + 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(cli, covArgs, opt); } /** @@ -151,7 +133,7 @@ class CovCommand extends Command { // nyc or c8 args passthrough let passthroughArgs = context.argv.nyc; - if (context.argv['c8-report'] || context.argv['c8-report-only']) { + if (context.argv['c8-report']) { passthroughArgs = context.argv.c8; context.argv['c8-report'] = undefined; } @@ -166,11 +148,8 @@ class CovCommand extends Command { } const testArgs = yield this.formatTestArgs(context); if (!testArgs) return; - if (!context.argv['c8-report-only']) { - covArgs.push(require.resolve('mocha/bin/_mocha')); - covArgs = covArgs.concat(testArgs); - } - context.argv['c8-report-only'] = undefined; + covArgs.push(require.resolve('mocha/bin/_mocha')); + covArgs = covArgs.concat(testArgs); return covArgs; } } diff --git a/test/lib/cmd/cov-c8-report-only.test.js b/test/lib/cmd/cov-c8-report-only.test.js deleted file mode 100644 index 3b9b0f9f..00000000 --- a/test/lib/cmd/cov-c8-report-only.test.js +++ /dev/null @@ -1,74 +0,0 @@ -'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-only.test.js', () => { - const eggBin = require.resolve('../../../bin/egg-bin.js'); - const cwd = path.join(__dirname, '../../fixtures/test-files-c8-report-only'); - - beforeEach(async () => { - rimraf(path.join(cwd, 'coverage')); - mm(process.env, 'TESTS', 'test/**/*.test.js'); - mm(process.env, 'NODE_V8_COVERAGE', 'node_modules/.c8_output'); - const test = coffee.fork(eggBin, [ 'test' ], { cwd }); - await test.expect('code', 0).end(); - }); - 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-only', function* () { - const child = coffee.fork(eggBin, [ 'cov', '--c8-report-only=true' ], { cwd }) - // .debug() - .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, 'COV_EXCLUDES', 'ignore/*'); - const child = coffee.fork(eggBin, [ 'cov', '--c8-report-only=true' ], { cwd }) - // .debug() - .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-only=true', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }) - .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-only=true', '-x', 'ignore2/*', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }) - .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 passthrough c8 args', done => { - mm(process.env, 'TESTS', 'test/**/*.test.js'); - coffee.fork(eggBin, [ 'cov', '--c8-report-only=true', '--c8=-r teamcity -r text' ], { cwd }) - // .debug() - .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/) - .expect('stdout', /##teamcity\[blockClosed name='Code Coverage Summary'\]/) - .end(done); - }); -}); diff --git a/test/lib/cmd/cov-c8-report.test.js b/test/lib/cmd/cov-c8-report.test.js index a1baaa16..03e56f6f 100644 --- a/test/lib/cmd/cov-c8-report.test.js +++ b/test/lib/cmd/cov-c8-report.test.js @@ -29,13 +29,12 @@ describe('test/lib/cmd/cov-c8-report.test.js', () => { .expect('stdout', /a\.test\.js/) .expect('stdout', /b[\/|\\]b\.test\.js/) .notExpect('stdout', /a.js/) - .expect('stdout', /Statements/); + .expect('stdout', /Statements {3}: 100% \( 11[\/|\\]11 \)/); yield child.expect('code', 0).end(); assertCoverage(cwd); }); it('should exit when not test files', done => { - mm(process.env, 'NYC_CWD', cwd); coffee.fork(eggBin, [ 'cov', '--c8-report=true', 'test/**/*.nth.js' ], { cwd }) // .debug() .expect('stdout', /No test files found/) @@ -168,7 +167,6 @@ describe('test/lib/cmd/cov-c8-report.test.js', () => { it('should passthrough c8 args', done => { mm(process.env, 'TESTS', 'test/**/*.test.js'); - mm(process.env, 'NYC_CWD', cwd); coffee.fork(eggBin, [ 'cov', '--c8-report=true', '--c8=-r teamcity -r text' ], { cwd }) // .debug() .expect('stdout', /should success/) From a0647adc6992ea823f8bf293e37102ff5547fe05 Mon Sep 17 00:00:00 2001 From: onlylovermb Date: Thu, 31 Mar 2022 14:23:19 +0800 Subject: [PATCH 7/8] fix: when node version < 10 skip c8 report test --- test/lib/cmd/cov-c8-report.test.js | 4 ++++ test/ts.test.js | 28 +++++++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/test/lib/cmd/cov-c8-report.test.js b/test/lib/cmd/cov-c8-report.test.js index 03e56f6f..69895b24 100644 --- a/test/lib/cmd/cov-c8-report.test.js +++ b/test/lib/cmd/cov-c8-report.test.js @@ -8,6 +8,10 @@ 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'); diff --git a/test/ts.test.js b/test/ts.test.js index 5008b0c2..5d4364ec 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -355,9 +355,21 @@ describe('test/ts.test.js', () => { fs.writeFileSync(path.resolve(cwd, './package.json'), JSON.stringify(pkgJson, null, 2)); }); + it('should not load egg-ts-helper without flag and egg.declarations', () => { + return coffee.fork(eggBin, [ 'dev' ], { cwd }) + // .debug() + .expect('stdout', /"typescript":true/) + .notExpect('stdout', /application log/) + .notExpect('stdout', /"declarations":true/) + .notExpect('stdout', /started/) + .expect('code', 1) + .end(); + }); + it('should load egg-ts-helper with dts flag', () => { + fs.mkdirSync(path.join(cwd, 'typings')); return coffee.fork(eggBin, [ 'dev', '--dts' ], { cwd }) - // .debug() + .debug() .expect('stdout', /application log/) .expect('stdout', /"typescript":true/) .expect('stdout', /started/) @@ -366,11 +378,12 @@ 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() + .debug() .expect('stdout', /application log/) .expect('stdout', /"typescript":true/) .expect('stdout', /"declarations":true/) @@ -378,16 +391,5 @@ describe('test/ts.test.js', () => { .expect('code', 0) .end(); }); - - it('should not load egg-ts-helper without flag and egg.declarations', () => { - return coffee.fork(eggBin, [ 'dev' ], { cwd }) - // .debug() - .expect('stdout', /"typescript":true/) - .notExpect('stdout', /application log/) - .notExpect('stdout', /"declarations":true/) - .notExpect('stdout', /started/) - .expect('code', 1) - .end(); - }); }); }); From 712f02565415ffd086478b743cfbaf0806d93d5b Mon Sep 17 00:00:00 2001 From: onlylovermb Date: Thu, 31 Mar 2022 14:28:44 +0800 Subject: [PATCH 8/8] fix: ts test case --- test/ts.test.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/test/ts.test.js b/test/ts.test.js index 5d4364ec..3714a859 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -355,21 +355,10 @@ describe('test/ts.test.js', () => { fs.writeFileSync(path.resolve(cwd, './package.json'), JSON.stringify(pkgJson, null, 2)); }); - it('should not load egg-ts-helper without flag and egg.declarations', () => { - return coffee.fork(eggBin, [ 'dev' ], { cwd }) - // .debug() - .expect('stdout', /"typescript":true/) - .notExpect('stdout', /application log/) - .notExpect('stdout', /"declarations":true/) - .notExpect('stdout', /started/) - .expect('code', 1) - .end(); - }); - it('should load egg-ts-helper with dts flag', () => { fs.mkdirSync(path.join(cwd, 'typings')); return coffee.fork(eggBin, [ 'dev', '--dts' ], { cwd }) - .debug() + // .debug() .expect('stdout', /application log/) .expect('stdout', /"typescript":true/) .expect('stdout', /started/) @@ -381,9 +370,8 @@ describe('test/ts.test.js', () => { 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() + // .debug() .expect('stdout', /application log/) .expect('stdout', /"typescript":true/) .expect('stdout', /"declarations":true/) @@ -391,5 +379,16 @@ describe('test/ts.test.js', () => { .expect('code', 0) .end(); }); + + it('should not load egg-ts-helper without flag and egg.declarations', () => { + return coffee.fork(eggBin, [ 'dev' ], { cwd }) + // .debug() + .expect('stdout', /"typescript":true/) + .notExpect('stdout', /application log/) + .notExpect('stdout', /"declarations":true/) + .notExpect('stdout', /started/) + .expect('code', 1) + .end(); + }); }); });