Skip to content

Commit e090153

Browse files
committed
accept external source maps from transformers
1 parent 851f5bd commit e090153

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
exports[`transform instruments with source map if preprocessor supplies it 1`] = `
2+
"({\"Object.<anonymous>\":function(module,exports,require,__dirname,__filename,global,jest){/* istanbul ignore next */var cov_25u22311x4 = function () {var path = \"/fruits/banana.js\",hash = \"2af15ff13eaa7e17fc0a001b1b75722b8fbeeb74\",global = new Function(\'return this\')(),gcv = \"__coverage__\",coverageData = { path: \"/fruits/banana.js\", statementMap: { \"0\": { start: { line: 1, column: 0 }, end: { line: 1, column: 7 } } }, fnMap: {}, branchMap: {}, s: { \"0\": 0 }, f: {}, b: {}, inputSourceMap: { mappings: \";AAAA\", version: 3 }, _coverageSchema: \"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c\" },coverage = global[gcv] || (global[gcv] = {});if (coverage[path] && coverage[path].hash === hash) {return coverage[path];}coverageData.hash = hash;return coverage[path] = coverageData;}();++cov_25u22311x4.s[0];content;
3+
}});"
4+
`;
5+
16
exports[`transform transforms a file properly 1`] = `
27
"({\"Object.<anonymous>\":function(module,exports,require,__dirname,__filename,global,jest){/* istanbul ignore next */var cov_25u22311x4 = function () {var path = \"/fruits/banana.js\",hash = \"04636d4ae73b4b3e24bf6fba39e08c946fd0afb5\",global = new Function(\'return this\')(),gcv = \"__coverage__\",coverageData = { path: \"/fruits/banana.js\", statementMap: { \"0\": { start: { line: 1, column: 0 }, end: { line: 1, column: 26 } } }, 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;}();++cov_25u22311x4.s[0];module.exports = \"banana\";
38
}});"

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ jest.mock(
4040
{virtual: true},
4141
);
4242

43+
jest.mock(
44+
'preprocessor-with-sourcemaps',
45+
() => {
46+
return {
47+
getCacheKey: jest.fn((content, filename, configStr) => 'ab'),
48+
process: (content, filename, config) => {
49+
return ({
50+
content: 'content',
51+
sourceMap: {
52+
mappings: ';AAAA',
53+
version: 3,
54+
},
55+
});
56+
},
57+
};
58+
},
59+
{virtual: true},
60+
);
61+
4362
jest.mock(
4463
'css-preprocessor',
4564
() => {
@@ -208,6 +227,16 @@ describe('transform', () => {
208227
expect(vm.Script.mock.calls[2][0]).toMatchSnapshot();
209228
});
210229

230+
it('instruments with source map if preprocessor supplies it', () => {
231+
config = Object.assign(config, {
232+
collectCoverage: true,
233+
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
234+
});
235+
236+
transform('/fruits/banana.js', config);
237+
expect(vm.Script.mock.calls[0][0]).toMatchSnapshot();
238+
});
239+
211240
it('reads values from the cache', () => {
212241
if (skipOnWindows.test()) {
213242
return;

packages/jest-runtime/src/transform.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
import type {Config, Path} from 'types/Config';
13-
import type {Transformer} from 'types/Transform';
13+
import type {Transformer, TransformedSource} from 'types/Transform';
1414

1515
const createDirectory = require('jest-util').createDirectory;
1616
const crypto = require('crypto');
@@ -235,7 +235,7 @@ const stripShebang = content => {
235235
};
236236

237237
const instrumentFile = (
238-
content: string,
238+
transformedSource: TransformedSource,
239239
filename: Path,
240240
config: Config,
241241
): string => {
@@ -244,7 +244,7 @@ const instrumentFile = (
244244
const babel = require('babel-core');
245245
const babelPluginIstanbul = require('babel-plugin-istanbul').default;
246246

247-
return babel.transform(content, {
247+
return babel.transform(transformedSource.content, {
248248
auxiliaryCommentBefore: ' istanbul ignore next ',
249249
babelrc: false,
250250
filename,
@@ -254,6 +254,7 @@ const instrumentFile = (
254254
{
255255
cwd: config.rootDir, // files outside `cwd` will not be instrumented
256256
exclude: [],
257+
inputSourceMap: transformedSource.sourceMap,
257258
},
258259
],
259260
],
@@ -276,21 +277,32 @@ const transformSource = (
276277
return result;
277278
}
278279

279-
result = content;
280+
let transformed: TransformedSource = {
281+
content,
282+
sourceMap: null,
283+
};
280284

281285
if (transform && shouldTransform(filename, config)) {
282-
result = transform.process(result, filename, config, {
286+
const processed = transform.process(content, filename, config, {
283287
instrument,
284288
watch: config.watch,
285289
});
290+
291+
if (typeof processed === 'string') {
292+
transformed.content = processed;
293+
} else {
294+
transformed = processed;
295+
}
286296
}
287297

288298
// That means that the transform has a custom instrumentation
289299
// logic and will handle it based on `config.collectCoverage` option
290-
const transformWillInstrument = transform && transform.canInstrument;
300+
const transformDidInstrument = transform && transform.canInstrument;
291301

292-
if (!transformWillInstrument && instrument) {
293-
result = instrumentFile(result, filename, config);
302+
if (!transformDidInstrument && instrument) {
303+
result = instrumentFile(transformed, filename, config);
304+
} else {
305+
result = transformed.content;
294306
}
295307

296308
writeCacheFile(cacheFilePath, result);

types/Transform.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
import type {Config, Path} from 'types/Config';
1313

14+
export type TransformedSource = {|
15+
content: string,
16+
sourceMap: ?SourceMap,
17+
|};
18+
1419
export type SourceMap = {|
1520
file: string,
1621
mappings: string,
@@ -41,5 +46,5 @@ export type Transformer = {|
4146
sourcePath: Path,
4247
config: Config,
4348
options?: TransformOptions,
44-
) => string,
49+
) => string | TransformedSource,
4550
|};

0 commit comments

Comments
 (0)