Skip to content

Commit ca2d3a3

Browse files
G-RathSimenB
authored andcommitted
Mark maxWorkers as optional (#8848)
1 parent 8862ec3 commit ca2d3a3

File tree

3 files changed

+126
-118
lines changed

3 files changed

+126
-118
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM from v11 to v15 ([#8851](https://github.com/facebook/jest/pull/8851))
6060
- `[jest-util]` [**BREAKING**] Remove deprecated exports ([#8863](https://github.com/facebook/jest/pull/8863))
6161
- `[jest-validate]` [**BREAKING**] Use ESM exports ([#8874](https://github.com/facebook/jest/pull/8874))
62+
- `[jest-types]` Mark `InitialOptions` as `Partial` ([#8848](https://github.com/facebook/jest/pull/8848))
63+
- `[jest-config]` Refactor `normalize` to be more type safe ([#8848](https://github.com/facebook/jest/pull/8848))
6264

6365
### Performance
6466

packages/jest-config/src/normalize.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const createConfigError = (message: string) =>
4848

4949
// TS 3.5 forces us to split these into 2
5050
const mergeModuleNameMapperWithPreset = (
51-
options: Config.InitialOptions,
51+
options: Config.InitialOptionsWithRootDir,
5252
preset: Config.InitialOptions,
5353
) => {
5454
if (options['moduleNameMapper'] && preset['moduleNameMapper']) {
@@ -61,7 +61,7 @@ const mergeModuleNameMapperWithPreset = (
6161
};
6262

6363
const mergeTransformWithPreset = (
64-
options: Config.InitialOptions,
64+
options: Config.InitialOptionsWithRootDir,
6565
preset: Config.InitialOptions,
6666
) => {
6767
if (options['transform'] && preset['transform']) {
@@ -88,9 +88,9 @@ const mergeGlobalsWithPreset = (
8888
};
8989

9090
const setupPreset = (
91-
options: Config.InitialOptions,
91+
options: Config.InitialOptionsWithRootDir,
9292
optionsPreset: string,
93-
): Config.InitialOptions => {
93+
): Config.InitialOptionsWithRootDir => {
9494
let preset: Config.InitialOptions;
9595
const presetPath = replaceRootDirInPath(options.rootDir, optionsPreset);
9696
const presetModule = Resolver.findNodeModule(
@@ -168,7 +168,7 @@ const setupPreset = (
168168
return {...preset, ...options};
169169
};
170170

171-
const setupBabelJest = (options: Config.InitialOptions) => {
171+
const setupBabelJest = (options: Config.InitialOptionsWithRootDir) => {
172172
const transform = options.transform;
173173
let babelJest;
174174
if (transform) {
@@ -210,7 +210,7 @@ const setupBabelJest = (options: Config.InitialOptions) => {
210210
};
211211

212212
const normalizeCollectCoverageOnlyFrom = (
213-
options: Config.InitialOptions &
213+
options: Config.InitialOptionsWithRootDir &
214214
Required<Pick<Config.InitialOptions, 'collectCoverageOnlyFrom'>>,
215215
key: keyof Pick<Config.InitialOptions, 'collectCoverageOnlyFrom'>,
216216
) => {
@@ -263,7 +263,7 @@ const normalizeCollectCoverageFrom = (
263263
};
264264

265265
const normalizeUnmockedModulePathPatterns = (
266-
options: Config.InitialOptions,
266+
options: Config.InitialOptionsWithRootDir,
267267
key: keyof Pick<
268268
Config.InitialOptions,
269269
| 'coveragePathIgnorePatterns'
@@ -285,8 +285,8 @@ const normalizeUnmockedModulePathPatterns = (
285285
);
286286

287287
const normalizePreprocessor = (
288-
options: Config.InitialOptions,
289-
): Config.InitialOptions => {
288+
options: Config.InitialOptionsWithRootDir,
289+
): Config.InitialOptionsWithRootDir => {
290290
if (options.scriptPreprocessor && options.transform) {
291291
throw createConfigError(
292292
` Options: ${chalk.bold('scriptPreprocessor')} and ${chalk.bold(
@@ -323,10 +323,10 @@ const normalizePreprocessor = (
323323
};
324324

325325
const normalizeMissingOptions = (
326-
options: Config.InitialOptions,
326+
options: Config.InitialOptionsWithRootDir,
327327
configPath: Config.Path | null | undefined,
328328
projectIndex: number,
329-
): Config.InitialOptions => {
329+
): Config.InitialOptionsWithRootDir => {
330330
if (!options.name) {
331331
options.name = createHash('md5')
332332
.update(options.rootDir)
@@ -345,9 +345,9 @@ const normalizeMissingOptions = (
345345

346346
const normalizeRootDir = (
347347
options: Config.InitialOptions,
348-
): Config.InitialOptions => {
348+
): Config.InitialOptionsWithRootDir => {
349349
// Assert that there *is* a rootDir
350-
if (!options.hasOwnProperty('rootDir')) {
350+
if (!options.rootDir) {
351351
throw createConfigError(
352352
` Configuration option ${chalk.bold('rootDir')} must be specified.`,
353353
);
@@ -361,10 +361,13 @@ const normalizeRootDir = (
361361
// ignored
362362
}
363363

364-
return options;
364+
return {
365+
...options,
366+
rootDir: options.rootDir,
367+
};
365368
};
366369

367-
const normalizeReporters = (options: Config.InitialOptions) => {
370+
const normalizeReporters = (options: Config.InitialOptionsWithRootDir) => {
368371
const reporters = options.reporters;
369372
if (!reporters || !Array.isArray(reporters)) {
370373
return options;
@@ -441,15 +444,15 @@ const showTestPathPatternError = (testPathPattern: string) => {
441444
};
442445

443446
export default function normalize(
444-
options: Config.InitialOptions,
447+
initialOptions: Config.InitialOptions,
445448
argv: Config.Argv,
446449
configPath?: Config.Path | null,
447450
projectIndex: number = Infinity,
448451
): {
449452
hasDeprecationWarnings: boolean;
450453
options: AllOptions;
451454
} {
452-
const {hasDeprecationWarnings} = validate(options, {
455+
const {hasDeprecationWarnings} = validate(initialOptions, {
453456
comment: DOCUMENTATION_NOTE,
454457
deprecatedConfig: DEPRECATED_CONFIG,
455458
exampleConfig: VALID_CONFIG,
@@ -465,10 +468,10 @@ export default function normalize(
465468
],
466469
});
467470

468-
options = normalizePreprocessor(
471+
let options = normalizePreprocessor(
469472
normalizeReporters(
470473
normalizeMissingOptions(
471-
normalizeRootDir(setFromArgv(options, argv)),
474+
normalizeRootDir(setFromArgv(initialOptions, argv)),
472475
configPath,
473476
projectIndex,
474477
),

packages/jest-types/src/Config.ts

Lines changed: 102 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -115,117 +115,120 @@ export type DisplayName =
115115
color: DisplayNameColor;
116116
};
117117

118-
export type InitialOptions = {
119-
automock?: boolean;
120-
bail?: boolean | number;
121-
browser?: boolean;
122-
cache?: boolean;
123-
cacheDirectory?: Path;
124-
clearMocks?: boolean;
125-
changedFilesWithAncestor?: boolean;
126-
changedSince?: string;
127-
collectCoverage?: boolean;
128-
collectCoverageFrom?: Array<Glob>;
129-
collectCoverageOnlyFrom?: {
118+
export type InitialOptionsWithRootDir = InitialOptions &
119+
Required<Pick<InitialOptions, 'rootDir'>>;
120+
121+
export type InitialOptions = Partial<{
122+
automock: boolean;
123+
bail: boolean | number;
124+
browser: boolean;
125+
cache: boolean;
126+
cacheDirectory: Path;
127+
clearMocks: boolean;
128+
changedFilesWithAncestor: boolean;
129+
changedSince: string;
130+
collectCoverage: boolean;
131+
collectCoverageFrom: Array<Glob>;
132+
collectCoverageOnlyFrom: {
130133
[key: string]: boolean;
131134
};
132-
coverageDirectory?: string;
133-
coveragePathIgnorePatterns?: Array<string>;
134-
coverageReporters?: Array<string>;
135-
coverageThreshold?: {
135+
coverageDirectory: string;
136+
coveragePathIgnorePatterns: Array<string>;
137+
coverageReporters: Array<string>;
138+
coverageThreshold: {
136139
global: {
137140
[key: string]: number;
138141
};
139142
};
140-
dependencyExtractor?: string;
141-
detectLeaks?: boolean;
142-
detectOpenHandles?: boolean;
143-
displayName?: DisplayName;
144-
expand?: boolean;
145-
extraGlobals?: Array<string>;
146-
filter?: Path;
147-
findRelatedTests?: boolean;
148-
forceCoverageMatch?: Array<Glob>;
149-
forceExit?: boolean;
150-
json?: boolean;
151-
globals?: ConfigGlobals;
152-
globalSetup?: string | null | undefined;
153-
globalTeardown?: string | null | undefined;
154-
haste?: HasteConfig;
155-
reporters?: Array<string | ReporterConfig>;
156-
logHeapUsage?: boolean;
157-
lastCommit?: boolean;
158-
listTests?: boolean;
159-
mapCoverage?: boolean;
160-
maxConcurrency?: number;
143+
dependencyExtractor: string;
144+
detectLeaks: boolean;
145+
detectOpenHandles: boolean;
146+
displayName: DisplayName;
147+
expand: boolean;
148+
extraGlobals: Array<string>;
149+
filter: Path;
150+
findRelatedTests: boolean;
151+
forceCoverageMatch: Array<Glob>;
152+
forceExit: boolean;
153+
json: boolean;
154+
globals: ConfigGlobals;
155+
globalSetup: string | null | undefined;
156+
globalTeardown: string | null | undefined;
157+
haste: HasteConfig;
158+
reporters: Array<string | ReporterConfig>;
159+
logHeapUsage: boolean;
160+
lastCommit: boolean;
161+
listTests: boolean;
162+
mapCoverage: boolean;
163+
maxConcurrency: number;
161164
maxWorkers: number | string;
162-
moduleDirectories?: Array<string>;
163-
moduleFileExtensions?: Array<string>;
164-
moduleLoader?: Path;
165-
moduleNameMapper?: {
165+
moduleDirectories: Array<string>;
166+
moduleFileExtensions: Array<string>;
167+
moduleLoader: Path;
168+
moduleNameMapper: {
166169
[key: string]: string;
167170
};
168-
modulePathIgnorePatterns?: Array<string>;
169-
modulePaths?: Array<string>;
170-
name?: string;
171-
noStackTrace?: boolean;
172-
notify?: boolean;
173-
notifyMode?: string;
174-
onlyChanged?: boolean;
175-
outputFile?: Path;
176-
passWithNoTests?: boolean;
177-
preprocessorIgnorePatterns?: Array<Glob>;
178-
preset?: string | null | undefined;
179-
prettierPath?: string | null | undefined;
180-
projects?: Array<Glob>;
181-
replname?: string | null | undefined;
182-
resetMocks?: boolean;
183-
resetModules?: boolean;
184-
resolver?: Path | null | undefined;
185-
restoreMocks?: boolean;
171+
modulePathIgnorePatterns: Array<string>;
172+
modulePaths: Array<string>;
173+
name: string;
174+
noStackTrace: boolean;
175+
notify: boolean;
176+
notifyMode: string;
177+
onlyChanged: boolean;
178+
outputFile: Path;
179+
passWithNoTests: boolean;
180+
preprocessorIgnorePatterns: Array<Glob>;
181+
preset: string | null | undefined;
182+
prettierPath: string | null | undefined;
183+
projects: Array<Glob>;
184+
replname: string | null | undefined;
185+
resetMocks: boolean;
186+
resetModules: boolean;
187+
resolver: Path | null | undefined;
188+
restoreMocks: boolean;
186189
rootDir: Path;
187-
roots?: Array<Path>;
188-
runner?: string;
189-
runTestsByPath?: boolean;
190-
scriptPreprocessor?: string;
191-
setupFiles?: Array<Path>;
192-
setupTestFrameworkScriptFile?: Path;
193-
setupFilesAfterEnv?: Array<Path>;
194-
silent?: boolean;
195-
skipFilter?: boolean;
196-
skipNodeResolution?: boolean;
197-
snapshotResolver?: Path;
198-
snapshotSerializers?: Array<Path>;
199-
errorOnDeprecated?: boolean;
200-
testEnvironment?: string;
201-
testEnvironmentOptions?: Record<string, any>;
202-
testFailureExitCode?: string | number;
203-
testLocationInResults?: boolean;
204-
testMatch?: Array<Glob>;
205-
testNamePattern?: string;
206-
testPathDirs?: Array<Path>;
207-
testPathIgnorePatterns?: Array<string>;
208-
testRegex?: string | Array<string>;
209-
testResultsProcessor?: string | null | undefined;
210-
testRunner?: string;
211-
testSequencer?: string;
212-
testURL?: string;
213-
testTimeout?: number;
214-
timers?: 'real' | 'fake';
215-
transform?: {
190+
roots: Array<Path>;
191+
runner: string;
192+
runTestsByPath: boolean;
193+
scriptPreprocessor: string;
194+
setupFiles: Array<Path>;
195+
setupTestFrameworkScriptFile: Path;
196+
setupFilesAfterEnv: Array<Path>;
197+
silent: boolean;
198+
skipFilter: boolean;
199+
skipNodeResolution: boolean;
200+
snapshotResolver: Path;
201+
snapshotSerializers: Array<Path>;
202+
errorOnDeprecated: boolean;
203+
testEnvironment: string;
204+
testEnvironmentOptions: Record<string, any>;
205+
testFailureExitCode: string | number;
206+
testLocationInResults: boolean;
207+
testMatch: Array<Glob>;
208+
testNamePattern: string;
209+
testPathDirs: Array<Path>;
210+
testPathIgnorePatterns: Array<string>;
211+
testRegex: string | Array<string>;
212+
testResultsProcessor: string | null | undefined;
213+
testRunner: string;
214+
testSequencer: string;
215+
testURL: string;
216+
testTimeout: number;
217+
timers: 'real' | 'fake';
218+
transform: {
216219
[regex: string]: Path | TransformerConfig;
217220
};
218-
transformIgnorePatterns?: Array<Glob>;
219-
watchPathIgnorePatterns?: Array<string>;
220-
unmockedModulePathPatterns?: Array<string>;
221-
updateSnapshot?: boolean;
222-
useStderr?: boolean;
223-
verbose?: boolean | null | undefined;
224-
watch?: boolean;
225-
watchAll?: boolean;
226-
watchman?: boolean;
227-
watchPlugins?: Array<string | [string, Record<string, any>]>;
228-
};
221+
transformIgnorePatterns: Array<Glob>;
222+
watchPathIgnorePatterns: Array<string>;
223+
unmockedModulePathPatterns: Array<string>;
224+
updateSnapshot: boolean;
225+
useStderr: boolean;
226+
verbose: boolean | null | undefined;
227+
watch: boolean;
228+
watchAll: boolean;
229+
watchman: boolean;
230+
watchPlugins: Array<string | [string, Record<string, any>]>;
231+
}>;
229232

230233
export type SnapshotUpdateState = 'all' | 'new' | 'none';
231234

0 commit comments

Comments
 (0)