Skip to content

Commit 89c151b

Browse files
authored
feat: add support for v8 code coverage (#8596)
1 parent 4f67bef commit 89c151b

File tree

34 files changed

+436
-51
lines changed

34 files changed

+436
-51
lines changed

TestUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
1616
collectCoverageFrom: [],
1717
collectCoverageOnlyFrom: null,
1818
coverageDirectory: 'coverage',
19+
coverageProvider: 'babel',
1920
coverageReporters: [],
2021
coverageThreshold: {global: {}},
2122
detectLeaks: false,

docs/CLI.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ Alias: `-c`. The path to a Jest config file specifying how to find and execute t
152152

153153
Alias: `--collectCoverage`. Indicates that test coverage information should be collected and reported in the output. Optionally pass `<boolean>` to override option set in configuration.
154154

155+
### `--coverageProvider=<provider>`
156+
157+
Indicates which provider should be used to instrument code for coverage. Allowed values are `babel` (default) or `v8`.
158+
159+
Note that using `v8` is considered experimental. This uses V8's builtin code coverage rather than one based on Babel and comes with a few caveats
160+
161+
1. Your node version must include `vm.compileFunction`, which was introduced in [node 10.10](https://nodejs.org/dist/latest-v12.x/docs/api/vm.html#vm_vm_compilefunction_code_params_options)
162+
1. Tests needs to run in Node test environment (support for `jsdom` is in the works, see [#9315](https://github.com/facebook/jest/issues/9315))
163+
1. V8 has way better data in the later versions, so using the latest versions of node (v13 at the time of this writing) will yield better results
164+
155165
### `--debug`
156166

157167
Print debugging info about your Jest config.

docs/Configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ An array of regexp pattern strings that are matched against all file paths befor
185185

186186
These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories. Example: `["<rootDir>/build/", "<rootDir>/node_modules/"]`.
187187

188+
### `coverageProvider` [string]
189+
190+
Indicates which provider should be used to instrument code for coverage. Allowed values are `babel` (default) or `v8`.
191+
192+
Note that using `v8` is considered experimental. This uses V8's builtin code coverage rather than one based on Babel and comes with a few caveats
193+
194+
1. Your node version must include `vm.compileFunction`, which was introduced in [node 10.10](https://nodejs.org/dist/latest-v12.x/docs/api/vm.html#vm_vm_compilefunction_code_params_options)
195+
1. Tests needs to run in Node test environment (support for `jsdom` is in the works, see [#9315](https://github.com/facebook/jest/issues/9315))
196+
1. V8 has way better data in the later versions, so using the latest versions of node (v13 at the time of this writing) will yield better results
197+
188198
### `coverageReporters` [array\<string>]
189199

190200
Default: `["json", "lcov", "text", "clover"]`

e2e/__tests__/__snapshots__/showConfig.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
8484
"collectCoverage": false,
8585
"collectCoverageFrom": [],
8686
"coverageDirectory": "<<REPLACED_ROOT_DIR>>/coverage",
87+
"coverageProvider": "babel",
8788
"coverageReporters": [
8889
"json",
8990
"text",

e2e/__tests__/v8Coverage.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as path from 'path';
9+
import {onNodeVersions} from '@jest/test-utils';
10+
import runJest from '../runJest';
11+
12+
const DIR = path.resolve(__dirname, '../v8-coverage');
13+
14+
onNodeVersions('>=10', () => {
15+
test('prints coverage', () => {
16+
const sourcemapDir = path.join(DIR, 'no-sourcemap');
17+
const {stdout, exitCode} = runJest(
18+
sourcemapDir,
19+
['--coverage', '--coverage-provider', 'v8'],
20+
{
21+
stripAnsi: true,
22+
},
23+
);
24+
25+
expect(exitCode).toBe(0);
26+
expect(
27+
'\n' +
28+
stdout
29+
.split('\n')
30+
.map(s => s.trimRight())
31+
.join('\n') +
32+
'\n',
33+
).toEqual(`
34+
console.log __tests__/Thing.test.js:10
35+
42
36+
37+
----------|---------|----------|---------|---------|-------------------
38+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
39+
----------|---------|----------|---------|---------|-------------------
40+
All files | 100 | 100 | 100 | 100 |
41+
Thing.js | 100 | 100 | 100 | 100 |
42+
x.css | 100 | 100 | 100 | 100 |
43+
----------|---------|----------|---------|---------|-------------------
44+
`);
45+
});
46+
});

e2e/v8-coverage/no-sourcemap/Thing.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
require('./x.css');
9+
10+
module.exports = 42;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
const Thing = require('../Thing');
9+
10+
console.log(Thing);
11+
test.todo('whatever');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = {
9+
getCacheKey: () => 'cssTransform',
10+
process: () => 'module.exports = {};',
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "no-sourcemap",
3+
"version": "1.0.0",
4+
"jest": {
5+
"testEnvironment": "node",
6+
"transform": {
7+
"^.+\\.[jt]sx?$": "babel-jest",
8+
"^.+\\.css$": "<rootDir>/cssTransform.js"
9+
}
10+
}
11+
}

e2e/v8-coverage/no-sourcemap/x.css

Whitespace-only changes.

0 commit comments

Comments
 (0)