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: add support for v8 code coverage #8596

Merged
merged 26 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
testTimeout: 5000,
updateSnapshot: 'none',
useStderr: false,
v8Coverage: false,
verbose: false,
watch: false,
watchAll: false,
Expand Down
15 changes: 15 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ 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.

Cannot be used together with `--v8-coverage`.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

### `--debug`

Print debugging info about your Jest config.
Expand Down Expand Up @@ -317,6 +319,19 @@ Divert all output to stderr.

Display individual test results with the test suite hierarchy.

### `--v8-coverage[=<boolean>]`

> _Experimental_

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

This uses V8's builtin code coverage rather than one based on Babel. Note that there are 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)

Cannot be used together with `--coverage`.

### `--version`

Alias: `-v`. Print the version and exit.
Expand Down
15 changes: 15 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ Default: `false`

Indicates whether the coverage information should be collected while executing the test. Because this retrofits all executed files with coverage collection statements, it may significantly slow down your tests.

Cannot be used together with `v8Coverage`.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

### `collectCoverageFrom` [array]

Default: `undefined`
Expand Down Expand Up @@ -1121,6 +1123,19 @@ This is useful for some commonly used 'utility' modules that are almost always u

It is possible to override this setting in individual tests by explicitly calling `jest.mock()` at the top of the test file.

### `v8Coverage` [boolean]

> _Experimental_

Indicates whether the coverage information should be collected while executing the test.

This uses V8's builtin code coverage rather than one based on Babel. Note that there are 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)

Cannot be used together with `collectCoverage`.

### `verbose` [boolean]

Default: `false`
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 @@ -118,6 +118,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"testSequencer": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-test-sequencer/build/index.js",
"updateSnapshot": "all",
"useStderr": false,
"v8Coverage": false,
"watch": false,
"watchAll": false,
"watchman": true
Expand Down
13 changes: 13 additions & 0 deletions e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints coverage 1`] = `
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 |
----------|---------|----------|---------|---------|-------------------
`;
25 changes: 25 additions & 0 deletions e2e/__tests__/v8Coverage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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 {wrap} from 'jest-snapshot-serializer-raw';
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, ['--v8-coverage'], {
stripAnsi: true,
});

expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});
});
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 = {};',
};
12 changes: 12 additions & 0 deletions e2e/v8-coverage/no-sourcemap/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "no-sourcemap",
"version": "1.0.0",
"jest": {
"collectCoverageFrom": ["Thing.js"],
"testEnvironment": "node",
"transform": {
"^.+\\.[jt]sx?$": "babel-jest",
"^.+\\.css$": "<rootDir>/cssTransform.js"
}
}
}
Empty file.
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
7 changes: 7 additions & 0 deletions packages/jest-cli/src/__tests__/cli/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ describe('check', () => {
'The --config option requires a JSON string literal, or a file path with a .js or .json extension',
);
});

it('raises an exception if both coverage and v8-coverage is specified', () => {
const argv = {collectCoverage: true, v8Coverage: true} as Config.Argv;
expect(() => check(argv)).toThrow(
'Both --coverage and --v8Coverage were specified, but these two options do not make sense together. Which is it?',
);
});
});

describe('buildArgv', () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/jest-cli/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export const check = (argv: Config.Argv) => {
);
}

if (argv.collectCoverage && argv.v8Coverage) {
throw new Error(
'Both --coverage and --v8Coverage were specified, but these two ' +
'options do not make sense together. Which is it?',
);
}

for (const key of [
'onlyChanged',
'lastCommit',
Expand Down Expand Up @@ -675,6 +682,11 @@ export const options = {
description: 'Divert all output to stderr.',
type: 'boolean',
},
v8Coverage: {
default: false,
description: 'Collect coverage using V8 instrumentation',
type: 'boolean' as 'boolean',
},
verbose: {
default: undefined,
description:
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 @@ -65,6 +65,7 @@ const defaultOptions: Config.DefaultOptions = {
timers: 'real',
transformIgnorePatterns: [NODE_MODULES_REGEXP],
useStderr: false,
v8Coverage: false,
watch: false,
watchPathIgnorePatterns: [],
watchman: true,
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 @@ -124,6 +124,7 @@ const initialOptions: Config.InitialOptions = {
unmockedModulePathPatterns: ['mock'],
updateSnapshot: true,
useStderr: false,
v8Coverage: false,
verbose: false,
watch: false,
watchPathIgnorePatterns: ['<rootDir>/e2e/'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ exports[`setupTestFrameworkScriptFile logs a deprecation warning when \`setupTes
<yellow></>"
`;

exports[`setupTestFrameworkScriptFile logs an error when \`collectCoverage\` and \`v8COverage\` are used 1`] = `
"<red><bold><bold>● </><bold>Validation Error</>:</>
<red></>
<red> Configuration options <bold>collectCoverage</> and <bold>v8Coverage</> cannot be used together.</>
<red></>
<red> <bold>Configuration Documentation:</></>
<red> https://jestjs.io/docs/configuration.html</>
<red></>"
`;

exports[`setupTestFrameworkScriptFile logs an error when \`setupTestFrameworkScriptFile\` and \`setupFilesAfterEnv\` are used 1`] = `
"<red><bold><bold>● </><bold>Validation Error</>:</>
<red></>
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,19 @@ describe('setupTestFrameworkScriptFile', () => {
),
).toThrowErrorMatchingSnapshot();
});

it('logs an error when `collectCoverage` and `v8COverage` are used', () => {
expect(() =>
normalize(
{
collectCoverage: true,
rootDir: '/root/path/foo',
v8Coverage: true,
},
{},
),
).toThrowErrorMatchingSnapshot();
});
});

describe('coveragePathIgnorePatterns', () => {
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 @@ -152,6 +152,7 @@ const groupOptions = (
testTimeout: options.testTimeout,
updateSnapshot: options.updateSnapshot,
useStderr: options.useStderr,
v8Coverage: options.v8Coverage,
verbose: options.verbose,
watch: options.watch,
watchAll: options.watchAll,
Expand Down
13 changes: 12 additions & 1 deletion packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ export default function normalize(
case 'timers':
case 'useStderr':
case 'verbose':
case 'v8Coverage':
case 'watch':
case 'watchAll':
case 'watchman':
Expand Down Expand Up @@ -1009,7 +1010,10 @@ export default function normalize(
// Is transformed to: `--findRelatedTests '/rootDir/file1.js' --coverage --collectCoverageFrom 'file1.js'`
// where arguments to `--collectCoverageFrom` should be globs (or relative
// paths to the rootDir)
if (newOptions.collectCoverage && argv.findRelatedTests) {
if (
(newOptions.collectCoverage || newOptions.v8Coverage) &&
argv.findRelatedTests
) {
let collectCoverageFrom = argv._.map(filename => {
filename = replaceRootDirInPath(options.rootDir, filename);
return path.isAbsolute(filename)
Expand Down Expand Up @@ -1057,6 +1061,13 @@ export default function normalize(
newOptions.logHeapUsage = false;
}

if (newOptions.collectCoverage && newOptions.v8Coverage) {
throw createConfigError(
` Configuration options ${chalk.bold('collectCoverage')} and` +
` ${chalk.bold('v8Coverage')} cannot be used together.`,
SimenB marked this conversation as resolved.
Show resolved Hide resolved
);
}

return {
hasDeprecationWarnings,
options: newOptions,
Expand Down
7 changes: 4 additions & 3 deletions packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,15 @@ export default class TestScheduler {
}

private _setupReporters() {
const {collectCoverage, notify, reporters} = this._globalConfig;
const {collectCoverage, notify, reporters, v8Coverage} = this._globalConfig;
const isDefault = this._shouldAddDefaultReporters(reporters);
const willCollectCoverage = collectCoverage || v8Coverage;

if (isDefault) {
this._setupDefaultReporters(collectCoverage);
this._setupDefaultReporters(willCollectCoverage);
}

if (!isDefault && collectCoverage) {
if (!isDefault && willCollectCoverage) {
this.addReporter(
new CoverageReporter(this._globalConfig, {
changedFiles: this._context && this._context.changedFiles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ exports[`prints the config object 1`] = `
"testTimeout": 5000,
"updateSnapshot": "none",
"useStderr": false,
"v8Coverage": false,
"verbose": false,
"watch": true,
"watchAll": false,
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-coverage/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
22 changes: 22 additions & 0 deletions packages/jest-coverage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@jest/coverage",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this module does not (and should not) have to live here. Maybe we can make it part of istanbul-lib-instrument? All it does is start and stop the coverage collection using the v8 inspector API.

/cc @bcoe @coreyfarrell

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have time to really look at this currently but I wouldn't object to creating a new module under the istanbuljs umbrella to accomplish this. Maybe @istanbuljs/v8-coverage would be a good name? I think it should be a separate module from istanbul-lib-instrument which pulls in a bunch of babel modules.

FWIW under the istanbuljs org I would not want TS source code but I don't have a problem with having the module include an index.d.ts with public definitions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I honestly have less concerns about TypeScript, since I've been using it at work these days; would be happy to put up my hand and help take on maintenance burden for this one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to take the code from this. Or create the repo and I can push it there.

If you want a separate d.ts. file, here's the built definition:

/// <reference types="node" />
import { Profiler } from 'inspector';
export declare type V8Coverage = ReadonlyArray<Profiler.ScriptCoverage>;
export default class CoverageInstrumenter {
    private readonly session;
    startInstrumenting(): Promise<void>;
    stopInstrumenting(): Promise<V8Coverage>;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"version": "24.9.0",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git",
"directory": "packages/jest-coverage"
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"devDependencies": {
"@types/node": "*"
},
"engines": {
"node": ">= 8"
},
"publishConfig": {
"access": "public"
},
"gitHead": "0efb1d7809cb96ae87a7601e7802f1dab3774280"
}