Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: support c8 report #172

Merged
merged 8 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Expand Down
5 changes: 5 additions & 0 deletions bin/c8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

'use strict';

require('c8/bin/c8');
46 changes: 30 additions & 16 deletions lib/cmd/cov.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,12 +78,6 @@ class CovCommand extends Command {
this.addExclude(exclude);
}

const nycCli = require.resolve('nyc/bin/nyc.js');
mansonchor marked this conversation as resolved.
Show resolved Hide resolved
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,
Expand All @@ -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);
}

/**
Expand All @@ -102,7 +113,6 @@ class CovCommand extends Command {
addExclude(exclude) {
this[EXCLUDES].add(exclude);
}

/**
* get coverage args
* @param {Object} context - { cwd, argv, ...}
Expand All @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/app/assets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/app/public/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/config/config.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/config/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/docs/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/example/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/examples/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/ignore/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
8 changes: 8 additions & 0 deletions test/fixtures/test-files-c8-report-only/lib/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = condition => {
if (condition) {
return 'a';
}
return 'b';
};
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/mocks/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/mocks_data/foo/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
6 changes: 6 additions & 0 deletions test/fixtures/test-files-c8-report-only/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-files",
"files": [
"lib"
]
}
9 changes: 9 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const a = require('../lib/a');

describe('a.js', () => {
it('should success', () => {
a(true);
});
});
15 changes: 15 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/a.test.js
Original file line number Diff line number Diff line change
@@ -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));
});
});
5 changes: 5 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/b/b.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

describe('b/b.test.js', () => {
it('should success', () => {});
});
7 changes: 7 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

describe('fail.js', () => {
it('should fail', () => {
throw new Error('fail.js throw');
});
});
10 changes: 10 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/ignore.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const assert = require('assert');
const a = require('../ignore/a');

describe('ignore.test.js', () => {
it('should success', () => {
assert(a === '');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

describe('no-timeouts.test.js', () => {
it('should success', function() {
console.log(`timeout: ${this.timeout()}`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const assert = require('power-assert');

describe('power-assert-fail.js', () => {
it('should fail', () => {
assert(1 === 2);
});
});
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/assets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/assets/subdir/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/public/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/public/subdir/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/view/subdir/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/config/config.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/config/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/docs/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/example/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/examples/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/ignore/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
8 changes: 8 additions & 0 deletions test/fixtures/test-files-c8/lib/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = condition => {
if (condition) {
return 'a';
}
return 'b';
};
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/mocks/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/mocks_data/foo/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
6 changes: 6 additions & 0 deletions test/fixtures/test-files-c8/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-files",
"files": [
"lib"
]
}
9 changes: 9 additions & 0 deletions test/fixtures/test-files-c8/test/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const a = require('../lib/a');

describe('a.js', () => {
it('should success', () => {
a(true);
});
});
15 changes: 15 additions & 0 deletions test/fixtures/test-files-c8/test/a.test.js
Original file line number Diff line number Diff line change
@@ -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));
});
});
5 changes: 5 additions & 0 deletions test/fixtures/test-files-c8/test/b/b.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

describe('b/b.test.js', () => {
it('should success', () => {});
});
7 changes: 7 additions & 0 deletions test/fixtures/test-files-c8/test/fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

describe('fail.js', () => {
it('should fail', () => {
throw new Error('fail.js throw');
});
});
Loading