Skip to content

Commit

Permalink
fix(jest-types): tighten Config types and set more defaults (#9200)
Browse files Browse the repository at this point in the history
* fix(jest-types): tighten Config types and set more defaults

* update snaps and make `extraGlobals` optional in `@jest/transform`
  • Loading branch information
SimenB authored and thymikee committed Nov 19, 2019
1 parent 6bd4e56 commit 8d3ddd5
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 165 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -56,6 +56,7 @@
- `[jest-snapshot]` Omit irrelevant `received` properties when property matchers fail ([#9198](https://github.com/facebook/jest/pull/9198))
- `[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
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Expand Up @@ -19,7 +19,7 @@ FAIL __tests__/index.js
"moduleNameMapper": {
"/\\.(css|less)$/": "no-such-module"
},
"resolver": null
"resolver": undefined
}
8 | 'use strict';
Expand Down
27 changes: 13 additions & 14 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -13,14 +13,11 @@ exports[`--showConfig outputs config info and exits 1`] = `
"/node_modules/"
],
"cwd": "<<REPLACED_ROOT_DIR>>",
"dependencyExtractor": null,
"detectLeaks": false,
"detectOpenHandles": false,
"errorOnDeprecated": false,
"filter": null,
"extraGlobals": [],
"forceCoverageMatch": [],
"globalSetup": null,
"globalTeardown": null,
"globals": {},
"haste": {
"computeSha1": false,
Expand All @@ -38,13 +35,12 @@ exports[`--showConfig outputs config info and exits 1`] = `
"tsx",
"node"
],
"moduleNameMapper": {},
"moduleNameMapper": [],
"modulePathIgnorePatterns": [],
"name": "[md5 hash]",
"prettierPath": "prettier",
"resetMocks": false,
"resetModules": false,
"resolver": null,
"restoreMocks": false,
"rootDir": "<<REPLACED_ROOT_DIR>>",
"roots": [
Expand Down Expand Up @@ -72,7 +68,8 @@ exports[`--showConfig outputs config info and exits 1`] = `
"transform": [
[
"^.+\\\\.[jt]sx?$",
"<<REPLACED_JEST_PACKAGES_DIR>>/babel-jest/build/index.js"
"<<REPLACED_JEST_PACKAGES_DIR>>/babel-jest/build/index.js",
{}
]
],
"transformIgnorePatterns": [
Expand All @@ -93,35 +90,37 @@ exports[`--showConfig outputs config info and exits 1`] = `
"lcov",
"clover"
],
"coverageThreshold": null,
"detectLeaks": false,
"detectOpenHandles": false,
"errorOnDeprecated": false,
"expand": false,
"filter": null,
"globalSetup": null,
"globalTeardown": null,
"extraGlobals": [],
"findRelatedTests": false,
"forceExit": false,
"json": false,
"lastCommit": false,
"listTests": false,
"logHeapUsage": false,
"maxConcurrency": 5,
"maxWorkers": "[maxWorkers]",
"noStackTrace": false,
"nonFlagArgs": [],
"notify": false,
"notifyMode": "failure-change",
"onlyChanged": false,
"onlyFailures": false,
"passWithNoTests": false,
"projects": null,
"projects": [],
"rootDir": "<<REPLACED_ROOT_DIR>>",
"runTestsByPath": false,
"skipFilter": false,
"testFailureExitCode": 1,
"testPathPattern": "",
"testResultsProcessor": null,
"testSequencer": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-test-sequencer/build/index.js",
"updateSnapshot": "all",
"useStderr": false,
"verbose": null,
"watch": false,
"watchAll": false,
"watchman": true
},
"version": "[version]"
Expand Down
Expand Up @@ -42,10 +42,10 @@ module.exports = {
// collectCoverage: false,
// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: null,
// collectCoverageFrom: undefined,
// The directory where Jest should output its coverage files
// coverageDirectory: null,
// coverageDirectory: undefined,
// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
Expand All @@ -61,10 +61,10 @@ module.exports = {
// ],
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: null,
// coverageThreshold: undefined,
// A path to a custom dependency extractor
// dependencyExtractor: null,
// dependencyExtractor: undefined,
// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,
Expand All @@ -73,10 +73,10 @@ module.exports = {
// forceCoverageMatch: [],
// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: null,
// globalSetup: undefined,
// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: null,
// globalTeardown: undefined,
// A set of global variables that need to be available in all test environments
// globals: {},
Expand Down Expand Up @@ -112,10 +112,10 @@ module.exports = {
// notifyMode: \\"failure-change\\",
// A preset that is used as a base for Jest's configuration
// preset: null,
// preset: undefined,
// Run tests from one or more projects
// projects: null,
// projects: undefined,
// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
Expand All @@ -127,13 +127,13 @@ module.exports = {
// resetModules: false,
// A path to a custom resolver
// resolver: null,
// resolver: undefined,
// Automatically restore mock state between every test
// restoreMocks: false,
// The root directory that Jest should scan for tests and modules within
// rootDir: null,
// rootDir: undefined,
// A list of paths to directories that Jest should use to search for files in
// roots: [
Expand Down Expand Up @@ -176,7 +176,7 @@ module.exports = {
// testRegex: [],
// This option allows the use of a custom results processor
// testResultsProcessor: null,
// testResultsProcessor: undefined,
// This option allows use of a custom test runner
// testRunner: \\"jasmine2\\",
Expand All @@ -188,7 +188,7 @@ module.exports = {
// timers: \\"real\\",
// A map from regular expressions to paths to transformers
// transform: null,
// transform: undefined,
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
Expand All @@ -199,7 +199,7 @@ module.exports = {
// unmockedModulePathPatterns: undefined,
// Indicates whether each individual test should be reported during the run
// verbose: null,
// verbose: undefined,
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
Expand Down
14 changes: 0 additions & 14 deletions packages/jest-config/src/Defaults.ts
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
26 changes: 13 additions & 13 deletions packages/jest-config/src/__tests__/normalize.test.js
Expand Up @@ -328,12 +328,12 @@ describe('transform', () => {
);

expect(options.transform).toEqual([
[DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util'],
[DEFAULT_JS_PATTERN, require.resolve('babel-jest')],
['abs-path', '/qux/quux'],
[DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util', {}],
[DEFAULT_JS_PATTERN, require.resolve('babel-jest'), {}],
['abs-path', '/qux/quux', {}],
]);
});
it("pulls in config if it's passed as an array", () => {
it("pulls in config if it's passed as an array, and defaults to empty object", () => {
const {options} = normalize(
{
rootDir: '/root/',
Expand All @@ -346,9 +346,9 @@ describe('transform', () => {
{},
);
expect(options.transform).toEqual([
[DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util'],
[DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util', {}],
[DEFAULT_JS_PATTERN, require.resolve('babel-jest'), {rootMode: 'upward'}],
['abs-path', '/qux/quux'],
['abs-path', '/qux/quux', {}],
]);
});
});
Expand Down Expand Up @@ -822,7 +822,7 @@ describe('Upgrade help', () => {
{},
);

expect(options.transform).toEqual([['.*', '/node_modules/bar/baz']]);
expect(options.transform).toEqual([['.*', '/node_modules/bar/baz', {}]]);
expect(options.transformIgnorePatterns).toEqual([
joinForPattern('bar', 'baz'),
joinForPattern('qux', 'quux'),
Expand Down Expand Up @@ -1152,8 +1152,8 @@ describe('preset', () => {
'/node_modules/b',
]);
expect(options.transform).toEqual([
['a', '/node_modules/a'],
['b', '/node_modules/b'],
['a', '/node_modules/a', {}],
['b', '/node_modules/b', {}],
]);
});

Expand Down Expand Up @@ -1202,10 +1202,10 @@ describe('preset', () => {
);

expect(options.transform).toEqual([
['e', '/node_modules/ee'],
['b', '/node_modules/bb'],
['c', '/node_modules/cc'],
['a', '/node_modules/aa'],
['e', '/node_modules/ee', {}],
['b', '/node_modules/bb', {}],
['c', '/node_modules/cc', {}],
['a', '/node_modules/aa', {}],
]);
});

Expand Down
45 changes: 44 additions & 1 deletion packages/jest-config/src/normalize.ts
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

0 comments on commit 8d3ddd5

Please sign in to comment.