Skip to content

Commit

Permalink
feat: add support for v8 code coverage (#8596)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 17, 2019
1 parent 4f67bef commit 89c151b
Show file tree
Hide file tree
Showing 34 changed files with 436 additions and 51 deletions.
1 change: 1 addition & 0 deletions TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
collectCoverageFrom: [],
collectCoverageOnlyFrom: null,
coverageDirectory: 'coverage',
coverageProvider: 'babel',
coverageReporters: [],
coverageThreshold: {global: {}},
detectLeaks: false,
Expand Down
10 changes: 10 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ Alias: `-c`. The path to a Jest config file specifying how to find and execute t

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

### `--coverageProvider=<provider>`

Indicates which provider should be used to instrument code for coverage. Allowed values are `babel` (default) or `v8`.

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

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)
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))
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

### `--debug`

Print debugging info about your Jest config.
Expand Down
10 changes: 10 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ An array of regexp pattern strings that are matched against all file paths befor

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/"]`.

### `coverageProvider` [string]

Indicates which provider should be used to instrument code for coverage. Allowed values are `babel` (default) or `v8`.

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

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)
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))
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

### `coverageReporters` [array\<string>]

Default: `["json", "lcov", "text", "clover"]`
Expand Down
1 change: 1 addition & 0 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"collectCoverage": false,
"collectCoverageFrom": [],
"coverageDirectory": "<<REPLACED_ROOT_DIR>>/coverage",
"coverageProvider": "babel",
"coverageReporters": [
"json",
"text",
Expand Down
46 changes: 46 additions & 0 deletions e2e/__tests__/v8Coverage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import * as path from 'path';
import {onNodeVersions} from '@jest/test-utils';
import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../v8-coverage');

onNodeVersions('>=10', () => {
test('prints coverage', () => {
const sourcemapDir = path.join(DIR, 'no-sourcemap');
const {stdout, exitCode} = runJest(
sourcemapDir,
['--coverage', '--coverage-provider', 'v8'],
{
stripAnsi: true,
},
);

expect(exitCode).toBe(0);
expect(
'\n' +
stdout
.split('\n')
.map(s => s.trimRight())
.join('\n') +
'\n',
).toEqual(`
console.log __tests__/Thing.test.js:10
42
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
Thing.js | 100 | 100 | 100 | 100 |
x.css | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
`);
});
});
10 changes: 10 additions & 0 deletions e2e/v8-coverage/no-sourcemap/Thing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

require('./x.css');

module.exports = 42;
11 changes: 11 additions & 0 deletions e2e/v8-coverage/no-sourcemap/__tests__/Thing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const Thing = require('../Thing');

console.log(Thing);
test.todo('whatever');
11 changes: 11 additions & 0 deletions e2e/v8-coverage/no-sourcemap/cssTransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
getCacheKey: () => 'cssTransform',
process: () => 'module.exports = {};',
};
11 changes: 11 additions & 0 deletions e2e/v8-coverage/no-sourcemap/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "no-sourcemap",
"version": "1.0.0",
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.[jt]sx?$": "babel-jest",
"^.+\\.css$": "<rootDir>/cssTransform.js"
}
}
}
Empty file.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = {
transform: {
'^.+\\.[jt]sx?$': '<rootDir>/packages/babel-jest',
},
watchPathIgnorePatterns: ['coverage'],
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"isbinaryfile": "^4.0.0",
"istanbul-lib-coverage": "^3.0.0-alpha.1",
"istanbul-lib-report": "^3.0.0-alpha.1",
"istanbul-reports": "^3.0.0-alpha.4",
"istanbul-reports": "^3.0.0-alpha.5",
"jest-junit": "^9.0.0",
"jest-silent-reporter": "^0.1.2",
"jest-snapshot-serializer-raw": "^1.1.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-cli/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ export const options = {
string: true,
type: 'array',
},
coverageProvider: {
choices: ['babel', 'v8'],
default: 'babel',
description: 'Select between Babel and V8 to collect coverage',
},
coverageReporters: {
description:
'A list of reporter names that Jest uses when writing ' +
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/Defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const defaultOptions: Config.DefaultOptions = {
clearMocks: false,
collectCoverage: false,
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
coverageProvider: 'babel',
coverageReporters: ['json', 'text', 'lcov', 'clover'],
errorOnDeprecated: false,
expand: false,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/ValidConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const initialOptions: Config.InitialOptions = {
},
coverageDirectory: 'coverage',
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
coverageProvider: 'v8',
coverageReporters: ['json', 'text', 'lcov', 'clover'],
coverageThreshold: {
global: {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const groupOptions = (
collectCoverageFrom: options.collectCoverageFrom,
collectCoverageOnlyFrom: options.collectCoverageOnlyFrom,
coverageDirectory: options.coverageDirectory,
coverageProvider: options.coverageProvider,
coverageReporters: options.coverageReporters,
coverageThreshold: options.coverageThreshold,
detectLeaks: options.detectLeaks,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ export default function normalize(
case 'changedFilesWithAncestor':
case 'clearMocks':
case 'collectCoverage':
case 'coverageProvider':
case 'coverageReporters':
case 'coverageThreshold':
case 'detectLeaks':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ exports[`prints the config object 1`] = `
"collectCoverageFrom": [],
"collectCoverageOnlyFrom": null,
"coverageDirectory": "coverage",
"coverageProvider": "babel",
"coverageReporters": [],
"coverageThreshold": {
"global": {}
Expand Down
7 changes: 5 additions & 2 deletions packages/jest-reporters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@bcoe/v8-coverage": "^0.2.3",
"@jest/console": "^24.9.0",
"@jest/environment": "^24.9.0",
"@jest/test-result": "^24.9.0",
"@jest/transform": "^24.9.0",
"@jest/types": "^24.9.0",
"chalk": "^3.0.0",
"collect-v8-coverage": "^1.0.0",
"exit": "^0.1.2",
"glob": "^7.1.2",
"istanbul-lib-coverage": "^3.0.0-alpha.1",
"istanbul-lib-instrument": "^4.0.0-alpha.2",
"istanbul-lib-report": "^3.0.0-alpha.1",
"istanbul-lib-source-maps": "^4.0.0-alpha.4",
"istanbul-reports": "^3.0.0-alpha.4",
"istanbul-reports": "^3.0.0-alpha.5",
"jest-haste-map": "^24.9.0",
"jest-resolve": "^24.9.0",
"jest-runtime": "^24.9.0",
Expand All @@ -26,7 +28,8 @@
"slash": "^3.0.0",
"source-map": "^0.6.0",
"string-length": "^3.1.0",
"terminal-link": "^2.0.0"
"terminal-link": "^2.0.0",
"v8-to-istanbul": "^4.0.1"
},
"devDependencies": {
"@types/exit": "^0.1.30",
Expand Down
Loading

0 comments on commit 89c151b

Please sign in to comment.