From 78c0a6293aa0046bc6f1a748186b2073892cf9d9 Mon Sep 17 00:00:00 2001 From: Justin Bay Date: Sun, 12 Feb 2017 23:02:48 -0500 Subject: [PATCH] add mapCoverage to CLI, switch off by default --- dangerfile.js | 4 +++- docs/Configuration.md | 20 +++++++++++-------- .../coverage-remapping-test.js.snap | 2 +- .../__tests__/coverage-remapping-test.js | 2 +- packages/jest-cli/src/cli/args.js | 6 ++++++ packages/jest-config/src/defaults.js | 2 +- packages/jest-config/src/setFromArgv.js | 4 ++++ packages/jest-config/src/validConfig.js | 2 +- .../__snapshots__/transform-test.js.snap | 10 +++++++++- .../__tests__/test_root/hasInlineSourceMap.js | 10 +++++++++- .../src/__tests__/transform-test.js | 8 ++++++++ 11 files changed, 55 insertions(+), 15 deletions(-) diff --git a/dangerfile.js b/dangerfile.js index 243b66e55122..49d9a673e478 100644 --- a/dangerfile.js +++ b/dangerfile.js @@ -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 = [ diff --git a/docs/Configuration.md b/docs/Configuration.md index 23d14067cd7b..06c5e104c130 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -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] Default: `["js", "json", "jsx", "node"]` diff --git a/integration_tests/__tests__/__snapshots__/coverage-remapping-test.js.snap b/integration_tests/__tests__/__snapshots__/coverage-remapping-test.js.snap index 9d2d3815470e..15b1454457f2 100644 --- a/integration_tests/__tests__/__snapshots__/coverage-remapping-test.js.snap +++ b/integration_tests/__tests__/__snapshots__/coverage-remapping-test.js.snap @@ -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 { diff --git a/integration_tests/__tests__/coverage-remapping-test.js b/integration_tests/__tests__/coverage-remapping-test.js index a4c912f989bb..cce153fe3618 100644 --- a/integration_tests/__tests__/coverage-remapping-test.js +++ b/integration_tests/__tests__/coverage-remapping-test.js @@ -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, diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index e2823d267254..14fdd1f6cc35 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -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: diff --git a/packages/jest-config/src/defaults.js b/packages/jest-config/src/defaults.js index c92e55ca30b1..68854d18f8fa 100644 --- a/packages/jest-config/src/defaults.js +++ b/packages/jest-config/src/defaults.js @@ -32,7 +32,7 @@ module.exports = ({ haste: { providesModuleNodeModules: [], }, - mapCoverage: true, + mapCoverage: false, moduleDirectories: ['node_modules'], moduleFileExtensions: [ 'js', diff --git a/packages/jest-config/src/setFromArgv.js b/packages/jest-config/src/setFromArgv.js index ce29233e35a0..2c6bb994e892 100644 --- a/packages/jest-config/src/setFromArgv.js +++ b/packages/jest-config/src/setFromArgv.js @@ -13,6 +13,10 @@ function setFromArgv(config, argv) { config.collectCoverage = true; } + if (argv.mapCoverage) { + config.mapCoverage = true; + } + if (argv.verbose) { config.verbose = argv.verbose; } diff --git a/packages/jest-config/src/validConfig.js b/packages/jest-config/src/validConfig.js index 307451558104..e6b984c9512e 100644 --- a/packages/jest-config/src/validConfig.js +++ b/packages/jest-config/src/validConfig.js @@ -45,7 +45,7 @@ module.exports = ({ }, logHeapUsage: true, logTransformErrors: true, - mapCoverage: true, + mapCoverage: false, moduleDirectories: ['node_modules'], moduleFileExtensions: ['js', 'json', 'jsx', 'node'], moduleLoader: '', diff --git a/packages/jest-runtime/src/__tests__/__snapshots__/transform-test.js.snap b/packages/jest-runtime/src/__tests__/__snapshots__/transform-test.js.snap index b1e6779361df..8b736a822ddd 100644 --- a/packages/jest-runtime/src/__tests__/__snapshots__/transform-test.js.snap +++ b/packages/jest-runtime/src/__tests__/__snapshots__/transform-test.js.snap @@ -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.\\":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.\\":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'; }});" `; diff --git a/packages/jest-runtime/src/__tests__/test_root/hasInlineSourceMap.js b/packages/jest-runtime/src/__tests__/test_root/hasInlineSourceMap.js index b27d518faa2f..adab3ddd4f27 100644 --- a/packages/jest-runtime/src/__tests__/test_root/hasInlineSourceMap.js +++ b/packages/jest-runtime/src/__tests__/test_root/hasInlineSourceMap.js @@ -1,2 +1,10 @@ +/** + * 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'; var value = true ? 1 : 0; -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsSUFBTSxLQUFLLEdBQVcsSUFBSSxHQUFHLENBQUMsR0FBRyxDQUFDLENBQUMifQ== +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBQ0gsWUFBWSxDQUFDO0FBQ2IsSUFBTSxLQUFLLEdBQUcsSUFBSSxHQUFHLENBQUMsR0FBRyxDQUFDLENBQUEifQ== diff --git a/packages/jest-runtime/src/__tests__/transform-test.js b/packages/jest-runtime/src/__tests__/transform-test.js index bc5ed914e7b7..5bf0d8358942 100644 --- a/packages/jest-runtime/src/__tests__/transform-test.js +++ b/packages/jest-runtime/src/__tests__/transform-test.js @@ -230,6 +230,10 @@ describe('transform', () => { }); it('instruments with source map if preprocessor supplies it', () => { + if (skipOnWindows.test()) { //snapshot has file paths + return; + } + config = Object.assign(config, { collectCoverage: true, mapCoverage: true, @@ -256,6 +260,10 @@ describe('transform', () => { }); it('instruments with source map if preprocessor inlines it', () => { + if (skipOnWindows.test()) { //snapshot has file paths + return; + } + const realFS = require('fs'); config = Object.assign(config, {