Skip to content

Commit 966aab6

Browse files
Aftabnackcpojer
authored andcommitted
Removing the mapCoverage condition on reading inlineSourceMaps. (#5177)
* * Removed usage of mapCoverage on reading inline sourcemaps. * Removed usage of instrument check before saving file into cache. * Passing sourceMaps only for files on which the coverage was obtained * Updated the changelog * Removed usage of mapCoverage from codebase * * Maintaining internal mapCoverage, map only for files which are instrumented by us * Feat: Return sourcemap from babel-jest * Adding back retainLines * * Removed the mapCoverage from docs * Updated the integration testcases * Remove mapCoverage from code base and add deprecation warning * Get sourcemaps only for the files covered in testcases * Updated the condition in babel-jest to prevent breaking changes for the existing users * Added some tests for babel-jest
1 parent d065e87 commit 966aab6

36 files changed

Lines changed: 1612 additions & 115 deletions

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
### Features
1010
* `[docs]` Add MongoDB guide ([#5571](https://github.com/facebook/jest/pull/5571))
11+
* `[jest-runtime]` Deprecate mapCoverage option.
12+
([#5177](https://github.com/facebook/jest/pull/5177))
13+
* `[babel-jest]` Add option to return sourcemap from the transformer separately
14+
from source. ([#5177](https://github.com/facebook/jest/pull/5177))
1115

1216
## jest 22.3.0
1317

@@ -1597,4 +1601,4 @@ See https://facebook.github.io/jest/blog/2016/12/15/2016-in-jest.html
15971601

15981602
## <=0.4.0
15991603

1600-
* See commit history for changes in previous versions of jest.
1604+
* See commit history for changes in previous versions of jest.

TestUtils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
3232
lastCommit: false,
3333
listTests: false,
3434
logHeapUsage: false,
35-
mapCoverage: false,
3635
maxWorkers: 2,
3736
noSCM: null,
3837
noStackTrace: false,

docs/Configuration.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -313,34 +313,6 @@ Default: `undefined`
313313
This option allows the use of a custom global teardown module which exports an
314314
async function that is triggered once after all test suites.
315315

316-
### `mapCoverage` [boolean]
317-
318-
##### available in Jest **20.0.0+**
319-
320-
Default: `false`
321-
322-
If you have [transformers](#transform-object-string-string) configured that emit
323-
source maps, Jest will use them to try and map code coverage against the
324-
original source code when writing [reports](#coveragereporters-array-string) and
325-
checking [thresholds](#coveragethreshold-object). This is done on a best-effort
326-
basis as some compile-to-JavaScript languages may provide more accurate source
327-
maps than others. This can also be resource-intensive. If Jest is taking a long
328-
time to calculate coverage at the end of a test run, try setting this option to
329-
`false`.
330-
331-
Both inline source maps and source maps returned directly from a transformer are
332-
supported. Source map URLs are not supported because Jest may not be able to
333-
locate them. To return source maps from a transformer, the `process` function
334-
can return an object like the following. The `map` property may either be the
335-
source map object, or the source map object as a string.
336-
337-
```js
338-
return {
339-
code: 'the code',
340-
map: 'the source map',
341-
};
342-
```
343-
344316
### `moduleFileExtensions` [array<string>]
345317

346318
Default: `["js", "json", "jsx", "node"]`
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`code coverage for transform instrumented code 1`] = `
4+
Object {
5+
"covered.js": Object {
6+
"_coverageSchema": "332fd63041d2c1bcb487cc26dd0d5f7d97098a6c",
7+
"b": Object {},
8+
"branchMap": Object {},
9+
"f": Object {
10+
"0": 1,
11+
},
12+
"fnMap": Object {
13+
"0": Object {
14+
"decl": Object {
15+
"end": Object {
16+
"column": 36,
17+
"line": 9,
18+
},
19+
"start": Object {
20+
"column": 26,
21+
"line": 9,
22+
},
23+
},
24+
"line": 9,
25+
"loc": Object {
26+
"end": Object {
27+
"column": 1,
28+
"line": 12,
29+
},
30+
"start": Object {
31+
"column": 58,
32+
"line": 9,
33+
},
34+
},
35+
"name": "doES6Stuff",
36+
},
37+
},
38+
"path": "covered.js",
39+
"s": Object {
40+
"0": 1,
41+
"1": 1,
42+
"2": 1,
43+
},
44+
"statementMap": Object {
45+
"0": Object {
46+
"end": Object {
47+
"column": 2,
48+
"line": 12,
49+
},
50+
"start": Object {
51+
"column": 0,
52+
"line": 9,
53+
},
54+
},
55+
"1": Object {
56+
"end": Object {
57+
"column": 41,
58+
"line": 10,
59+
},
60+
"start": Object {
61+
"column": 34,
62+
"line": 10,
63+
},
64+
},
65+
"2": Object {
66+
"end": Object {
67+
"column": 33,
68+
"line": 11,
69+
},
70+
"start": Object {
71+
"column": 2,
72+
"line": 11,
73+
},
74+
},
75+
},
76+
},
77+
}
78+
`;

integration-tests/__tests__/__snapshots__/show_config.test.js.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ exports[`--showConfig outputs config info and exits 1`] = `
7575
\\"globalSetup\\": null,
7676
\\"globalTeardown\\": null,
7777
\\"listTests\\": false,
78-
\\"mapCoverage\\": false,
7978
\\"maxWorkers\\": \\"[maxWorkers]\\",
8079
\\"noStackTrace\\": false,
8180
\\"nonFlagArgs\\": [],

integration-tests/__tests__/coverage_remapping.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ beforeAll(() => {
2323

2424
it('maps code coverage against original source', () => {
2525
run('yarn', dir);
26-
const result = runJest(dir, ['--coverage', '--mapCoverage', '--no-cache']);
26+
const result = runJest(dir, ['--coverage', '--no-cache']);
2727

2828
expect(result.status).toBe(0);
2929

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. 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+
* @flow
8+
*/
9+
10+
'use strict';
11+
12+
const {readFileSync} = require('fs');
13+
const path = require('path');
14+
const {cleanup, run} = require('../Utils');
15+
const runJest = require('../runJest');
16+
17+
const dir = path.resolve(__dirname, '../coverage-transform-instrumented');
18+
const coverageDir = path.join(dir, 'coverage');
19+
20+
beforeAll(() => {
21+
cleanup(coverageDir);
22+
});
23+
24+
it('code coverage for transform instrumented code', () => {
25+
run('yarn', dir);
26+
const result = runJest(dir, ['--coverage', '--no-cache']);
27+
28+
expect(result.status).toBe(0);
29+
30+
const coverageMapFile = path.join(coverageDir, 'coverage-final.json');
31+
const coverageMap = JSON.parse(readFileSync(coverageMapFile, 'utf-8'));
32+
33+
// reduce absolute paths embedded in the coverage map to just filenames
34+
Object.keys(coverageMap).forEach(filename => {
35+
coverageMap[filename].path = path.basename(coverageMap[filename].path);
36+
delete coverageMap[filename].hash;
37+
coverageMap[path.basename(filename)] = coverageMap[filename];
38+
delete coverageMap[filename];
39+
});
40+
expect(coverageMap).toMatchSnapshot();
41+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. 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+
* @flow
8+
*/
9+
10+
'use strict';
11+
12+
const path = require('path');
13+
const {run} = require('../Utils');
14+
const runJest = require('../runJest');
15+
16+
it('processes stack traces and code frames with source maps', () => {
17+
const dir = path.resolve(__dirname, '../stack-trace-source-maps');
18+
run('yarn', dir);
19+
const {stderr} = runJest(dir, ['--no-cache']);
20+
expect(stderr).toMatch('> 14 | (() => expect(false).toBe(true))();');
21+
expect(stderr).toMatch(`at __tests__/fails.ts:14:24
22+
at Object.<anonymous> (__tests__/fails.ts:14:35)`);
23+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. 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 jestPreset = require('babel-preset-jest');
9+
const babelTransform = require('babel-core').transform;
10+
const babelIstanbulPlugin = require('babel-plugin-istanbul').default;
11+
12+
const options = {
13+
presets: ['env', jestPreset],
14+
retainLines: true,
15+
sourceMaps: 'inline',
16+
};
17+
18+
module.exports = {
19+
canInstrument: true,
20+
process(src, filename, config, transformOptions) {
21+
options.filename = filename;
22+
if (transformOptions && transformOptions.instrument) {
23+
options.auxiliaryCommentBefore = ' istanbul ignore next ';
24+
options.plugins = [
25+
[
26+
babelIstanbulPlugin,
27+
{
28+
cwd: config.rootDir,
29+
exclude: [],
30+
},
31+
],
32+
];
33+
}
34+
35+
const transformResult = babelTransform(src, options);
36+
37+
if (!transformResult) {
38+
return src;
39+
}
40+
41+
return transformResult.code;
42+
},
43+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. 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 doES6Stuff = require('../covered.js');
9+
10+
it('works correctly', () => {
11+
const someObj = {someNumber: 10, this: 'is irrelevant'};
12+
expect(doES6Stuff(someObj, 2)).toBe(20);
13+
});

0 commit comments

Comments
 (0)