Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add mapCoverage to CLI, switch off by default
  • Loading branch information
jwbay committed Feb 13, 2017
commit ad11e77f411c5801295fb294e9920b267a22fc69
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
bin/
docs/
examples/react-native
packages/jest-runtime/src/__tests__/test_root/hasInlineSourceMap.js
flow-typed/**
packages/*/build/**
types/**
Expand Down
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,7 @@ 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){/* istanbul ignore next */var cov_25u22311x4 = function () {var path = \\"/fruits/banana.js\\",hash = \\"051b2f7208d7f64ba1cda9a8fec8ebb925b6a32a\\",global = new Function('return this')(),gcv = \\"__coverage__\\",coverageData = { path: \\"/fruits/banana.js\\", statementMap: { \\"0\\": { start: { line: 1, column: 8 }, end: { line: 1, column: 9 } } }, fnMap: {}, branchMap: {}, s: { \\"0\\": 0 }, f: {}, b: {}, _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 x = /* istanbul ignore next */(++cov_25u22311x4.s[0], 1);
;global.__coverage__['/fruits/banana.js'].inputSourceMapPath = '/cache/jest-transform-cache-test/ab/banana_ab.map';
}});"
`;
Expand Down

This file was deleted.

33 changes: 20 additions & 13 deletions packages/jest-runtime/src/__tests__/transform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
'use strict';

const skipOnWindows = require('skipOnWindows');
const path = require('path');

jest
.mock('graceful-fs')
Expand Down Expand Up @@ -51,11 +50,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 +227,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 +242,7 @@ describe('transform', () => {
version: 3,
};

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

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

config = Object.assign(config, {
collectCoverage: true,
mapCoverage: true,
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
});

require('preprocessor-with-sourcemaps').getProcessResult.mockReturnValue({
content: realFS.readFileSync(
path.resolve(__dirname, './test_root/hasInlineSourceMap.js'),
'utf8'
),
const sourceMap = JSON.stringify({
mappings: 'AAAA,IAAM,CAAC,GAAW,CAAC,CAAC',
version: 3,
});

const content = 'var x = 1;\n' +
'//# sourceMappingURL=data:application/json;base64,' +
new Buffer(sourceMap).toString('base64');

require('preprocessor-with-sourcemaps').process.mockReturnValue(content);

transform('/fruits/banana.js', config);
expect(vm.Script.mock.calls[0][0]).toMatchSnapshot();
expect(fs.writeFileSync).toBeCalledWith(
'/cache/jest-transform-cache-test/ab/banana_ab.map',
expect.stringMatching(/mappings/),
sourceMap,
'utf8',
);
});
Expand All @@ -292,7 +299,7 @@ describe('transform', () => {
version: 3,
};

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