Skip to content

Commit

Permalink
fix(jest-types): tighten Config types and set more defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 17, 2019
1 parent 4643178 commit 619e01d
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 122 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049))
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))
- `[jest-transform]` Don't fail the test suite when a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058))
- `[jest-types]` [**BREAKING**] Use less `null | undefined` in config types ([#9200](https://github.com/facebook/jest/pull/9200))
- `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136))
- `[pretty-format]` Correctly detect memoized elements ([#9196](https://github.com/facebook/jest/pull/9196))

Expand Down
14 changes: 0 additions & 14 deletions packages/jest-config/src/Defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,11 @@ const defaultOptions: Config.DefaultOptions = {
changedFilesWithAncestor: false,
clearMocks: false,
collectCoverage: false,
collectCoverageFrom: null,
coverageDirectory: null,
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
coverageReporters: ['json', 'text', 'lcov', 'clover'],
coverageThreshold: null,
dependencyExtractor: null,
errorOnDeprecated: false,
expand: false,
filter: null,
forceCoverageMatch: [],
globalSetup: null,
globalTeardown: null,
globals: {},
haste: {
computeSha1: false,
Expand All @@ -48,14 +41,10 @@ const defaultOptions: Config.DefaultOptions = {
noStackTrace: false,
notify: false,
notifyMode: 'failure-change',
preset: null,
prettierPath: 'prettier',
projects: null,
resetMocks: false,
resetModules: false,
resolver: null,
restoreMocks: false,
rootDir: null,
roots: ['<rootDir>'],
runTestsByPath: false,
runner: 'jest-runner',
Expand All @@ -70,15 +59,12 @@ const defaultOptions: Config.DefaultOptions = {
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'],
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
testRegex: [],
testResultsProcessor: null,
testRunner: 'jasmine2',
testSequencer: '@jest/test-sequencer',
testURL: 'http://localhost',
timers: 'real',
transform: null,
transformIgnorePatterns: [NODE_MODULES_REGEXP],
useStderr: false,
verbose: null,
watch: false,
watchPathIgnorePatterns: [],
watchman: true,
Expand Down
45 changes: 44 additions & 1 deletion packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ export default function normalize(
key,
rootDir: options.rootDir,
}),
...(Array.isArray(transformElement) ? [transformElement[1]] : []),
Array.isArray(transformElement) ? transformElement[1] : {},
];
});
break;
Expand Down Expand Up @@ -947,6 +947,29 @@ export default function normalize(
newOptions.onlyChanged = newOptions.watch;
}

if (!newOptions.onlyChanged) {
newOptions.onlyChanged = false;
}

if (!newOptions.lastCommit) {
newOptions.lastCommit = false;
}

if (!newOptions.onlyFailures) {
newOptions.onlyFailures = false;
}

if (!newOptions.watchAll) {
newOptions.watchAll = false;
}

// as any since it can happen. We really need to fix the types here
if (
newOptions.moduleNameMapper === (DEFAULT_CONFIG.moduleNameMapper as any)
) {
newOptions.moduleNameMapper = [];
}

newOptions.updateSnapshot =
argv.ci && !argv.updateSnapshot
? 'none'
Expand Down Expand Up @@ -1014,6 +1037,26 @@ export default function normalize(
newOptions.collectCoverageFrom = [];
}

if (!newOptions.findRelatedTests) {
newOptions.findRelatedTests = false;
}

if (!newOptions.projects) {
newOptions.projects = [];
}

if (!newOptions.extraGlobals) {
newOptions.extraGlobals = [];
}

if (!newOptions.forceExit) {
newOptions.forceExit = false;
}

if (!newOptions.logHeapUsage) {
newOptions.logHeapUsage = false;
}

return {
hasDeprecationWarnings,
options: newOptions,
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Options = {
computeDependencies?: boolean;
computeSha1?: boolean;
console?: Console;
dependencyExtractor?: string;
dependencyExtractor?: string | null;
extensions: Array<string>;
forceNodeFilesystemAPI?: boolean;
hasteImplModulePath?: string;
Expand All @@ -79,7 +79,7 @@ type InternalOptions = {
cacheDirectory: string;
computeDependencies: boolean;
computeSha1: boolean;
dependencyExtractor?: string;
dependencyExtractor: string | null;
extensions: Array<string>;
forceNodeFilesystemAPI: boolean;
hasteImplModulePath?: string;
Expand Down Expand Up @@ -251,7 +251,7 @@ class HasteMap extends EventEmitter {
? true
: options.computeDependencies,
computeSha1: options.computeSha1 || false,
dependencyExtractor: options.dependencyExtractor,
dependencyExtractor: options.dependencyExtractor || null,
extensions: options.extensions,
forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI,
hasteImplModulePath: options.hasteImplModulePath,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type Mapper = (item: string) => Array<string> | null;
export type WorkerMessage = {
computeDependencies: boolean;
computeSha1: boolean;
dependencyExtractor?: string;
dependencyExtractor?: string | null;
rootDir: string;
filePath: string;
hasteImplModulePath?: string;
Expand Down
19 changes: 9 additions & 10 deletions packages/jest-reporters/src/coverage_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class CoverageReporter extends BaseReporter {
);
}

this._checkThreshold(this._globalConfig, map);
this._checkThreshold(map);
}

private async _addUntestedFiles(
Expand Down Expand Up @@ -209,11 +209,10 @@ export default class CoverageReporter extends BaseReporter {
}
}

private _checkThreshold(
globalConfig: Config.GlobalConfig,
map: istanbulCoverage.CoverageMap,
) {
if (globalConfig.coverageThreshold) {
private _checkThreshold(map: istanbulCoverage.CoverageMap) {
const {coverageThreshold} = this._globalConfig;

if (coverageThreshold) {
function check(
name: string,
thresholds: Config.CoverageThresholdValue,
Expand Down Expand Up @@ -250,7 +249,7 @@ export default class CoverageReporter extends BaseReporter {
PATH: 'path',
};
const coveredFiles = map.files();
const thresholdGroups = Object.keys(globalConfig.coverageThreshold);
const thresholdGroups = Object.keys(coverageThreshold);
const groupTypeByThresholdGroup: {[index: string]: string} = {};
const filesByGlob: {[index: string]: Array<string>} = {};

Expand Down Expand Up @@ -342,7 +341,7 @@ export default class CoverageReporter extends BaseReporter {
errors = errors.concat(
check(
thresholdGroup,
globalConfig.coverageThreshold[thresholdGroup],
coverageThreshold[thresholdGroup],
coverage,
),
);
Expand All @@ -357,7 +356,7 @@ export default class CoverageReporter extends BaseReporter {
errors = errors.concat(
check(
thresholdGroup,
globalConfig.coverageThreshold[thresholdGroup],
coverageThreshold[thresholdGroup],
coverage,
),
);
Expand All @@ -370,7 +369,7 @@ export default class CoverageReporter extends BaseReporter {
errors = errors.concat(
check(
fileMatchingGlob,
globalConfig.coverageThreshold[thresholdGroup],
coverageThreshold[thresholdGroup],
map.fileCoverageFor(fileMatchingGlob).toSummary(),
),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-resolve/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type ResolverConfig = {
hasCoreModules: boolean;
moduleDirectories: Array<string>;
moduleNameMapper?: Array<ModuleNameMapperConfig> | null;
modulePaths: Array<Config.Path>;
modulePaths?: Array<Config.Path>;
platforms?: Array<string>;
resolver?: Config.Path | null;
rootDir: Config.Path;
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-runtime/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ export function run(cliArgv?: Config.Argv, cliInfo?: Array<string>) {
const options = readConfig(argv, root);
const globalConfig = options.globalConfig;
// Always disable automocking in scripts.
const config = {
const config: Config.ProjectConfig = {
...options.projectConfig,
automock: false,
unmockedModulePathPatterns: null,
};

// Break circular dependency
Expand Down
7 changes: 3 additions & 4 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Runtime {
changedFiles: undefined,
collectCoverage: false,
collectCoverageFrom: [],
collectCoverageOnlyFrom: null,
collectCoverageOnlyFrom: undefined,
};
this._currentlyExecutingModulePath = '';
this._environment = environment;
Expand Down Expand Up @@ -489,7 +489,7 @@ class Runtime {
collectCoverage: this._coverageOptions.collectCoverage,
collectCoverageFrom: this._coverageOptions.collectCoverageFrom,
collectCoverageOnlyFrom: this._coverageOptions.collectCoverageOnlyFrom,
extraGlobals: this._config.extraGlobals || [],
extraGlobals: this._config.extraGlobals,
};
}

Expand Down Expand Up @@ -713,7 +713,6 @@ class Runtime {
Object.defineProperty(localModule, 'require', {
value: this._createRequireImplementation(localModule, options),
});
const extraGlobals = this._config.extraGlobals || [];
const transformedFile = this._scriptTransformer.transform(
filename,
this._getFullTransformationOptions(options),
Expand Down Expand Up @@ -750,7 +749,7 @@ class Runtime {
filename,
localModule.require as LocalModuleRequire,
), // jest object
...extraGlobals.map(globalVariable => {
...this._config.extraGlobals.map(globalVariable => {
if (this._environment.global[globalVariable]) {
return this._environment.global[globalVariable];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export default class ScriptTransformer {
(this.shouldTransform(filename) || instrument);

try {
const extraGlobals = (options && options.extraGlobals) || [];
const extraGlobals = options ? options.extraGlobals : [];

if (willTransform) {
const transformedSource = this.transformSource(
Expand Down
Loading

0 comments on commit 619e01d

Please sign in to comment.