Skip to content

Commit 25133b3

Browse files
authored
chore: migrate jest-config to TypeScript (#7944)
1 parent 16fe18d commit 25133b3

28 files changed

+377
-298
lines changed

packages/jest-config/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
},
99
"license": "MIT",
1010
"main": "build/index.js",
11+
"types": "build/index.d.ts",
1112
"dependencies": {
1213
"@babel/core": "^7.1.0",
14+
"@jest/types": "^24.1.0",
1315
"babel-jest": "^24.1.0",
1416
"chalk": "^2.0.1",
1517
"glob": "^7.1.1",

packages/jest-config/src/Defaults.js renamed to packages/jest-config/src/Defaults.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
import type {DefaultOptions} from 'types/Config';
11-
8+
import {Config} from '@jest/types';
129
import {replacePathSepForRegex} from 'jest-regex-util';
1310
import {NODE_MODULES} from './constants';
1411
import getCacheDirectory from './getCacheDirectory';
1512

1613
const NODE_MODULES_REGEXP = replacePathSepForRegex(NODE_MODULES);
1714

18-
export default ({
15+
const defaultOptions: Config.DefaultOptions = {
1916
automock: false,
2017
bail: 0,
2118
browser: false,
@@ -83,4 +80,6 @@ export default ({
8380
watch: false,
8481
watchPathIgnorePatterns: [],
8582
watchman: true,
86-
}: DefaultOptions);
83+
};
84+
85+
export default defaultOptions;

packages/jest-config/src/Deprecated.js renamed to packages/jest-config/src/Deprecated.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

108
import chalk from 'chalk';
119
import prettyFormat from 'pretty-format';
1210

13-
const format = (value: mixed) => prettyFormat(value, {min: true});
11+
const format = (value: unknown) => prettyFormat(value, {min: true});
1412

1513
export default {
1614
mapCoverage: () => ` Option ${chalk.bold(
@@ -20,7 +18,7 @@ export default {
2018
Please update your configuration.`,
2119

2220
preprocessorIgnorePatterns: (options: {
23-
preprocessorIgnorePatterns: Array<string>,
21+
preprocessorIgnorePatterns: Array<string>;
2422
}) => ` Option ${chalk.bold(
2523
'"preprocessorIgnorePatterns"',
2624
)} was replaced by ${chalk.bold(
@@ -37,7 +35,7 @@ export default {
3735
Please update your configuration.`,
3836

3937
scriptPreprocessor: (options: {
40-
scriptPreprocessor: string,
38+
scriptPreprocessor: string;
4139
}) => ` Option ${chalk.bold(
4240
'"scriptPreprocessor"',
4341
)} was replaced by ${chalk.bold(
@@ -53,8 +51,8 @@ export default {
5351
5452
Please update your configuration.`,
5553

56-
setupTestFrameworkScriptFile: (options: {
57-
setupTestFrameworkScriptFile: Array<string>,
54+
setupTestFrameworkScriptFile: (_options: {
55+
setupTestFrameworkScriptFile: Array<string>;
5856
}) => ` Option ${chalk.bold(
5957
'"setupTestFrameworkScriptFile"',
6058
)} was replaced by configuration ${chalk.bold(
@@ -64,7 +62,7 @@ export default {
6462
Please update your configuration.`,
6563

6664
testPathDirs: (options: {
67-
testPathDirs: Array<string>,
65+
testPathDirs: Array<string>;
6866
}) => ` Option ${chalk.bold('"testPathDirs"')} was replaced by ${chalk.bold(
6967
'"roots"',
7068
)}.

packages/jest-config/src/Descriptions.js renamed to packages/jest-config/src/Descriptions.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
export default ({
8+
import {Config} from '@jest/types';
9+
10+
const descriptions: {[key in keyof Config.InitialOptions]: string} = {
1111
automock: 'All imported modules in your tests should be mocked automatically',
1212
bail: 'Stop running tests after `n` failures',
1313
browser: 'Respect "browser" field in package.json when resolving modules',
@@ -91,4 +91,6 @@ export default ({
9191
watchPathIgnorePatterns:
9292
'An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode',
9393
watchman: 'Whether to use watchman for file crawling',
94-
}: {[string]: string});
94+
};
95+
96+
export default descriptions;

packages/jest-config/src/ReporterValidationErrors.js renamed to packages/jest-config/src/ReporterValidationErrors.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* @flow
77
*/
88

9-
import type {ReporterConfig} from 'types/Config';
10-
9+
import {Config} from '@jest/types';
10+
// @ts-ignore: Not migrated to TS
1111
import {ValidationError} from 'jest-validate';
1212
import chalk from 'chalk';
1313
import getType from 'jest-get-type';
@@ -26,7 +26,7 @@ const ERROR = `${BULLET}Reporter Validation Error`;
2626
*/
2727
export function createReporterError(
2828
reporterIndex: number,
29-
reporterValue: Array<ReporterConfig> | string,
29+
reporterValue: Array<Config.ReporterConfig> | string,
3030
): ValidationError {
3131
const errorMessage =
3232
` Reporter at index ${reporterIndex} must be of type:\n` +
@@ -38,7 +38,7 @@ export function createReporterError(
3838
}
3939

4040
export function createArrayReporterError(
41-
arrayReporter: ReporterConfig,
41+
arrayReporter: Config.ReporterConfig,
4242
reporterIndex: number,
4343
valueIndex: number,
4444
value: string | Object,
@@ -63,7 +63,7 @@ export function createArrayReporterError(
6363
}
6464

6565
export function validateReporters(
66-
reporterConfig: Array<ReporterConfig | string>,
66+
reporterConfig: Array<Config.ReporterConfig | string>,
6767
): boolean {
6868
return reporterConfig.every((reporter, index) => {
6969
if (Array.isArray(reporter)) {
@@ -77,7 +77,7 @@ export function validateReporters(
7777
}
7878

7979
function validateArrayReporter(
80-
arrayReporter: ReporterConfig,
80+
arrayReporter: Config.ReporterConfig,
8181
reporterIndex: number,
8282
) {
8383
const [path, options] = arrayReporter;

packages/jest-config/src/ValidConfig.js renamed to packages/jest-config/src/ValidConfig.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
import type {InitialOptions} from 'types/Config';
11-
8+
import {Config} from '@jest/types';
129
import {replacePathSepForRegex} from 'jest-regex-util';
10+
// @ts-ignore: Not migrated to TS
1311
import {multipleValidOptions} from 'jest-validate';
1412
import {NODE_MODULES} from './constants';
1513

1614
const NODE_MODULES_REGEXP = replacePathSepForRegex(NODE_MODULES);
1715

18-
export default ({
16+
const initialOptions: Config.InitialOptions = {
1917
automock: false,
20-
bail: (multipleValidOptions(false, 0): any),
18+
bail: multipleValidOptions(false, 0),
2119
browser: false,
2220
cache: true,
2321
cacheDirectory: '/tmp/user/jest',
@@ -40,6 +38,7 @@ export default ({
4038
statements: 100,
4139
},
4240
},
41+
// @ts-ignore: Missing from initial options... https://github.com/facebook/jest/pull/7923
4342
cwd: '/root',
4443
dependencyExtractor: '<rootDir>/dependencyExtractor.js',
4544
displayName: 'project-name',
@@ -135,4 +134,6 @@ export default ({
135134
],
136135
],
137136
watchman: true,
138-
}: InitialOptions);
137+
};
138+
139+
export default initialOptions;

packages/jest-config/src/__tests__/getMaxWorkers.test.js renamed to packages/jest-config/src/__tests__/getMaxWorkers.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ describe('getMaxWorkers', () => {
3838
describe('% based', () => {
3939
it('50% = 2 workers', () => {
4040
const argv = {maxWorkers: '50%'};
41+
// @ts-ignore: need to fix the typing
4142
expect(getMaxWorkers(argv)).toBe(2);
4243
});
4344

4445
it('< 0 workers should become 1', () => {
4546
const argv = {maxWorkers: '1%'};
47+
// @ts-ignore: need to fix the typing
4648
expect(getMaxWorkers(argv)).toBe(1);
4749
});
4850

4951
it("0% shouldn't break", () => {
5052
const argv = {maxWorkers: '0%'};
53+
// @ts-ignore: need to fix the typing
5154
expect(getMaxWorkers(argv)).toBe(1);
5255
});
5356
});

packages/jest-config/src/__tests__/readConfig.test.js renamed to packages/jest-config/src/__tests__/readConfig.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {readConfig} from '../index';
55
test('readConfig() throws when an object is passed without a file path', () => {
66
expect(() => {
77
readConfig(
8+
// @ts-ignore
89
null /* argv */,
910
{} /* packageRootOrConfig */,
1011
false /* skipArgvConfigOption */,

packages/jest-config/src/__tests__/readConfigs.test.js renamed to packages/jest-config/src/__tests__/readConfigs.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {readConfigs} from '../index';
44

55
test('readConfigs() throws when called without project paths', () => {
66
expect(() => {
7+
// @ts-ignore
78
readConfigs(null /* argv */, [] /* projectPaths */);
89
}).toThrowError('jest: No configuration found for any project.');
910
});

0 commit comments

Comments
 (0)