Skip to content

Commit 619e01d

Browse files
committed
fix(jest-types): tighten Config types and set more defaults
1 parent 4643178 commit 619e01d

File tree

11 files changed

+112
-122
lines changed

11 files changed

+112
-122
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
- `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049))
5555
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))
5656
- `[jest-transform]` Don't fail the test suite when a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058))
57+
- `[jest-types]` [**BREAKING**] Use less `null | undefined` in config types ([#9200](https://github.com/facebook/jest/pull/9200))
5758
- `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136))
5859
- `[pretty-format]` Correctly detect memoized elements ([#9196](https://github.com/facebook/jest/pull/9196))
5960

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/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,

packages/jest-haste-map/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Options = {
5454
computeDependencies?: boolean;
5555
computeSha1?: boolean;
5656
console?: Console;
57-
dependencyExtractor?: string;
57+
dependencyExtractor?: string | null;
5858
extensions: Array<string>;
5959
forceNodeFilesystemAPI?: boolean;
6060
hasteImplModulePath?: string;
@@ -79,7 +79,7 @@ type InternalOptions = {
7979
cacheDirectory: string;
8080
computeDependencies: boolean;
8181
computeSha1: boolean;
82-
dependencyExtractor?: string;
82+
dependencyExtractor: string | null;
8383
extensions: Array<string>;
8484
forceNodeFilesystemAPI: boolean;
8585
hasteImplModulePath?: string;
@@ -251,7 +251,7 @@ class HasteMap extends EventEmitter {
251251
? true
252252
: options.computeDependencies,
253253
computeSha1: options.computeSha1 || false,
254-
dependencyExtractor: options.dependencyExtractor,
254+
dependencyExtractor: options.dependencyExtractor || null,
255255
extensions: options.extensions,
256256
forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI,
257257
hasteImplModulePath: options.hasteImplModulePath,

packages/jest-haste-map/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export type Mapper = (item: string) => Array<string> | null;
1616
export type WorkerMessage = {
1717
computeDependencies: boolean;
1818
computeSha1: boolean;
19-
dependencyExtractor?: string;
19+
dependencyExtractor?: string | null;
2020
rootDir: string;
2121
filePath: string;
2222
hasteImplModulePath?: string;

packages/jest-reporters/src/coverage_reporter.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default class CoverageReporter extends BaseReporter {
107107
);
108108
}
109109

110-
this._checkThreshold(this._globalConfig, map);
110+
this._checkThreshold(map);
111111
}
112112

113113
private async _addUntestedFiles(
@@ -209,11 +209,10 @@ export default class CoverageReporter extends BaseReporter {
209209
}
210210
}
211211

212-
private _checkThreshold(
213-
globalConfig: Config.GlobalConfig,
214-
map: istanbulCoverage.CoverageMap,
215-
) {
216-
if (globalConfig.coverageThreshold) {
212+
private _checkThreshold(map: istanbulCoverage.CoverageMap) {
213+
const {coverageThreshold} = this._globalConfig;
214+
215+
if (coverageThreshold) {
217216
function check(
218217
name: string,
219218
thresholds: Config.CoverageThresholdValue,
@@ -250,7 +249,7 @@ export default class CoverageReporter extends BaseReporter {
250249
PATH: 'path',
251250
};
252251
const coveredFiles = map.files();
253-
const thresholdGroups = Object.keys(globalConfig.coverageThreshold);
252+
const thresholdGroups = Object.keys(coverageThreshold);
254253
const groupTypeByThresholdGroup: {[index: string]: string} = {};
255254
const filesByGlob: {[index: string]: Array<string>} = {};
256255

@@ -342,7 +341,7 @@ export default class CoverageReporter extends BaseReporter {
342341
errors = errors.concat(
343342
check(
344343
thresholdGroup,
345-
globalConfig.coverageThreshold[thresholdGroup],
344+
coverageThreshold[thresholdGroup],
346345
coverage,
347346
),
348347
);
@@ -357,7 +356,7 @@ export default class CoverageReporter extends BaseReporter {
357356
errors = errors.concat(
358357
check(
359358
thresholdGroup,
360-
globalConfig.coverageThreshold[thresholdGroup],
359+
coverageThreshold[thresholdGroup],
361360
coverage,
362361
),
363362
);
@@ -370,7 +369,7 @@ export default class CoverageReporter extends BaseReporter {
370369
errors = errors.concat(
371370
check(
372371
fileMatchingGlob,
373-
globalConfig.coverageThreshold[thresholdGroup],
372+
coverageThreshold[thresholdGroup],
374373
map.fileCoverageFor(fileMatchingGlob).toSummary(),
375374
),
376375
);

packages/jest-resolve/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type ResolverConfig = {
1414
hasCoreModules: boolean;
1515
moduleDirectories: Array<string>;
1616
moduleNameMapper?: Array<ModuleNameMapperConfig> | null;
17-
modulePaths: Array<Config.Path>;
17+
modulePaths?: Array<Config.Path>;
1818
platforms?: Array<string>;
1919
resolver?: Config.Path | null;
2020
rootDir: Config.Path;

packages/jest-runtime/src/cli/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ export function run(cliArgv?: Config.Argv, cliInfo?: Array<string>) {
6868
const options = readConfig(argv, root);
6969
const globalConfig = options.globalConfig;
7070
// Always disable automocking in scripts.
71-
const config = {
71+
const config: Config.ProjectConfig = {
7272
...options.projectConfig,
7373
automock: false,
74-
unmockedModulePathPatterns: null,
7574
};
7675

7776
// Break circular dependency

packages/jest-runtime/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class Runtime {
123123
changedFiles: undefined,
124124
collectCoverage: false,
125125
collectCoverageFrom: [],
126-
collectCoverageOnlyFrom: null,
126+
collectCoverageOnlyFrom: undefined,
127127
};
128128
this._currentlyExecutingModulePath = '';
129129
this._environment = environment;
@@ -489,7 +489,7 @@ class Runtime {
489489
collectCoverage: this._coverageOptions.collectCoverage,
490490
collectCoverageFrom: this._coverageOptions.collectCoverageFrom,
491491
collectCoverageOnlyFrom: this._coverageOptions.collectCoverageOnlyFrom,
492-
extraGlobals: this._config.extraGlobals || [],
492+
extraGlobals: this._config.extraGlobals,
493493
};
494494
}
495495

@@ -713,7 +713,6 @@ class Runtime {
713713
Object.defineProperty(localModule, 'require', {
714714
value: this._createRequireImplementation(localModule, options),
715715
});
716-
const extraGlobals = this._config.extraGlobals || [];
717716
const transformedFile = this._scriptTransformer.transform(
718717
filename,
719718
this._getFullTransformationOptions(options),
@@ -750,7 +749,7 @@ class Runtime {
750749
filename,
751750
localModule.require as LocalModuleRequire,
752751
), // jest object
753-
...extraGlobals.map(globalVariable => {
752+
...this._config.extraGlobals.map(globalVariable => {
754753
if (this._environment.global[globalVariable]) {
755754
return this._environment.global[globalVariable];
756755
}

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export default class ScriptTransformer {
361361
(this.shouldTransform(filename) || instrument);
362362

363363
try {
364-
const extraGlobals = (options && options.extraGlobals) || [];
364+
const extraGlobals = options ? options.extraGlobals : [];
365365

366366
if (willTransform) {
367367
const transformedSource = this.transformSource(

0 commit comments

Comments
 (0)