Skip to content

Commit a7f4eed

Browse files
jwbaycpojer
authored andcommitted
let transformers adjust cache key based on mapCoverage (jestjs#4187)
1 parent 96be6f5 commit a7f4eed

4 files changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`ScriptTransformer passes expected transform options to getCacheKey 1`] = `
4+
Object {
5+
"instrument": true,
6+
"mapCoverage": true,
7+
}
8+
`;
9+
310
exports[`ScriptTransformer transforms a file properly 1`] = `
411
"({\\"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\\";
512
}});"

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,21 @@ describe('ScriptTransformer', () => {
359359
expect(writeFileAtomic.sync).toHaveBeenCalledTimes(1);
360360
});
361361

362+
it('passes expected transform options to getCacheKey', () => {
363+
config = Object.assign(config, {
364+
transform: [['^.+\\.js$', 'test_preprocessor']],
365+
});
366+
const scriptTransformer = new ScriptTransformer(config);
367+
368+
scriptTransformer.transform('/fruits/banana.js', {
369+
collectCoverage: true,
370+
mapCoverage: true,
371+
});
372+
373+
const {getCacheKey} = require('test_preprocessor');
374+
expect(getCacheKey.mock.calls[0][3]).toMatchSnapshot();
375+
});
376+
362377
it('reads values from the cache', () => {
363378
const transformConfig = Object.assign(config, {
364379
transform: [['^.+\\.js$', 'test_preprocessor']],

packages/jest-runtime/src/script_transformer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class ScriptTransformer {
7676
.update(
7777
transformer.getCacheKey(fileData, filename, configString, {
7878
instrument,
79+
mapCoverage,
7980
}),
8081
)
8182
.update(CACHE_VERSION)

types/Transform.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export type TransformOptions = {|
2525
instrument: boolean,
2626
|};
2727

28+
export type CacheKeyOptions = {|
29+
instrument: boolean,
30+
mapCoverage: boolean,
31+
|};
32+
2833
export type Transformer = {|
2934
canInstrument?: boolean,
3035
createTransformer(options: any): Transformer,
@@ -33,7 +38,7 @@ export type Transformer = {|
3338
fileData: string,
3439
filePath: Path,
3540
configStr: string,
36-
options: TransformOptions,
41+
options: CacheKeyOptions,
3742
) => string,
3843

3944
process: (

0 commit comments

Comments
 (0)