Skip to content

Commit 03086ac

Browse files
committed
add mapCoverage to CLI, switch off by default
1 parent ff8941a commit 03086ac

File tree

11 files changed

+59
-21
lines changed

11 files changed

+59
-21
lines changed

dangerfile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ const raiseIssueAboutPaths = (
5151
};
5252

5353
const newJsFiles = danger.git.created_files.filter(path => path.endsWith('js'));
54-
const isSourceFile = path => includes(path, '/src/');
54+
const isSourceFile = path =>
55+
includes(path, '/src/') &&
56+
!includes(path, '__tests__');
5557

5658
// New JS files should have the FB copyright header + flow
5759
const facebookLicenseHeaderComponents = [

docs/Configuration.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,18 @@ Note that, if you specify a global reference value (like an object or array) her
154154

155155
##### available in Jest **19.0.0+**
156156

157-
Default: `true`
158-
159-
If you have [transformers](#transform-object-string-string) configured that emit
160-
source maps, Jest will use them to map code coverage against the original source
161-
code when writing [reports](#coveragereporters-array-string) and checking
162-
[thresholds](#coveragethreshold-object). This can be resource-intensive. If Jest
163-
consumes large amounts of memory while calculating coverage, try setting this
164-
option to `false`.
157+
Default: `false`
158+
159+
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`.
160+
161+
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.
162+
163+
```js
164+
return {
165+
content: 'the code',
166+
sourceMap: 'the source map',
167+
};
168+
```
165169

166170
### `moduleFileExtensions` [array<string>]
167171
Default: `["js", "json", "jsx", "node"]`

integration_tests/__tests__/__snapshots__/coverage-remapping-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exports[`test maps code coverage against original source 1`] = `
1+
exports[`maps code coverage against original source 1`] = `
22
Object {
33
"covered.ts": Object {
44
"b": Object {

integration_tests/__tests__/coverage-remapping-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ skipOnWindows.suite();
1919
it('maps code coverage against original source', () => {
2020
const dir = path.resolve(__dirname, '../coverage-remapping');
2121
run('npm install', dir);
22-
runJest(dir, ['--coverage', '--no-cache']);
22+
runJest(dir, ['--coverage', '--mapCoverage', '--no-cache']);
2323

2424
const coverageMapFile = path.join(
2525
__dirname,

packages/jest-cli/src/cli/args.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ const options = {
147147
'leaks. Use together with `--runInBand` and `--expose-gc` in node.',
148148
type: 'boolean',
149149
},
150+
mapCoverage: {
151+
description:
152+
'Maps code coverage reports against original source code when ' +
153+
'transformers supply source maps.',
154+
type: 'boolean',
155+
},
150156
maxWorkers: {
151157
alias: 'w',
152158
description:

packages/jest-config/src/defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = ({
3232
haste: {
3333
providesModuleNodeModules: [],
3434
},
35-
mapCoverage: true,
35+
mapCoverage: false,
3636
moduleDirectories: ['node_modules'],
3737
moduleFileExtensions: [
3838
'js',

packages/jest-config/src/setFromArgv.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function setFromArgv(config, argv) {
1313
config.collectCoverage = true;
1414
}
1515

16+
if (argv.mapCoverage) {
17+
config.mapCoverage = true;
18+
}
19+
1620
if (argv.verbose) {
1721
config.verbose = argv.verbose;
1822
}

packages/jest-config/src/validConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = ({
4545
},
4646
logHeapUsage: true,
4747
logTransformErrors: true,
48-
mapCoverage: true,
48+
mapCoverage: false,
4949
moduleDirectories: ['node_modules'],
5050
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
5151
moduleLoader: '<rootDir>',

packages/jest-runtime/src/__tests__/__snapshots__/transform-test.js.snap

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ exports[`transform does not instrument with source map if mapCoverage config opt
44
`;
55

66
exports[`transform instruments with source map if preprocessor inlines it 1`] = `
7-
"({\\"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));
7+
"({\\"Object.<anonymous>\\":function(module,exports,require,__dirname,__filename,global,jest){/**
8+
* Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
9+
*
10+
* This source code is licensed under the BSD-style license found in the
11+
* LICENSE file in the root directory of this source tree. An additional grant
12+
* of patent rights can be found in the PATENTS file in the same directory.
13+
*/
14+
'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;}();
15+
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));
816
;global.__coverage__['/fruits/banana.js'].inputSourceMapPath = '/cache/jest-transform-cache-test/ab/banana_ab.map';
917
}});"
1018
`;

packages/jest-runtime/src/__tests__/test_root/hasInlineSourceMap.js

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/jest-runtime/src/__tests__/transform-test.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ jest.mock(
5151
jest.mock(
5252
'preprocessor-with-sourcemaps',
5353
() => {
54-
const getProcessResult = jest.fn();
5554
return {
5655
getCacheKey: jest.fn((content, filename, configStr) => 'ab'),
57-
getProcessResult,
58-
process: (content, filename, config) => getProcessResult(),
56+
process: jest.fn(),
5957
};
6058
},
6159
{virtual: true},
@@ -230,6 +228,10 @@ describe('transform', () => {
230228
});
231229

232230
it('instruments with source map if preprocessor supplies it', () => {
231+
if (skipOnWindows.test()) { //snapshot has os-dependent path separators
232+
return;
233+
}
234+
233235
config = Object.assign(config, {
234236
collectCoverage: true,
235237
mapCoverage: true,
@@ -241,7 +243,7 @@ describe('transform', () => {
241243
version: 3,
242244
};
243245

244-
require('preprocessor-with-sourcemaps').getProcessResult.mockReturnValue({
246+
require('preprocessor-with-sourcemaps').process.mockReturnValue({
245247
content: 'content',
246248
sourceMap,
247249
});
@@ -256,6 +258,10 @@ describe('transform', () => {
256258
});
257259

258260
it('instruments with source map if preprocessor inlines it', () => {
261+
if (skipOnWindows.test()) { //snapshot has os-dependent path separators
262+
return;
263+
}
264+
259265
const realFS = require('fs');
260266

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

267-
require('preprocessor-with-sourcemaps').getProcessResult.mockReturnValue({
273+
require('preprocessor-with-sourcemaps').process.mockReturnValue({
268274
content: realFS.readFileSync(
269275
path.resolve(__dirname, './test_root/hasInlineSourceMap.js'),
270276
'utf8'
@@ -292,7 +298,7 @@ describe('transform', () => {
292298
version: 3,
293299
};
294300

295-
require('preprocessor-with-sourcemaps').getProcessResult.mockReturnValue({
301+
require('preprocessor-with-sourcemaps').process.mockReturnValue({
296302
content: 'content',
297303
sourceMap,
298304
});

0 commit comments

Comments
 (0)