Skip to content

Commit bc51873

Browse files
lekterableSimenB
authored andcommitted
Remove unnecessary config checks (#7801)
1 parent 94576a5 commit bc51873

9 files changed

Lines changed: 95 additions & 58 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- `[jest-changed-files]` Improve default file selection for Mercurial repos ([#7880](https://github.com/facebook/jest/pull/7880))
1515
- `[jest-validate]` Fix validating async functions ([#7894](https://github.com/facebook/jest/issues/7894))
1616
- `[jest-circus]` Fix bug with test.only ([#7888](https://github.com/facebook/jest/pull/7888))
17+
- `[jest-transform]` Normalize config and remove unecessary checks, convert `TestUtils.js` to TypeScript ([#7801](https://github.com/facebook/jest/pull/7801)
1718

1819
### Chore & Maintenance
1920

TestUtils.js renamed to TestUtils.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
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-
*
7-
* @flow
86
*/
97

10-
'use strict';
11-
12-
import type {GlobalConfig, ProjectConfig} from 'types/Config';
8+
// eslint-disable-next-line import/no-extraneous-dependencies
9+
import {Config} from '@jest/types';
1310

14-
const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
11+
const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
1512
bail: 0,
1613
changedFilesWithAncestor: false,
1714
changedSince: '',
@@ -67,7 +64,7 @@ const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
6764
watchman: false,
6865
};
6966

70-
const DEFAULT_PROJECT_CONFIG: ProjectConfig = {
67+
const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = {
7168
automock: false,
7269
browser: false,
7370
cache: false,
@@ -124,7 +121,9 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = {
124121
watchPathIgnorePatterns: [],
125122
};
126123

127-
export const makeGlobalConfig = (overrides: Object = {}): GlobalConfig => {
124+
export const makeGlobalConfig = (
125+
overrides: Partial<Config.GlobalConfig> = {},
126+
): Config.GlobalConfig => {
128127
const overridesKeys = new Set(Object.keys(overrides));
129128
Object.keys(DEFAULT_GLOBAL_CONFIG).forEach(key => overridesKeys.delete(key));
130129

@@ -138,7 +137,9 @@ export const makeGlobalConfig = (overrides: Object = {}): GlobalConfig => {
138137
return {...DEFAULT_GLOBAL_CONFIG, ...overrides};
139138
};
140139

141-
export const makeProjectConfig = (overrides: Object = {}): ProjectConfig => {
140+
export const makeProjectConfig = (
141+
overrides: Partial<Config.ProjectConfig> = {},
142+
): Config.ProjectConfig => {
142143
const overridesKeys = new Set(Object.keys(overrides));
143144
Object.keys(DEFAULT_PROJECT_CONFIG).forEach(key => overridesKeys.delete(key));
144145

@@ -149,5 +150,5 @@ export const makeProjectConfig = (overrides: Object = {}): ProjectConfig => {
149150
`);
150151
}
151152

152-
return {...DEFAULT_GLOBAL_CONFIG, ...overrides};
153+
return {...DEFAULT_PROJECT_CONFIG, ...overrides};
153154
};

packages/jest-core/src/lib/__tests__/is_valid_path.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import isValidPath from '../is_valid_path';
1111
import path from 'path';
12+
//$FlowFixMe: Converted to TS
1213
import {makeGlobalConfig} from '../../../../../TestUtils';
1314

1415
const rootDir = path.resolve(path.sep, 'root');

packages/jest-reporters/src/__tests__/generateEmptyCoverage.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import generateEmptyCoverage from '../generateEmptyCoverage';
1313

1414
import path from 'path';
1515
import os from 'os';
16+
//$FlowFixMe: Converted to TS
1617
import {makeGlobalConfig, makeProjectConfig} from '../../../../TestUtils';
1718

1819
jest.mock('@jest/transform', () => ({

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import vm from 'vm';
1212
import path from 'path';
1313
import os from 'os';
1414
import {ScriptTransformer} from '@jest/transform';
15+
import {makeProjectConfig} from '../../../../TestUtils';
1516

1617
jest.mock('vm');
1718

@@ -21,11 +22,11 @@ const FILE_PATH_TO_INSTRUMENT = path.resolve(
2122
);
2223

2324
it('instruments files', () => {
24-
const config = {
25+
const config = makeProjectConfig({
2526
cache: false,
2627
cacheDirectory: os.tmpdir(),
2728
rootDir: '/',
28-
};
29+
});
2930
const instrumented = new ScriptTransformer(config).transform(
3031
FILE_PATH_TO_INSTRUMENT,
3132
{collectCoverage: true},

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,60 @@
33
exports[`ScriptTransformer passes expected transform options to getCacheKey 1`] = `
44
Object {
55
"config": Object {
6+
"automock": false,
7+
"browser": false,
68
"cache": true,
79
"cacheDirectory": "/cache/",
10+
"clearMocks": false,
11+
"coveragePathIgnorePatterns": Array [],
12+
"cwd": "/test_root_dir/",
13+
"detectLeaks": false,
14+
"detectOpenHandles": false,
15+
"displayName": undefined,
16+
"errorOnDeprecated": false,
17+
"extraGlobals": Array [],
18+
"filter": null,
19+
"forceCoverageMatch": Array [],
20+
"globalSetup": null,
21+
"globalTeardown": null,
22+
"globals": Object {},
23+
"haste": Object {
24+
"providesModuleNodeModules": Array [],
25+
},
26+
"moduleDirectories": Array [],
27+
"moduleFileExtensions": Array [
28+
"js",
29+
],
30+
"moduleLoader": "/test_module_loader_path",
31+
"moduleNameMapper": Array [],
32+
"modulePathIgnorePatterns": Array [],
33+
"modulePaths": Array [],
834
"name": "test",
35+
"prettierPath": "prettier",
36+
"resetMocks": false,
37+
"resetModules": false,
38+
"resolver": null,
39+
"restoreMocks": false,
940
"rootDir": "/",
41+
"roots": Array [],
42+
"runner": "jest-runner",
43+
"setupFiles": Array [],
44+
"setupFilesAfterEnv": Array [],
45+
"skipFilter": false,
46+
"skipNodeResolution": false,
47+
"snapshotResolver": null,
48+
"snapshotSerializers": Array [],
49+
"testEnvironment": "node",
50+
"testEnvironmentOptions": Object {},
51+
"testLocationInResults": false,
52+
"testMatch": Array [],
53+
"testPathIgnorePatterns": Array [],
54+
"testRegex": Array [
55+
"\\\\.test\\\\.js$",
56+
],
57+
"testRunner": "jest-jasmine2",
58+
"testURL": "",
59+
"timers": "real",
1060
"transform": Array [
1161
Array [
1262
"^.+\\\\.js$",
@@ -16,6 +66,8 @@ Object {
1666
"transformIgnorePatterns": Array [
1767
"/node_modules/",
1868
],
69+
"unmockedModulePathPatterns": null,
70+
"watchPathIgnorePatterns": Array [],
1971
},
2072
"instrument": true,
2173
"rootDir": "/",
@@ -172,7 +224,7 @@ exports[`ScriptTransformer uses multiple preprocessors 1`] = `
172224
const TRANSFORMED = {
173225
filename: '/fruits/banana.js',
174226
script: 'module.exports = \\"banana\\";',
175-
config: '{\\"cache\\":true,\\"cacheDirectory\\":\\"/cache/\\",\\"name\\":\\"test\\",\\"rootDir\\":\\"/\\",\\"transformIgnorePatterns\\":[\\"/node_modules/\\"],\\"transform\\":[[\\"^.+\\\\\\\\.js$\\",\\"test_preprocessor\\"],[\\"^.+\\\\\\\\.css$\\",\\"css-preprocessor\\"]]}',
227+
config: '{\\"automock\\":false,\\"browser\\":false,\\"cache\\":true,\\"cacheDirectory\\":\\"/cache/\\",\\"clearMocks\\":false,\\"coveragePathIgnorePatterns\\":[],\\"cwd\\":\\"/test_root_dir/\\",\\"detectLeaks\\":false,\\"detectOpenHandles\\":false,\\"errorOnDeprecated\\":false,\\"extraGlobals\\":[],\\"filter\\":null,\\"forceCoverageMatch\\":[],\\"globalSetup\\":null,\\"globalTeardown\\":null,\\"globals\\":{},\\"haste\\":{\\"providesModuleNodeModules\\":[]},\\"moduleDirectories\\":[],\\"moduleFileExtensions\\":[\\"js\\"],\\"moduleLoader\\":\\"/test_module_loader_path\\",\\"moduleNameMapper\\":[],\\"modulePathIgnorePatterns\\":[],\\"modulePaths\\":[],\\"name\\":\\"test\\",\\"prettierPath\\":\\"prettier\\",\\"resetMocks\\":false,\\"resetModules\\":false,\\"resolver\\":null,\\"restoreMocks\\":false,\\"rootDir\\":\\"/\\",\\"roots\\":[],\\"runner\\":\\"jest-runner\\",\\"setupFiles\\":[],\\"setupFilesAfterEnv\\":[],\\"skipFilter\\":false,\\"skipNodeResolution\\":false,\\"snapshotResolver\\":null,\\"snapshotSerializers\\":[],\\"testEnvironment\\":\\"node\\",\\"testEnvironmentOptions\\":{},\\"testLocationInResults\\":false,\\"testMatch\\":[],\\"testPathIgnorePatterns\\":[],\\"testRegex\\":[\\"\\\\\\\\.test\\\\\\\\.js$\\"],\\"testRunner\\":\\"jest-jasmine2\\",\\"testURL\\":\\"\\",\\"timers\\":\\"real\\",\\"transform\\":[[\\"^.+\\\\\\\\.js$\\",\\"test_preprocessor\\"],[\\"^.+\\\\\\\\.css$\\",\\"css-preprocessor\\"]],\\"transformIgnorePatterns\\":[\\"/node_modules/\\"],\\"unmockedModulePathPatterns\\":null,\\"watchPathIgnorePatterns\\":[]}',
176228
};
177229
178230
}});"
@@ -198,7 +250,7 @@ exports[`ScriptTransformer uses the supplied preprocessor 1`] = `
198250
const TRANSFORMED = {
199251
filename: '/fruits/banana.js',
200252
script: 'module.exports = \\"banana\\";',
201-
config: '{\\"cache\\":true,\\"cacheDirectory\\":\\"/cache/\\",\\"name\\":\\"test\\",\\"rootDir\\":\\"/\\",\\"transformIgnorePatterns\\":[\\"/node_modules/\\"],\\"transform\\":[[\\"^.+\\\\\\\\.js$\\",\\"test_preprocessor\\"]]}',
253+
config: '{\\"automock\\":false,\\"browser\\":false,\\"cache\\":true,\\"cacheDirectory\\":\\"/cache/\\",\\"clearMocks\\":false,\\"coveragePathIgnorePatterns\\":[],\\"cwd\\":\\"/test_root_dir/\\",\\"detectLeaks\\":false,\\"detectOpenHandles\\":false,\\"errorOnDeprecated\\":false,\\"extraGlobals\\":[],\\"filter\\":null,\\"forceCoverageMatch\\":[],\\"globalSetup\\":null,\\"globalTeardown\\":null,\\"globals\\":{},\\"haste\\":{\\"providesModuleNodeModules\\":[]},\\"moduleDirectories\\":[],\\"moduleFileExtensions\\":[\\"js\\"],\\"moduleLoader\\":\\"/test_module_loader_path\\",\\"moduleNameMapper\\":[],\\"modulePathIgnorePatterns\\":[],\\"modulePaths\\":[],\\"name\\":\\"test\\",\\"prettierPath\\":\\"prettier\\",\\"resetMocks\\":false,\\"resetModules\\":false,\\"resolver\\":null,\\"restoreMocks\\":false,\\"rootDir\\":\\"/\\",\\"roots\\":[],\\"runner\\":\\"jest-runner\\",\\"setupFiles\\":[],\\"setupFilesAfterEnv\\":[],\\"skipFilter\\":false,\\"skipNodeResolution\\":false,\\"snapshotResolver\\":null,\\"snapshotSerializers\\":[],\\"testEnvironment\\":\\"node\\",\\"testEnvironmentOptions\\":{},\\"testLocationInResults\\":false,\\"testMatch\\":[],\\"testPathIgnorePatterns\\":[],\\"testRegex\\":[\\"\\\\\\\\.test\\\\\\\\.js$\\"],\\"testRunner\\":\\"jest-jasmine2\\",\\"testURL\\":\\"\\",\\"timers\\":\\"real\\",\\"transform\\":[[\\"^.+\\\\\\\\.js$\\",\\"test_preprocessor\\"]],\\"transformIgnorePatterns\\":[\\"/node_modules/\\"],\\"unmockedModulePathPatterns\\":null,\\"watchPathIgnorePatterns\\":[]}',
202254
};
203255
204256
}});"

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
'use strict';
1010

11+
import {makeProjectConfig} from '../../../../TestUtils';
12+
1113
jest
1214
.mock('fs', () =>
1315
// Node 10.5.x compatibility
@@ -185,13 +187,13 @@ describe('ScriptTransformer', () => {
185187

186188
writeFileAtomic = require('write-file-atomic');
187189

188-
config = {
190+
config = makeProjectConfig({
189191
cache: true,
190192
cacheDirectory: '/cache/',
191193
name: 'test',
192194
rootDir: '/',
193195
transformIgnorePatterns: ['/node_modules/'],
194-
};
196+
});
195197

196198
ScriptTransformer = require('../ScriptTransformer').default;
197199
};

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@
77
*/
88

99
import shouldInstrument from '../shouldInstrument';
10-
import {makeGlobalConfig} from '../../../../TestUtils';
10+
import {makeProjectConfig} from '../../../../TestUtils';
1111

1212
describe('shouldInstrument', () => {
1313
const defaultFilename = 'source_file.test.js';
1414
const defaultOptions = {
1515
collectCoverage: true,
1616
};
17-
const defaultConfig = makeGlobalConfig({rootDir: '/'});
18-
17+
const defaultConfig = makeProjectConfig({rootDir: '/'});
1918
describe('should return true', () => {
2019
const testShouldInstrument = (
2120
filename = defaultFilename,
22-
options = defaultOptions,
23-
config = defaultConfig,
21+
options,
22+
config,
2423
) => {
25-
const result = shouldInstrument(filename, options, config);
24+
const result = shouldInstrument(
25+
filename,
26+
{...defaultOptions, ...options},
27+
{...defaultConfig, ...config},
28+
);
2629
expect(result).toBe(true);
2730
};
2831

@@ -74,7 +77,6 @@ describe('shouldInstrument', () => {
7477
testShouldInstrument(
7578
'collect/only/from/here.js',
7679
{
77-
collectCoverage: true,
7880
collectCoverageOnlyFrom: {'collect/only/from/here.js': true},
7981
},
8082
defaultConfig,
@@ -85,7 +87,6 @@ describe('shouldInstrument', () => {
8587
testShouldInstrument(
8688
'do/collect/coverage.js',
8789
{
88-
collectCoverage: true,
8990
collectCoverageFrom: ['!**/dont/**/*.js', '**/do/**/*.js'],
9091
},
9192
defaultConfig,
@@ -95,14 +96,12 @@ describe('shouldInstrument', () => {
9596
it('should return true if the file is not in coveragePathIgnorePatterns', () => {
9697
testShouldInstrument('do/collect/coverage.js', defaultOptions, {
9798
coveragePathIgnorePatterns: ['dont'],
98-
rootDir: '/',
9999
});
100100
});
101101

102102
it('should return true if file is a testfile but forceCoverageMatch is set', () => {
103103
testShouldInstrument('do/collect/sum.coverage.test.js', defaultOptions, {
104104
forceCoverageMatch: ['**/*.(coverage).(test).js'],
105-
rootDir: '/',
106105
testRegex: ['.*\\.(test)\\.(js)$'],
107106
});
108107
});
@@ -111,10 +110,14 @@ describe('shouldInstrument', () => {
111110
describe('should return false', () => {
112111
const testShouldInstrument = (
113112
filename = defaultFilename,
114-
options = defaultOptions,
115-
config = defaultConfig,
113+
options,
114+
config,
116115
) => {
117-
const result = shouldInstrument(filename, options, config);
116+
const result = shouldInstrument(
117+
filename,
118+
{...defaultOptions, ...options},
119+
{...defaultConfig, ...config},
120+
);
118121
expect(result).toBe(false);
119122
};
120123

@@ -164,7 +167,6 @@ describe('shouldInstrument', () => {
164167
testShouldInstrument(
165168
'source_file.js',
166169
{
167-
collectCoverage: true,
168170
collectCoverageOnlyFrom: {'collect/only/from/here.js': true},
169171
},
170172
defaultConfig,
@@ -175,7 +177,6 @@ describe('shouldInstrument', () => {
175177
testShouldInstrument(
176178
'dont/collect/coverage.js',
177179
{
178-
collectCoverage: true,
179180
collectCoverageFrom: ['!**/dont/**/*.js', '**/do/**/*.js'],
180181
},
181182
defaultConfig,
@@ -185,7 +186,6 @@ describe('shouldInstrument', () => {
185186
it('if the file is in coveragePathIgnorePatterns', () => {
186187
testShouldInstrument('dont/collect/coverage.js', defaultOptions, {
187188
coveragePathIgnorePatterns: ['dont'],
188-
rootDir: '/',
189189
});
190190
});
191191

@@ -201,27 +201,23 @@ describe('shouldInstrument', () => {
201201
it('if file is a globalSetup file', () => {
202202
testShouldInstrument('globalSetup.js', defaultOptions, {
203203
globalSetup: 'globalSetup.js',
204-
rootDir: '/',
205204
});
206205
});
207206

208207
it('if file is globalTeardown file', () => {
209208
testShouldInstrument('globalTeardown.js', defaultOptions, {
210209
globalTeardown: 'globalTeardown.js',
211-
rootDir: '/',
212210
});
213211
});
214212

215213
it('if file is in setupFiles', () => {
216214
testShouldInstrument('setupTest.js', defaultOptions, {
217-
rootDir: '/',
218215
setupFiles: ['setupTest.js'],
219216
});
220217
});
221218

222219
it('if file is in setupFilesAfterEnv', () => {
223220
testShouldInstrument('setupTest.js', defaultOptions, {
224-
rootDir: '/',
225221
setupFilesAfterEnv: ['setupTest.js'],
226222
});
227223
});

0 commit comments

Comments
 (0)