Skip to content

Commit 4643178

Browse files
authored
chore: make changedFiles option optional in shouldInstrument (#9197)
1 parent 03b97d6 commit 4643178

File tree

8 files changed

+86
-60
lines changed

8 files changed

+86
-60
lines changed

e2e/__tests__/__snapshots__/showConfig.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
8585
"bail": 0,
8686
"changedFilesWithAncestor": false,
8787
"collectCoverage": false,
88-
"collectCoverageFrom": null,
88+
"collectCoverageFrom": [],
8989
"coverageDirectory": "<<REPLACED_ROOT_DIR>>/coverage",
9090
"coverageReporters": [
9191
"json",

packages/jest-config/src/normalize.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,8 @@ export default function normalize(
10101010
}
10111011

10121012
newOptions.collectCoverageFrom = collectCoverageFrom;
1013+
} else if (!newOptions.collectCoverageFrom) {
1014+
newOptions.collectCoverageFrom = [];
10131015
}
10141016

10151017
return {

packages/jest-runtime/src/__tests__/instrumentation.test.js renamed to packages/jest-runtime/src/__tests__/instrumentation.test.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
*
77
*/
88

9-
'use strict';
10-
11-
import vm from 'vm';
12-
import path from 'path';
13-
import os from 'os';
9+
import * as vm from 'vm';
10+
import * as path from 'path';
11+
import * as os from 'os';
1412
import {ScriptTransformer} from '@jest/transform';
15-
import {makeProjectConfig} from '../../../../TestUtils';
13+
import {makeGlobalConfig, makeProjectConfig} from '../../../../TestUtils';
1614

1715
jest.mock('vm');
1816

@@ -27,9 +25,13 @@ it('instruments files', () => {
2725
cacheDirectory: os.tmpdir(),
2826
rootDir: '/',
2927
});
30-
const instrumented = new ScriptTransformer(
31-
config,
32-
).transform(FILE_PATH_TO_INSTRUMENT, {collectCoverage: true}).script;
28+
const instrumented = new ScriptTransformer(config).transform(
29+
FILE_PATH_TO_INSTRUMENT,
30+
{
31+
...makeGlobalConfig({collectCoverage: true}),
32+
changedFiles: undefined,
33+
},
34+
).script;
3335
expect(instrumented instanceof vm.Script).toBe(true);
3436
// We can't really snapshot the resulting coverage, because it depends on
3537
// absolute path of the file, which will be different on different

packages/jest-runtime/src/index.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,7 @@ class Runtime {
178178
}
179179
}
180180

181-
// TODO: Make this `static shouldInstrument = shouldInstrument;` after https://github.com/facebook/jest/issues/7846
182-
static shouldInstrument(
183-
filename: Config.Path,
184-
options: ShouldInstrumentOptions,
185-
config: Config.ProjectConfig,
186-
) {
187-
return shouldInstrument(filename, options, config);
188-
}
181+
static shouldInstrument = shouldInstrument;
189182

190183
static createContext(
191184
config: Config.ProjectConfig,

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

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
*
77
*/
88

9-
'use strict';
10-
11-
import {makeProjectConfig} from '../../../../TestUtils';
9+
import {makeGlobalConfig, makeProjectConfig} from '../../../../TestUtils';
1210

1311
jest
1412
.mock('fs', () =>
@@ -214,9 +212,10 @@ describe('ScriptTransformer', () => {
214212

215213
it('transforms a file properly', () => {
216214
const scriptTransformer = new ScriptTransformer(config);
217-
const response = scriptTransformer.transform('/fruits/banana.js', {
218-
collectCoverage: true,
219-
}).script;
215+
const response = scriptTransformer.transform(
216+
'/fruits/banana.js',
217+
makeGlobalConfig({collectCoverage: true}),
218+
).script;
220219

221220
expect(response instanceof vm.Script).toBe(true);
222221
expect(vm.Script.mock.calls[0][0]).toMatchSnapshot();
@@ -226,24 +225,32 @@ describe('ScriptTransformer', () => {
226225
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');
227226

228227
// in-memory cache
229-
const response2 = scriptTransformer.transform('/fruits/banana.js', {
230-
collectCoverage: true,
231-
}).script;
228+
const response2 = scriptTransformer.transform(
229+
'/fruits/banana.js',
230+
makeGlobalConfig({collectCoverage: true}),
231+
).script;
232232
expect(response2).toBe(response);
233233

234-
scriptTransformer.transform('/fruits/kiwi.js', {
235-
collectCoverage: true,
236-
});
234+
scriptTransformer.transform(
235+
'/fruits/kiwi.js',
236+
makeGlobalConfig({collectCoverage: true}),
237+
);
237238
const snapshot = vm.Script.mock.calls[1][0];
238239
expect(snapshot).toMatchSnapshot();
239240

240-
scriptTransformer.transform('/fruits/kiwi.js', {collectCoverage: true});
241+
scriptTransformer.transform(
242+
'/fruits/kiwi.js',
243+
makeGlobalConfig({collectCoverage: true}),
244+
);
241245

242246
expect(vm.Script.mock.calls[0][0]).not.toEqual(snapshot);
243247
expect(vm.Script.mock.calls[0][0]).not.toMatch(/instrumented kiwi/);
244248

245249
// If we disable coverage, we get a different result.
246-
scriptTransformer.transform('/fruits/kiwi.js', {collectCoverage: false});
250+
scriptTransformer.transform(
251+
'/fruits/kiwi.js',
252+
makeGlobalConfig({collectCoverage: false}),
253+
);
247254
expect(vm.Script.mock.calls[1][0]).toEqual(snapshot);
248255

249256
scriptTransformer.transform('/fruits/banana.js', {
@@ -401,9 +408,12 @@ describe('ScriptTransformer', () => {
401408
map,
402409
});
403410

404-
const result = scriptTransformer.transform('/fruits/banana.js', {
405-
collectCoverage: true,
406-
});
411+
const result = scriptTransformer.transform(
412+
'/fruits/banana.js',
413+
makeGlobalConfig({
414+
collectCoverage: true,
415+
}),
416+
);
407417
expect(result.sourceMapPath).toEqual(expect.any(String));
408418
const mapStr = JSON.stringify(map);
409419
expect(writeFileAtomic.sync).toBeCalledTimes(2);
@@ -431,9 +441,12 @@ describe('ScriptTransformer', () => {
431441

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

434-
const result = scriptTransformer.transform('/fruits/banana.js', {
435-
collectCoverage: true,
436-
});
444+
const result = scriptTransformer.transform(
445+
'/fruits/banana.js',
446+
makeGlobalConfig({
447+
collectCoverage: true,
448+
}),
449+
);
437450
expect(result.sourceMapPath).toEqual(expect.any(String));
438451
expect(writeFileAtomic.sync).toBeCalledTimes(2);
439452
expect(writeFileAtomic.sync).toBeCalledWith(
@@ -468,9 +481,12 @@ describe('ScriptTransformer', () => {
468481

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

471-
const result = scriptTransformer.transform('/fruits/banana.js', {
472-
collectCoverage: true,
473-
});
484+
const result = scriptTransformer.transform(
485+
'/fruits/banana.js',
486+
makeGlobalConfig({
487+
collectCoverage: true,
488+
}),
489+
);
474490
expect(result.sourceMapPath).toBeNull();
475491
expect(writeFileAtomic.sync).toBeCalledTimes(1);
476492

@@ -496,9 +512,12 @@ describe('ScriptTransformer', () => {
496512
map,
497513
});
498514

499-
const result = scriptTransformer.transform('/fruits/banana.js', {
500-
collectCoverage: true,
501-
});
515+
const result = scriptTransformer.transform(
516+
'/fruits/banana.js',
517+
makeGlobalConfig({
518+
collectCoverage: true,
519+
}),
520+
);
502521
expect(result.sourceMapPath).toEqual(expect.any(String));
503522
expect(writeFileAtomic.sync).toBeCalledTimes(2);
504523
expect(writeFileAtomic.sync).toBeCalledWith(
@@ -522,9 +541,12 @@ describe('ScriptTransformer', () => {
522541
map: null,
523542
});
524543

525-
const result = scriptTransformer.transform('/fruits/banana.js', {
526-
collectCoverage: true,
527-
});
544+
const result = scriptTransformer.transform(
545+
'/fruits/banana.js',
546+
makeGlobalConfig({
547+
collectCoverage: true,
548+
}),
549+
);
528550
expect(result.sourceMapPath).toBeFalsy();
529551
expect(writeFileAtomic.sync).toHaveBeenCalledTimes(1);
530552
});
@@ -533,9 +555,12 @@ describe('ScriptTransformer', () => {
533555
config = {...config, transform: [['^.+\\.js$', 'test_preprocessor']]};
534556
const scriptTransformer = new ScriptTransformer(config);
535557

536-
scriptTransformer.transform('/fruits/banana.js', {
537-
collectCoverage: true,
538-
});
558+
scriptTransformer.transform(
559+
'/fruits/banana.js',
560+
makeGlobalConfig({
561+
collectCoverage: true,
562+
}),
563+
);
539564

540565
const {getCacheKey} = require('test_preprocessor');
541566
expect(getCacheKey.mock.calls[0][3]).toMatchSnapshot();

packages/jest-transform/src/__tests__/should_instrument.test.js renamed to packages/jest-transform/src/__tests__/should_instrument.test.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
76
*/
87

8+
import {Config} from '@jest/types';
99
import shouldInstrument from '../shouldInstrument';
10-
import {makeProjectConfig} from '../../../../TestUtils';
10+
import {makeGlobalConfig, makeProjectConfig} from '../../../../TestUtils';
11+
import {Options} from '../types';
1112

1213
describe('shouldInstrument', () => {
1314
const defaultFilename = 'source_file.test.js';
14-
const defaultOptions = {
15-
collectCoverage: true,
15+
const defaultOptions: Options = {
16+
...makeGlobalConfig({
17+
collectCoverage: true,
18+
}),
19+
changedFiles: undefined,
1620
};
1721
const defaultConfig = makeProjectConfig({rootDir: '/'});
1822
describe('should return true', () => {
1923
const testShouldInstrument = (
2024
filename = defaultFilename,
21-
options,
22-
config,
25+
options: Partial<Options>,
26+
config: Partial<Config.ProjectConfig>,
2327
) => {
2428
const result = shouldInstrument(
2529
filename,
@@ -110,8 +114,8 @@ describe('shouldInstrument', () => {
110114
describe('should return false', () => {
111115
const testShouldInstrument = (
112116
filename = defaultFilename,
113-
options,
114-
config,
117+
options: Partial<Options>,
118+
config: Partial<Config.ProjectConfig>,
115119
) => {
116120
const result = shouldInstrument(
117121
filename,

packages/jest-transform/src/shouldInstrument.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default function shouldInstrument(
5858
if (
5959
// still cover if `only` is specified
6060
!options.collectCoverageOnlyFrom &&
61-
options.collectCoverageFrom &&
61+
options.collectCoverageFrom.length &&
6262
micromatch(
6363
[replacePathSepForGlob(path.relative(config.rootDir, filename))],
6464
options.collectCoverageFrom,

packages/jest-transform/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type ShouldInstrumentOptions = Pick<
1313
Config.GlobalConfig,
1414
'collectCoverage' | 'collectCoverageFrom' | 'collectCoverageOnlyFrom'
1515
> & {
16-
changedFiles: Set<Config.Path> | undefined;
16+
changedFiles?: Set<Config.Path>;
1717
};
1818

1919
export type Options = ShouldInstrumentOptions &

0 commit comments

Comments
 (0)