Skip to content

Commit 45cc554

Browse files
committed
Revert "Produce source maps when instrumenting code (jestjs#9460)"
This reverts commit 03a7877.
1 parent 2a4b073 commit 45cc554

File tree

9 files changed

+88
-142
lines changed

9 files changed

+88
-142
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
### Fixes
88

9-
- `[jest-circus]` Fix type ellision of jest-runtime imports ([#9717](https://github.com/facebook/jest/pull/9717))
9+
- `[jest-circus]` Fix type elision of jest-runtime imports ([#9717](https://github.com/facebook/jest/pull/9717))
10+
- `[@jest/transform]` Fix coverage reporter for uncovered files without transformers, reverting ([#9460](https://github.com/facebook/jest/pull/9460)) ([#9724](https://github.com/facebook/jest/pull/9724))
1011

1112
### Chore & Maintenance
1213

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`produces code coverage for uncovered files without transformer 1`] = `
4+
---------------------|---------|----------|---------|---------|-------------------
5+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
6+
---------------------|---------|----------|---------|---------|-------------------
7+
All files | 0 | 100 | 0 | 0 |
8+
some-random-file.js | 0 | 100 | 0 | 0 | 8,10,11
9+
---------------------|---------|----------|---------|---------|-------------------
10+
`;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. 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+
import * as path from 'path';
9+
import {wrap} from 'jest-snapshot-serializer-raw';
10+
import {cleanup} from '../Utils';
11+
import runJest from '../runJest';
12+
13+
const dir = path.resolve(__dirname, '../coverage-without-transform');
14+
const coverageDir = path.join(dir, 'coverage');
15+
16+
beforeAll(() => {
17+
cleanup(coverageDir);
18+
});
19+
20+
afterAll(() => {
21+
cleanup(coverageDir);
22+
});
23+
24+
it('produces code coverage for uncovered files without transformer', () => {
25+
const {exitCode, stdout} = runJest(dir, ['--coverage', '--no-cache']);
26+
27+
expect(exitCode).toBe(0);
28+
expect(wrap(stdout)).toMatchSnapshot();
29+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. 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+
it('dummy test', () => {
9+
expect(1).toBe(1);
10+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"jest": {
3+
"collectCoverageFrom": ["*.js"],
4+
"transform": {},
5+
"testEnvironment": "node"
6+
}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. 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+
module.exports = function doES6Stuff(testObj, multiplier) {
9+
// eslint-disable-next-line no-unused-vars
10+
const {someNumber, ...others} = testObj;
11+
return someNumber * multiplier;
12+
};

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,8 @@ export default class ScriptTransformer {
191191
return transform;
192192
}
193193

194-
private _instrumentFile(
195-
filename: Config.Path,
196-
input: TransformedSource,
197-
canMapToInput: boolean,
198-
): TransformedSource {
199-
const result = babelTransform(input.code, {
194+
private _instrumentFile(filename: Config.Path, content: string): string {
195+
const result = babelTransform(content, {
200196
auxiliaryCommentBefore: ' istanbul ignore next ',
201197
babelrc: false,
202198
caller: {
@@ -214,27 +210,21 @@ export default class ScriptTransformer {
214210
cwd: this._config.rootDir,
215211
exclude: [],
216212
extension: false,
217-
// Needed for correct coverage as soon as we start storing a source map of the instrumented code
218-
inputSourceMap: input.map,
219213
useInlineSourceMaps: false,
220214
},
221215
],
222216
],
223-
/**
224-
* It's necessary to be able to map back to original source from the instrumented code.
225-
* The inline map is needed for debugging functionality, and exposing it as a separate file is needed
226-
* for mapping stack traces. It's convenient to use 'both' here and avoid extracting the source map.
227-
*
228-
* Previous behavior of emitting no map when we can't map back to original source is preserved.
229-
*/
230-
sourceMaps: canMapToInput ? 'both' : false,
231217
});
232218

233-
if (result && result.code) {
234-
return result as TransformResult;
219+
if (result) {
220+
const {code} = result;
221+
222+
if (code) {
223+
return code;
224+
}
235225
}
236226

237-
return {code: input.code};
227+
return content;
238228
}
239229

240230
private _getRealPath(filepath: Config.Path): Config.Path {
@@ -328,36 +318,17 @@ export default class ScriptTransformer {
328318
}
329319
}
330320

331-
// Apply instrumentation to the code if necessary, keeping the instrumented code and new map
332-
let map = transformed.map;
333321
if (!transformWillInstrument && instrument) {
334-
/**
335-
* We can map the original source code to the instrumented code ONLY if
336-
* - the process of transforming the code produced a source map e.g. ts-jest
337-
* - we did not transform the source code
338-
*
339-
* Otherwise we cannot make any statements about how the instrumented code corresponds to the original code,
340-
* and we should NOT emit any source maps
341-
*
342-
*/
343-
const shouldEmitSourceMaps = (!!transform && !!map) || !transform;
344-
const instrumented = this._instrumentFile(
345-
filename,
346-
transformed,
347-
shouldEmitSourceMaps,
348-
);
349-
code = instrumented.code;
350-
351-
if (instrumented.map) {
352-
map = instrumented.map;
353-
}
322+
code = this._instrumentFile(filename, transformed.code);
354323
} else {
355324
code = transformed.code;
356325
}
357326

358-
if (map) {
327+
if (transformed.map) {
359328
const sourceMapContent =
360-
typeof map === 'string' ? map : JSON.stringify(map);
329+
typeof transformed.map === 'string'
330+
? transformed.map
331+
: JSON.stringify(transformed.map);
361332
writeCacheFile(sourceMapPath, sourceMapContent);
362333
} else {
363334
sourceMapPath = null;

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ exports[`ScriptTransformer transforms a file properly 1`] = `
7878
/* istanbul ignore next */
7979
function cov_25u22311x4() {
8080
var path = "/fruits/banana.js";
81-
var hash = "3f8e915bed83285455a8a16aa04dc0cf5242d755";
81+
var hash = "4be0f6184160be573fc43f7c2a5877c28b7ce249";
8282
var global = new Function("return this")();
8383
var gcv = "__coverage__";
8484
var coverageData = {
@@ -102,9 +102,8 @@ function cov_25u22311x4() {
102102
},
103103
f: {},
104104
b: {},
105-
inputSourceMap: null,
106105
_coverageSchema: "1a1c01bbd47fc00a2c39e90264f33305004495a9",
107-
hash: "3f8e915bed83285455a8a16aa04dc0cf5242d755"
106+
hash: "4be0f6184160be573fc43f7c2a5877c28b7ce249"
108107
};
109108
var coverage = global[gcv] || (global[gcv] = {});
110109
@@ -124,14 +123,13 @@ function cov_25u22311x4() {
124123
cov_25u22311x4();
125124
cov_25u22311x4().s[0]++;
126125
module.exports = "banana";
127-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImJhbmFuYS5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIl0sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQUEsTUFBTSxDQUFDQyxPQUFQLEdBQWlCLFFBQWpCIiwic291cmNlc0NvbnRlbnQiOlsibW9kdWxlLmV4cG9ydHMgPSBcImJhbmFuYVwiOyJdfQ==
128126
`;
129127

130128
exports[`ScriptTransformer transforms a file properly 2`] = `
131129
/* istanbul ignore next */
132130
function cov_23yvu8etmu() {
133131
var path = "/fruits/kiwi.js";
134-
var hash = "8b5afd38d79008f13ebc229b89ef82b12ee9447a";
132+
var hash = "7705dd5fcfbc884dcea7062944cfb8cc5d141d1a";
135133
var global = new Function("return this")();
136134
var gcv = "__coverage__";
137135
var coverageData = {
@@ -193,9 +191,8 @@ function cov_23yvu8etmu() {
193191
"0": 0
194192
},
195193
b: {},
196-
inputSourceMap: null,
197194
_coverageSchema: "1a1c01bbd47fc00a2c39e90264f33305004495a9",
198-
hash: "8b5afd38d79008f13ebc229b89ef82b12ee9447a"
195+
hash: "7705dd5fcfbc884dcea7062944cfb8cc5d141d1a"
199196
};
200197
var coverage = global[gcv] || (global[gcv] = {});
201198
@@ -221,7 +218,6 @@ module.exports = () => {
221218
cov_23yvu8etmu().s[1]++;
222219
return "kiwi";
223220
};
224-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImtpd2kuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyJdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUFBLE1BQU0sQ0FBQ0MsT0FBUCxHQUFpQixNQUFNO0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFBTSxDQUE3QiIsInNvdXJjZXNDb250ZW50IjpbIm1vZHVsZS5leHBvcnRzID0gKCkgPT4gXCJraXdpXCI7Il19
225221
`;
226222

227223
exports[`ScriptTransformer uses multiple preprocessors 1`] = `

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

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -535,96 +535,6 @@ describe('ScriptTransformer', () => {
535535
expect(writeFileAtomic.sync).toHaveBeenCalledTimes(1);
536536
});
537537

538-
it('should write a source map for the instrumented file when transformed', () => {
539-
const transformerConfig = {
540-
...config,
541-
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
542-
};
543-
const scriptTransformer = new ScriptTransformer(transformerConfig);
544-
545-
const map = {
546-
mappings: ';AAAA',
547-
version: 3,
548-
};
549-
550-
// A map from the original source to the instrumented output
551-
/* eslint-disable sort-keys */
552-
const instrumentedCodeMap = {
553-
version: 3,
554-
sources: ['banana.js'],
555-
names: ['content'],
556-
mappings: ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAAA,OAAO',
557-
sourcesContent: ['content'],
558-
};
559-
/* eslint-enable */
560-
561-
require('preprocessor-with-sourcemaps').process.mockReturnValue({
562-
code: 'content',
563-
map,
564-
});
565-
566-
const result = scriptTransformer.transform(
567-
'/fruits/banana.js',
568-
makeGlobalConfig({
569-
collectCoverage: true,
570-
}),
571-
);
572-
expect(result.sourceMapPath).toEqual(expect.any(String));
573-
expect(writeFileAtomic.sync).toBeCalledTimes(2);
574-
expect(writeFileAtomic.sync).toBeCalledWith(
575-
result.sourceMapPath,
576-
JSON.stringify(instrumentedCodeMap),
577-
{
578-
encoding: 'utf8',
579-
fsync: false,
580-
},
581-
);
582-
583-
// Inline source map allows debugging of original source when running instrumented code
584-
expect(result.code).toContain('//# sourceMappingURL');
585-
});
586-
587-
it('should write a source map for the instrumented file when not transformed', () => {
588-
const scriptTransformer = new ScriptTransformer(config);
589-
590-
// A map from the original source to the instrumented output
591-
/* eslint-disable sort-keys */
592-
const instrumentedCodeMap = {
593-
version: 3,
594-
sources: ['banana.js'],
595-
names: ['module', 'exports'],
596-
mappings:
597-
';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAAA,MAAM,CAACC,OAAP,GAAiB,QAAjB',
598-
sourcesContent: ['module.exports = "banana";'],
599-
};
600-
/* eslint-enable */
601-
602-
require('preprocessor-with-sourcemaps').process.mockReturnValue({
603-
code: 'content',
604-
map: null,
605-
});
606-
607-
const result = scriptTransformer.transform(
608-
'/fruits/banana.js',
609-
makeGlobalConfig({
610-
collectCoverage: true,
611-
}),
612-
);
613-
expect(result.sourceMapPath).toEqual(expect.any(String));
614-
expect(writeFileAtomic.sync).toBeCalledTimes(2);
615-
expect(writeFileAtomic.sync).toBeCalledWith(
616-
result.sourceMapPath,
617-
JSON.stringify(instrumentedCodeMap),
618-
{
619-
encoding: 'utf8',
620-
fsync: false,
621-
},
622-
);
623-
624-
// Inline source map allows debugging of original source when running instrumented code
625-
expect(result.code).toContain('//# sourceMappingURL');
626-
});
627-
628538
it('passes expected transform options to getCacheKey', () => {
629539
config = {...config, transform: [['^.+\\.js$', 'test_preprocessor']]};
630540
const scriptTransformer = new ScriptTransformer(config);

0 commit comments

Comments
 (0)