Skip to content

Commit 8d3ddd5

Browse files
SimenBthymikee
authored andcommitted
fix(jest-types): tighten Config types and set more defaults (#9200)
* fix(jest-types): tighten Config types and set more defaults * update snaps and make `extraGlobals` optional in `@jest/transform`
1 parent 6bd4e56 commit 8d3ddd5

File tree

16 files changed

+153
-165
lines changed

16 files changed

+153
-165
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
- `[jest-snapshot]` Omit irrelevant `received` properties when property matchers fail ([#9198](https://github.com/facebook/jest/pull/9198))
5757
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))
5858
- `[jest-transform]` Don't fail the test suite when a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058))
59+
- `[jest-types]` [**BREAKING**] Use less `null | undefined` in config types ([#9200](https://github.com/facebook/jest/pull/9200))
5960
- `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136))
6061
- `[pretty-format]` Correctly detect memoized elements ([#9196](https://github.com/facebook/jest/pull/9196))
6162

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ FAIL __tests__/index.js
1919
"moduleNameMapper": {
2020
"/\\.(css|less)$/": "no-such-module"
2121
},
22-
"resolver": null
22+
"resolver": undefined
2323
}
2424
2525
8 | 'use strict';

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ exports[`--showConfig outputs config info and exits 1`] = `
1313
"/node_modules/"
1414
],
1515
"cwd": "<<REPLACED_ROOT_DIR>>",
16-
"dependencyExtractor": null,
1716
"detectLeaks": false,
1817
"detectOpenHandles": false,
1918
"errorOnDeprecated": false,
20-
"filter": null,
19+
"extraGlobals": [],
2120
"forceCoverageMatch": [],
22-
"globalSetup": null,
23-
"globalTeardown": null,
2421
"globals": {},
2522
"haste": {
2623
"computeSha1": false,
@@ -38,13 +35,12 @@ exports[`--showConfig outputs config info and exits 1`] = `
3835
"tsx",
3936
"node"
4037
],
41-
"moduleNameMapper": {},
38+
"moduleNameMapper": [],
4239
"modulePathIgnorePatterns": [],
4340
"name": "[md5 hash]",
4441
"prettierPath": "prettier",
4542
"resetMocks": false,
4643
"resetModules": false,
47-
"resolver": null,
4844
"restoreMocks": false,
4945
"rootDir": "<<REPLACED_ROOT_DIR>>",
5046
"roots": [
@@ -72,7 +68,8 @@ exports[`--showConfig outputs config info and exits 1`] = `
7268
"transform": [
7369
[
7470
"^.+\\\\.[jt]sx?$",
75-
"<<REPLACED_JEST_PACKAGES_DIR>>/babel-jest/build/index.js"
71+
"<<REPLACED_JEST_PACKAGES_DIR>>/babel-jest/build/index.js",
72+
{}
7673
]
7774
],
7875
"transformIgnorePatterns": [
@@ -93,35 +90,37 @@ exports[`--showConfig outputs config info and exits 1`] = `
9390
"lcov",
9491
"clover"
9592
],
96-
"coverageThreshold": null,
9793
"detectLeaks": false,
9894
"detectOpenHandles": false,
9995
"errorOnDeprecated": false,
10096
"expand": false,
101-
"filter": null,
102-
"globalSetup": null,
103-
"globalTeardown": null,
97+
"extraGlobals": [],
98+
"findRelatedTests": false,
99+
"forceExit": false,
104100
"json": false,
101+
"lastCommit": false,
105102
"listTests": false,
103+
"logHeapUsage": false,
106104
"maxConcurrency": 5,
107105
"maxWorkers": "[maxWorkers]",
108106
"noStackTrace": false,
109107
"nonFlagArgs": [],
110108
"notify": false,
111109
"notifyMode": "failure-change",
110+
"onlyChanged": false,
111+
"onlyFailures": false,
112112
"passWithNoTests": false,
113-
"projects": null,
113+
"projects": [],
114114
"rootDir": "<<REPLACED_ROOT_DIR>>",
115115
"runTestsByPath": false,
116116
"skipFilter": false,
117117
"testFailureExitCode": 1,
118118
"testPathPattern": "",
119-
"testResultsProcessor": null,
120119
"testSequencer": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-test-sequencer/build/index.js",
121120
"updateSnapshot": "all",
122121
"useStderr": false,
123-
"verbose": null,
124122
"watch": false,
123+
"watchAll": false,
125124
"watchman": true
126125
},
127126
"version": "[version]"

packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ module.exports = {
4242
// collectCoverage: false,
4343
4444
// An array of glob patterns indicating a set of files for which coverage information should be collected
45-
// collectCoverageFrom: null,
45+
// collectCoverageFrom: undefined,
4646
4747
// The directory where Jest should output its coverage files
48-
// coverageDirectory: null,
48+
// coverageDirectory: undefined,
4949
5050
// An array of regexp pattern strings used to skip coverage collection
5151
// coveragePathIgnorePatterns: [
@@ -61,10 +61,10 @@ module.exports = {
6161
// ],
6262
6363
// An object that configures minimum threshold enforcement for coverage results
64-
// coverageThreshold: null,
64+
// coverageThreshold: undefined,
6565
6666
// A path to a custom dependency extractor
67-
// dependencyExtractor: null,
67+
// dependencyExtractor: undefined,
6868
6969
// Make calling deprecated APIs throw helpful error messages
7070
// errorOnDeprecated: false,
@@ -73,10 +73,10 @@ module.exports = {
7373
// forceCoverageMatch: [],
7474
7575
// A path to a module which exports an async function that is triggered once before all test suites
76-
// globalSetup: null,
76+
// globalSetup: undefined,
7777
7878
// A path to a module which exports an async function that is triggered once after all test suites
79-
// globalTeardown: null,
79+
// globalTeardown: undefined,
8080
8181
// A set of global variables that need to be available in all test environments
8282
// globals: {},
@@ -112,10 +112,10 @@ module.exports = {
112112
// notifyMode: \\"failure-change\\",
113113
114114
// A preset that is used as a base for Jest's configuration
115-
// preset: null,
115+
// preset: undefined,
116116
117117
// Run tests from one or more projects
118-
// projects: null,
118+
// projects: undefined,
119119
120120
// Use this configuration option to add custom reporters to Jest
121121
// reporters: undefined,
@@ -127,13 +127,13 @@ module.exports = {
127127
// resetModules: false,
128128
129129
// A path to a custom resolver
130-
// resolver: null,
130+
// resolver: undefined,
131131
132132
// Automatically restore mock state between every test
133133
// restoreMocks: false,
134134
135135
// The root directory that Jest should scan for tests and modules within
136-
// rootDir: null,
136+
// rootDir: undefined,
137137
138138
// A list of paths to directories that Jest should use to search for files in
139139
// roots: [
@@ -176,7 +176,7 @@ module.exports = {
176176
// testRegex: [],
177177
178178
// This option allows the use of a custom results processor
179-
// testResultsProcessor: null,
179+
// testResultsProcessor: undefined,
180180
181181
// This option allows use of a custom test runner
182182
// testRunner: \\"jasmine2\\",
@@ -188,7 +188,7 @@ module.exports = {
188188
// timers: \\"real\\",
189189
190190
// A map from regular expressions to paths to transformers
191-
// transform: null,
191+
// transform: undefined,
192192
193193
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
194194
// transformIgnorePatterns: [
@@ -199,7 +199,7 @@ module.exports = {
199199
// unmockedModulePathPatterns: undefined,
200200
201201
// Indicates whether each individual test should be reported during the run
202-
// verbose: null,
202+
// verbose: undefined,
203203
204204
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
205205
// watchPathIgnorePatterns: [],

packages/jest-config/src/Defaults.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,11 @@ const defaultOptions: Config.DefaultOptions = {
2121
changedFilesWithAncestor: false,
2222
clearMocks: false,
2323
collectCoverage: false,
24-
collectCoverageFrom: null,
25-
coverageDirectory: null,
2624
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
2725
coverageReporters: ['json', 'text', 'lcov', 'clover'],
28-
coverageThreshold: null,
29-
dependencyExtractor: null,
3026
errorOnDeprecated: false,
3127
expand: false,
32-
filter: null,
3328
forceCoverageMatch: [],
34-
globalSetup: null,
35-
globalTeardown: null,
3629
globals: {},
3730
haste: {
3831
computeSha1: false,
@@ -48,14 +41,10 @@ const defaultOptions: Config.DefaultOptions = {
4841
noStackTrace: false,
4942
notify: false,
5043
notifyMode: 'failure-change',
51-
preset: null,
5244
prettierPath: 'prettier',
53-
projects: null,
5445
resetMocks: false,
5546
resetModules: false,
56-
resolver: null,
5747
restoreMocks: false,
58-
rootDir: null,
5948
roots: ['<rootDir>'],
6049
runTestsByPath: false,
6150
runner: 'jest-runner',
@@ -70,15 +59,12 @@ const defaultOptions: Config.DefaultOptions = {
7059
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'],
7160
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
7261
testRegex: [],
73-
testResultsProcessor: null,
7462
testRunner: 'jasmine2',
7563
testSequencer: '@jest/test-sequencer',
7664
testURL: 'http://localhost',
7765
timers: 'real',
78-
transform: null,
7966
transformIgnorePatterns: [NODE_MODULES_REGEXP],
8067
useStderr: false,
81-
verbose: null,
8268
watch: false,
8369
watchPathIgnorePatterns: [],
8470
watchman: true,

packages/jest-config/src/__tests__/normalize.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,12 @@ describe('transform', () => {
328328
);
329329

330330
expect(options.transform).toEqual([
331-
[DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util'],
332-
[DEFAULT_JS_PATTERN, require.resolve('babel-jest')],
333-
['abs-path', '/qux/quux'],
331+
[DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util', {}],
332+
[DEFAULT_JS_PATTERN, require.resolve('babel-jest'), {}],
333+
['abs-path', '/qux/quux', {}],
334334
]);
335335
});
336-
it("pulls in config if it's passed as an array", () => {
336+
it("pulls in config if it's passed as an array, and defaults to empty object", () => {
337337
const {options} = normalize(
338338
{
339339
rootDir: '/root/',
@@ -346,9 +346,9 @@ describe('transform', () => {
346346
{},
347347
);
348348
expect(options.transform).toEqual([
349-
[DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util'],
349+
[DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util', {}],
350350
[DEFAULT_JS_PATTERN, require.resolve('babel-jest'), {rootMode: 'upward'}],
351-
['abs-path', '/qux/quux'],
351+
['abs-path', '/qux/quux', {}],
352352
]);
353353
});
354354
});
@@ -822,7 +822,7 @@ describe('Upgrade help', () => {
822822
{},
823823
);
824824

825-
expect(options.transform).toEqual([['.*', '/node_modules/bar/baz']]);
825+
expect(options.transform).toEqual([['.*', '/node_modules/bar/baz', {}]]);
826826
expect(options.transformIgnorePatterns).toEqual([
827827
joinForPattern('bar', 'baz'),
828828
joinForPattern('qux', 'quux'),
@@ -1152,8 +1152,8 @@ describe('preset', () => {
11521152
'/node_modules/b',
11531153
]);
11541154
expect(options.transform).toEqual([
1155-
['a', '/node_modules/a'],
1156-
['b', '/node_modules/b'],
1155+
['a', '/node_modules/a', {}],
1156+
['b', '/node_modules/b', {}],
11571157
]);
11581158
});
11591159

@@ -1202,10 +1202,10 @@ describe('preset', () => {
12021202
);
12031203

12041204
expect(options.transform).toEqual([
1205-
['e', '/node_modules/ee'],
1206-
['b', '/node_modules/bb'],
1207-
['c', '/node_modules/cc'],
1208-
['a', '/node_modules/aa'],
1205+
['e', '/node_modules/ee', {}],
1206+
['b', '/node_modules/bb', {}],
1207+
['c', '/node_modules/cc', {}],
1208+
['a', '/node_modules/aa', {}],
12091209
]);
12101210
});
12111211

packages/jest-config/src/normalize.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ export default function normalize(
673673
key,
674674
rootDir: options.rootDir,
675675
}),
676-
...(Array.isArray(transformElement) ? [transformElement[1]] : []),
676+
Array.isArray(transformElement) ? transformElement[1] : {},
677677
];
678678
});
679679
break;
@@ -947,6 +947,29 @@ export default function normalize(
947947
newOptions.onlyChanged = newOptions.watch;
948948
}
949949

950+
if (!newOptions.onlyChanged) {
951+
newOptions.onlyChanged = false;
952+
}
953+
954+
if (!newOptions.lastCommit) {
955+
newOptions.lastCommit = false;
956+
}
957+
958+
if (!newOptions.onlyFailures) {
959+
newOptions.onlyFailures = false;
960+
}
961+
962+
if (!newOptions.watchAll) {
963+
newOptions.watchAll = false;
964+
}
965+
966+
// as any since it can happen. We really need to fix the types here
967+
if (
968+
newOptions.moduleNameMapper === (DEFAULT_CONFIG.moduleNameMapper as any)
969+
) {
970+
newOptions.moduleNameMapper = [];
971+
}
972+
950973
newOptions.updateSnapshot =
951974
argv.ci && !argv.updateSnapshot
952975
? 'none'
@@ -1014,6 +1037,26 @@ export default function normalize(
10141037
newOptions.collectCoverageFrom = [];
10151038
}
10161039

1040+
if (!newOptions.findRelatedTests) {
1041+
newOptions.findRelatedTests = false;
1042+
}
1043+
1044+
if (!newOptions.projects) {
1045+
newOptions.projects = [];
1046+
}
1047+
1048+
if (!newOptions.extraGlobals) {
1049+
newOptions.extraGlobals = [];
1050+
}
1051+
1052+
if (!newOptions.forceExit) {
1053+
newOptions.forceExit = false;
1054+
}
1055+
1056+
if (!newOptions.logHeapUsage) {
1057+
newOptions.logHeapUsage = false;
1058+
}
1059+
10171060
return {
10181061
hasDeprecationWarnings,
10191062
options: newOptions,

0 commit comments

Comments
 (0)