From 68bd0e58dfe38534cb933558ca04289e39fc1adb Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Mon, 19 Aug 2019 08:51:13 +1200 Subject: [PATCH 1/3] Make `InitialOptions` `Partial` --- packages/jest-types/src/Config.ts | 198 +++++++++++++++--------------- 1 file changed, 99 insertions(+), 99 deletions(-) diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 42efe27238c8..82d981df411d 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -115,117 +115,117 @@ export type DisplayName = color: DisplayNameColor; }; -export type InitialOptions = { - automock?: boolean; - bail?: boolean | number; - browser?: boolean; - cache?: boolean; - cacheDirectory?: Path; - clearMocks?: boolean; - changedFilesWithAncestor?: boolean; - changedSince?: string; - collectCoverage?: boolean; - collectCoverageFrom?: Array; - collectCoverageOnlyFrom?: { +export type InitialOptions = Partial<{ + automock: boolean; + bail: boolean | number; + browser: boolean; + cache: boolean; + cacheDirectory: Path; + clearMocks: boolean; + changedFilesWithAncestor: boolean; + changedSince: string; + collectCoverage: boolean; + collectCoverageFrom: Array; + collectCoverageOnlyFrom: { [key: string]: boolean; }; - coverageDirectory?: string; - coveragePathIgnorePatterns?: Array; - coverageReporters?: Array; - coverageThreshold?: { + coverageDirectory: string; + coveragePathIgnorePatterns: Array; + coverageReporters: Array; + coverageThreshold: { global: { [key: string]: number; }; }; - dependencyExtractor?: string; - detectLeaks?: boolean; - detectOpenHandles?: boolean; - displayName?: DisplayName; - expand?: boolean; - extraGlobals?: Array; - filter?: Path; - findRelatedTests?: boolean; - forceCoverageMatch?: Array; - forceExit?: boolean; - json?: boolean; - globals?: ConfigGlobals; - globalSetup?: string | null | undefined; - globalTeardown?: string | null | undefined; - haste?: HasteConfig; - reporters?: Array; - logHeapUsage?: boolean; - lastCommit?: boolean; - listTests?: boolean; - mapCoverage?: boolean; - maxConcurrency?: number; + dependencyExtractor: string; + detectLeaks: boolean; + detectOpenHandles: boolean; + displayName: DisplayName; + expand: boolean; + extraGlobals: Array; + filter: Path; + findRelatedTests: boolean; + forceCoverageMatch: Array; + forceExit: boolean; + json: boolean; + globals: ConfigGlobals; + globalSetup: string | null | undefined; + globalTeardown: string | null | undefined; + haste: HasteConfig; + reporters: Array; + logHeapUsage: boolean; + lastCommit: boolean; + listTests: boolean; + mapCoverage: boolean; + maxConcurrency: number; maxWorkers: number | string; - moduleDirectories?: Array; - moduleFileExtensions?: Array; - moduleLoader?: Path; - moduleNameMapper?: { + moduleDirectories: Array; + moduleFileExtensions: Array; + moduleLoader: Path; + moduleNameMapper: { [key: string]: string; }; - modulePathIgnorePatterns?: Array; - modulePaths?: Array; - name?: string; - noStackTrace?: boolean; - notify?: boolean; - notifyMode?: string; - onlyChanged?: boolean; - outputFile?: Path; - passWithNoTests?: boolean; - preprocessorIgnorePatterns?: Array; - preset?: string | null | undefined; - prettierPath?: string | null | undefined; - projects?: Array; - replname?: string | null | undefined; - resetMocks?: boolean; - resetModules?: boolean; - resolver?: Path | null | undefined; - restoreMocks?: boolean; + modulePathIgnorePatterns: Array; + modulePaths: Array; + name: string; + noStackTrace: boolean; + notify: boolean; + notifyMode: string; + onlyChanged: boolean; + outputFile: Path; + passWithNoTests: boolean; + preprocessorIgnorePatterns: Array; + preset: string | null | undefined; + prettierPath: string | null | undefined; + projects: Array; + replname: string | null | undefined; + resetMocks: boolean; + resetModules: boolean; + resolver: Path | null | undefined; + restoreMocks: boolean; rootDir: Path; - roots?: Array; - runner?: string; - runTestsByPath?: boolean; - scriptPreprocessor?: string; - setupFiles?: Array; - setupTestFrameworkScriptFile?: Path; - setupFilesAfterEnv?: Array; - silent?: boolean; - skipFilter?: boolean; - skipNodeResolution?: boolean; - snapshotResolver?: Path; - snapshotSerializers?: Array; - errorOnDeprecated?: boolean; - testEnvironment?: string; - testEnvironmentOptions?: Record; - testFailureExitCode?: string | number; - testLocationInResults?: boolean; - testMatch?: Array; - testNamePattern?: string; - testPathDirs?: Array; - testPathIgnorePatterns?: Array; - testRegex?: string | Array; - testResultsProcessor?: string | null | undefined; - testRunner?: string; - testSequencer?: string; - testURL?: string; - testTimeout?: number; - timers?: 'real' | 'fake'; - transform?: { + roots: Array; + runner: string; + runTestsByPath: boolean; + scriptPreprocessor: string; + setupFiles: Array; + setupTestFrameworkScriptFile: Path; + setupFilesAfterEnv: Array; + silent: boolean; + skipFilter: boolean; + skipNodeResolution: boolean; + snapshotResolver: Path; + snapshotSerializers: Array; + errorOnDeprecated: boolean; + testEnvironment: string; + testEnvironmentOptions: Record; + testFailureExitCode: string | number; + testLocationInResults: boolean; + testMatch: Array; + testNamePattern: string; + testPathDirs: Array; + testPathIgnorePatterns: Array; + testRegex: string | Array; + testResultsProcessor: string | null | undefined; + testRunner: string; + testSequencer: string; + testURL: string; + testTimeout: number; + timers: 'real' | 'fake'; + transform: { [regex: string]: Path | TransformerConfig; }; - transformIgnorePatterns?: Array; - watchPathIgnorePatterns?: Array; - unmockedModulePathPatterns?: Array; - updateSnapshot?: boolean; - useStderr?: boolean; - verbose?: boolean | null | undefined; - watch?: boolean; - watchAll?: boolean; - watchman?: boolean; - watchPlugins?: Array]>; -}; + transformIgnorePatterns: Array; + watchPathIgnorePatterns: Array; + unmockedModulePathPatterns: Array; + updateSnapshot: boolean; + useStderr: boolean; + verbose: boolean | null | undefined; + watch: boolean; + watchAll: boolean; + watchman: boolean; + watchPlugins: Array]>; +}>; export type SnapshotUpdateState = 'all' | 'new' | 'none'; From f755875dd327e8f57fa3bb9f27c528db6c2cc098 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 23 Aug 2019 08:20:26 +1200 Subject: [PATCH 2/3] Refactor `normalize` to be type safer --- packages/jest-config/src/normalize.ts | 41 ++++++++++++++------------- packages/jest-types/src/Config.ts | 3 ++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 391a78e22385..90ac9f57a8af 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -48,7 +48,7 @@ const createConfigError = (message: string) => // TS 3.5 forces us to split these into 2 const mergeModuleNameMapperWithPreset = ( - options: Config.InitialOptions, + options: Config.InitialOptionsWithRootDir, preset: Config.InitialOptions, ) => { if (options['moduleNameMapper'] && preset['moduleNameMapper']) { @@ -61,7 +61,7 @@ const mergeModuleNameMapperWithPreset = ( }; const mergeTransformWithPreset = ( - options: Config.InitialOptions, + options: Config.InitialOptionsWithRootDir, preset: Config.InitialOptions, ) => { if (options['transform'] && preset['transform']) { @@ -88,9 +88,9 @@ const mergeGlobalsWithPreset = ( }; const setupPreset = ( - options: Config.InitialOptions, + options: Config.InitialOptionsWithRootDir, optionsPreset: string, -): Config.InitialOptions => { +): Config.InitialOptionsWithRootDir => { let preset: Config.InitialOptions; const presetPath = replaceRootDirInPath(options.rootDir, optionsPreset); const presetModule = Resolver.findNodeModule( @@ -168,7 +168,7 @@ const setupPreset = ( return {...preset, ...options}; }; -const setupBabelJest = (options: Config.InitialOptions) => { +const setupBabelJest = (options: Config.InitialOptionsWithRootDir) => { const transform = options.transform; let babelJest; if (transform) { @@ -210,7 +210,7 @@ const setupBabelJest = (options: Config.InitialOptions) => { }; const normalizeCollectCoverageOnlyFrom = ( - options: Config.InitialOptions & + options: Config.InitialOptionsWithRootDir & Required>, key: keyof Pick, ) => { @@ -263,7 +263,7 @@ const normalizeCollectCoverageFrom = ( }; const normalizeUnmockedModulePathPatterns = ( - options: Config.InitialOptions, + options: Config.InitialOptionsWithRootDir, key: keyof Pick< Config.InitialOptions, | 'coveragePathIgnorePatterns' @@ -285,8 +285,8 @@ const normalizeUnmockedModulePathPatterns = ( ); const normalizePreprocessor = ( - options: Config.InitialOptions, -): Config.InitialOptions => { + options: Config.InitialOptionsWithRootDir, +): Config.InitialOptionsWithRootDir => { if (options.scriptPreprocessor && options.transform) { throw createConfigError( ` Options: ${chalk.bold('scriptPreprocessor')} and ${chalk.bold( @@ -323,10 +323,10 @@ const normalizePreprocessor = ( }; const normalizeMissingOptions = ( - options: Config.InitialOptions, + options: Config.InitialOptionsWithRootDir, configPath: Config.Path | null | undefined, projectIndex: number, -): Config.InitialOptions => { +): Config.InitialOptionsWithRootDir => { if (!options.name) { options.name = createHash('md5') .update(options.rootDir) @@ -345,9 +345,9 @@ const normalizeMissingOptions = ( const normalizeRootDir = ( options: Config.InitialOptions, -): Config.InitialOptions => { +): Config.InitialOptionsWithRootDir => { // Assert that there *is* a rootDir - if (!options.hasOwnProperty('rootDir')) { + if (!options.rootDir) { throw createConfigError( ` Configuration option ${chalk.bold('rootDir')} must be specified.`, ); @@ -361,10 +361,13 @@ const normalizeRootDir = ( // ignored } - return options; + return { + ...options, + rootDir: options.rootDir, + }; }; -const normalizeReporters = (options: Config.InitialOptions) => { +const normalizeReporters = (options: Config.InitialOptionsWithRootDir) => { const reporters = options.reporters; if (!reporters || !Array.isArray(reporters)) { return options; @@ -441,7 +444,7 @@ const showTestPathPatternError = (testPathPattern: string) => { }; export default function normalize( - options: Config.InitialOptions, + initialOptions: Config.InitialOptions, argv: Config.Argv, configPath?: Config.Path | null, projectIndex: number = Infinity, @@ -449,7 +452,7 @@ export default function normalize( hasDeprecationWarnings: boolean; options: AllOptions; } { - const {hasDeprecationWarnings} = validate(options, { + const {hasDeprecationWarnings} = validate(initialOptions, { comment: DOCUMENTATION_NOTE, deprecatedConfig: DEPRECATED_CONFIG, exampleConfig: VALID_CONFIG, @@ -465,10 +468,10 @@ export default function normalize( ], }); - options = normalizePreprocessor( + let options = normalizePreprocessor( normalizeReporters( normalizeMissingOptions( - normalizeRootDir(setFromArgv(options, argv)), + normalizeRootDir(setFromArgv(initialOptions, argv)), configPath, projectIndex, ), diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 82d981df411d..ec9ad63921b0 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -115,6 +115,9 @@ export type DisplayName = color: DisplayNameColor; }; +export type InitialOptionsWithRootDir = InitialOptions & + Required>; + export type InitialOptions = Partial<{ automock: boolean; bail: boolean | number; From 6d9a328f58ec570e550bcf52d2d831e14ae91c86 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 26 Oct 2019 22:24:24 +1300 Subject: [PATCH 3/3] Update `CHANGELOG.md` --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9692e4b6c2c0..7157176cf7a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,8 @@ - `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM from v11 to v15 ([#8851](https://github.com/facebook/jest/pull/8851)) - `[jest-util]` [**BREAKING**] Remove deprecated exports ([#8863](https://github.com/facebook/jest/pull/8863)) - `[jest-validate]` [**BREAKING**] Use ESM exports ([#8874](https://github.com/facebook/jest/pull/8874)) +- `[jest-types]` Mark `InitialOptions` as `Partial` ([#8848](https://github.com/facebook/jest/pull/8848)) +- `[jest-config]` Refactor `normalize` to be more type safe ([#8848](https://github.com/facebook/jest/pull/8848)) ### Performance