Skip to content

Commit

Permalink
add mapCoverage to CLI, switch off by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Feb 13, 2017
1 parent ff8941a commit 03086ac
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 21 deletions.
4 changes: 3 additions & 1 deletion dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const raiseIssueAboutPaths = (
};

const newJsFiles = danger.git.created_files.filter(path => path.endsWith('js'));
const isSourceFile = path => includes(path, '/src/');
const isSourceFile = path =>
includes(path, '/src/') &&
!includes(path, '__tests__');

// New JS files should have the FB copyright header + flow
const facebookLicenseHeaderComponents = [
Expand Down
20 changes: 12 additions & 8 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,18 @@ Note that, if you specify a global reference value (like an object or array) her

##### available in Jest **19.0.0+**

Default: `true`

If you have [transformers](#transform-object-string-string) configured that emit
source maps, Jest will use them to map code coverage against the original source
code when writing [reports](#coveragereporters-array-string) and checking
[thresholds](#coveragethreshold-object). This can be resource-intensive. If Jest
consumes large amounts of memory while calculating coverage, try setting this
option to `false`.
Default: `false`

If you have [transformers](#transform-object-string-string) configured that emit source maps, Jest will use them to map code coverage against the original source code when writing [reports](#coveragereporters-array-string) and checking [thresholds](#coveragethreshold-object). This can be resource-intensive. If Jest is taking a long time to calculate coverage at the end of a test run, try setting this option to `false`.

Both inline source maps and source maps returned directly from a transformer are supported. Source map URLs are not supported because Jest may not be able to locate them. To return source maps from a transformer, the `process` function can return an object like the following. The sourceMap property may either be an object, or a string of JSON.

```js
return {
content: 'the code',
sourceMap: 'the source map',
};
```

### `moduleFileExtensions` [array<string>]
Default: `["js", "json", "jsx", "node"]`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports[`test maps code coverage against original source 1`] = `
exports[`maps code coverage against original source 1`] = `
Object {
"covered.ts": Object {
"b": Object {
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/__tests__/coverage-remapping-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ skipOnWindows.suite();
it('maps code coverage against original source', () => {
const dir = path.resolve(__dirname, '../coverage-remapping');
run('npm install', dir);
runJest(dir, ['--coverage', '--no-cache']);
runJest(dir, ['--coverage', '--mapCoverage', '--no-cache']);

const coverageMapFile = path.join(
__dirname,
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ const options = {
'leaks. Use together with `--runInBand` and `--expose-gc` in node.',
type: 'boolean',
},
mapCoverage: {
description:
'Maps code coverage reports against original source code when ' +
'transformers supply source maps.',
type: 'boolean',
},
maxWorkers: {
alias: 'w',
description:
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = ({
haste: {
providesModuleNodeModules: [],
},
mapCoverage: true,
mapCoverage: false,
moduleDirectories: ['node_modules'],
moduleFileExtensions: [
'js',
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-config/src/setFromArgv.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function setFromArgv(config, argv) {
config.collectCoverage = true;
}

if (argv.mapCoverage) {
config.mapCoverage = true;
}

if (argv.verbose) {
config.verbose = argv.verbose;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/validConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = ({
},
logHeapUsage: true,
logTransformErrors: true,
mapCoverage: true,
mapCoverage: false,
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
moduleLoader: '<rootDir>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ exports[`transform does not instrument with source map if mapCoverage config opt
`;

exports[`transform instruments with source map if preprocessor inlines it 1`] = `
"({\\"Object.<anonymous>\\":function(module,exports,require,__dirname,__filename,global,jest){/* istanbul ignore next */var cov_25u22311x4 = function () {var path = \\"/fruits/banana.js\\",hash = \\"5dc8e485e00fed337d9101d8fac0e5dd5a88b834\\",global = new Function('return this')(),gcv = \\"__coverage__\\",coverageData = { path: \\"/fruits/banana.js\\", statementMap: { \\"0\\": { start: { line: 1, column: 12 }, end: { line: 1, column: 24 } } }, fnMap: {}, branchMap: { \\"0\\": { loc: { start: { line: 1, column: 12 }, end: { line: 1, column: 24 } }, type: \\"cond-expr\\", locations: [{ start: { line: 1, column: 19 }, end: { line: 1, column: 20 } }, { start: { line: 1, column: 23 }, end: { line: 1, column: 24 } }] } }, s: { \\"0\\": 0 }, f: {}, b: { \\"0\\": [0, 0] }, _coverageSchema: \\"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c\\" },coverage = global[gcv] || (global[gcv] = {});if (coverage[path] && coverage[path].hash === hash) {return coverage[path];}coverageData.hash = hash;return coverage[path] = coverageData;}();var value = /* istanbul ignore next */(++cov_25u22311x4.s[0], true ? /* istanbul ignore next */(++cov_25u22311x4.b[0][0], 1) : /* istanbul ignore next */(++cov_25u22311x4.b[0][1], 0));
"({\\"Object.<anonymous>\\":function(module,exports,require,__dirname,__filename,global,jest){/**
* Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict'; /* istanbul ignore next */var cov_25u22311x4 = function () {var path = '/fruits/banana.js',hash = '0f286c0a1633c0df1abca7c94242aff8823cbbc3',global = new Function('return this')(),gcv = '__coverage__',coverageData = { path: '/fruits/banana.js', statementMap: { '0': { start: { line: 9, column: 12 }, end: { line: 9, column: 24 } } }, fnMap: {}, branchMap: { '0': { loc: { start: { line: 9, column: 12 }, end: { line: 9, column: 24 } }, type: 'cond-expr', locations: [{ start: { line: 9, column: 19 }, end: { line: 9, column: 20 } }, { start: { line: 9, column: 23 }, end: { line: 9, column: 24 } }] } }, s: { '0': 0 }, f: {}, b: { '0': [0, 0] }, _coverageSchema: '332fd63041d2c1bcb487cc26dd0d5f7d97098a6c' },coverage = global[gcv] || (global[gcv] = {});if (coverage[path] && coverage[path].hash === hash) {return coverage[path];}coverageData.hash = hash;return coverage[path] = coverageData;}();
var value = /* istanbul ignore next */(++cov_25u22311x4.s[0], true ? /* istanbul ignore next */(++cov_25u22311x4.b[0][0], 1) : /* istanbul ignore next */(++cov_25u22311x4.b[0][1], 0));
;global.__coverage__['/fruits/banana.js'].inputSourceMapPath = '/cache/jest-transform-cache-test/ab/banana_ab.map';
}});"
`;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions packages/jest-runtime/src/__tests__/transform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ jest.mock(
jest.mock(
'preprocessor-with-sourcemaps',
() => {
const getProcessResult = jest.fn();
return {
getCacheKey: jest.fn((content, filename, configStr) => 'ab'),
getProcessResult,
process: (content, filename, config) => getProcessResult(),
process: jest.fn(),
};
},
{virtual: true},
Expand Down Expand Up @@ -230,6 +228,10 @@ describe('transform', () => {
});

it('instruments with source map if preprocessor supplies it', () => {
if (skipOnWindows.test()) { //snapshot has os-dependent path separators
return;
}

config = Object.assign(config, {
collectCoverage: true,
mapCoverage: true,
Expand All @@ -241,7 +243,7 @@ describe('transform', () => {
version: 3,
};

require('preprocessor-with-sourcemaps').getProcessResult.mockReturnValue({
require('preprocessor-with-sourcemaps').process.mockReturnValue({
content: 'content',
sourceMap,
});
Expand All @@ -256,6 +258,10 @@ describe('transform', () => {
});

it('instruments with source map if preprocessor inlines it', () => {
if (skipOnWindows.test()) { //snapshot has os-dependent path separators
return;
}

const realFS = require('fs');

config = Object.assign(config, {
Expand All @@ -264,7 +270,7 @@ describe('transform', () => {
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
});

require('preprocessor-with-sourcemaps').getProcessResult.mockReturnValue({
require('preprocessor-with-sourcemaps').process.mockReturnValue({
content: realFS.readFileSync(
path.resolve(__dirname, './test_root/hasInlineSourceMap.js'),
'utf8'
Expand Down Expand Up @@ -292,7 +298,7 @@ describe('transform', () => {
version: 3,
};

require('preprocessor-with-sourcemaps').getProcessResult.mockReturnValue({
require('preprocessor-with-sourcemaps').process.mockReturnValue({
content: 'content',
sourceMap,
});
Expand Down

0 comments on commit 03086ac

Please sign in to comment.