Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate jest-config to TypeScript #7944

Merged
merged 8 commits into from Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/jest-config/package.json
Expand Up @@ -8,8 +8,10 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@babel/core": "^7.1.0",
"@jest/types": "^24.1.0",
"babel-jest": "^24.1.0",
"chalk": "^2.0.1",
"glob": "^7.1.1",
Expand Down
Expand Up @@ -3,19 +3,16 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {DefaultOptions} from 'types/Config';

import {Config} from '@jest/types';
import {replacePathSepForRegex} from 'jest-regex-util';
import {NODE_MODULES} from './constants';
import getCacheDirectory from './getCacheDirectory';

const NODE_MODULES_REGEXP = replacePathSepForRegex(NODE_MODULES);

export default ({
const defaultOptions: Config.DefaultOptions = {
automock: false,
bail: 0,
browser: false,
Expand Down Expand Up @@ -83,4 +80,6 @@ export default ({
watch: false,
watchPathIgnorePatterns: [],
watchman: true,
}: DefaultOptions);
};

export default defaultOptions;
Expand Up @@ -3,14 +3,12 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import chalk from 'chalk';
import prettyFormat from 'pretty-format';

const format = (value: mixed) => prettyFormat(value, {min: true});
const format = (value: unknown) => prettyFormat(value, {min: true});

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

preprocessorIgnorePatterns: (options: {
preprocessorIgnorePatterns: Array<string>,
preprocessorIgnorePatterns: Array<string>;
}) => ` Option ${chalk.bold(
'"preprocessorIgnorePatterns"',
)} was replaced by ${chalk.bold(
Expand All @@ -37,7 +35,7 @@ export default {
Please update your configuration.`,

scriptPreprocessor: (options: {
scriptPreprocessor: string,
scriptPreprocessor: string;
}) => ` Option ${chalk.bold(
'"scriptPreprocessor"',
)} was replaced by ${chalk.bold(
Expand All @@ -53,8 +51,8 @@ export default {

Please update your configuration.`,

setupTestFrameworkScriptFile: (options: {
setupTestFrameworkScriptFile: Array<string>,
setupTestFrameworkScriptFile: (_options: {
setupTestFrameworkScriptFile: Array<string>;
}) => ` Option ${chalk.bold(
'"setupTestFrameworkScriptFile"',
)} was replaced by configuration ${chalk.bold(
Expand All @@ -64,7 +62,7 @@ export default {
Please update your configuration.`,

testPathDirs: (options: {
testPathDirs: Array<string>,
testPathDirs: Array<string>;
}) => ` Option ${chalk.bold('"testPathDirs"')} was replaced by ${chalk.bold(
'"roots"',
)}.
Expand Down
Expand Up @@ -3,11 +3,11 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export default ({
import {Config} from '@jest/types';

const descriptions: {[key in keyof Config.InitialOptions]: string} = {
automock: 'All imported modules in your tests should be mocked automatically',
bail: 'Stop running tests after `n` failures',
browser: 'Respect "browser" field in package.json when resolving modules',
Expand Down Expand Up @@ -91,4 +91,6 @@ export default ({
watchPathIgnorePatterns:
'An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode',
watchman: 'Whether to use watchman for file crawling',
}: {[string]: string});
};

export default descriptions;
Expand Up @@ -6,8 +6,8 @@
* @flow
*/

import type {ReporterConfig} from 'types/Config';

import {Config} from '@jest/types';
// @ts-ignore: Not migrated to TS
import {ValidationError} from 'jest-validate';
import chalk from 'chalk';
import getType from 'jest-get-type';
Expand All @@ -26,7 +26,7 @@ const ERROR = `${BULLET}Reporter Validation Error`;
*/
export function createReporterError(
reporterIndex: number,
reporterValue: Array<ReporterConfig> | string,
reporterValue: Array<Config.ReporterConfig> | string,
): ValidationError {
const errorMessage =
` Reporter at index ${reporterIndex} must be of type:\n` +
Expand All @@ -38,7 +38,7 @@ export function createReporterError(
}

export function createArrayReporterError(
arrayReporter: ReporterConfig,
arrayReporter: Config.ReporterConfig,
reporterIndex: number,
valueIndex: number,
value: string | Object,
Expand All @@ -63,7 +63,7 @@ export function createArrayReporterError(
}

export function validateReporters(
reporterConfig: Array<ReporterConfig | string>,
reporterConfig: Array<Config.ReporterConfig | string>,
): boolean {
return reporterConfig.every((reporter, index) => {
if (Array.isArray(reporter)) {
Expand All @@ -77,7 +77,7 @@ export function validateReporters(
}

function validateArrayReporter(
arrayReporter: ReporterConfig,
arrayReporter: Config.ReporterConfig,
reporterIndex: number,
) {
const [path, options] = arrayReporter;
Expand Down
Expand Up @@ -3,21 +3,19 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {InitialOptions} from 'types/Config';

import {Config} from '@jest/types';
import {replacePathSepForRegex} from 'jest-regex-util';
// @ts-ignore: Not migrated to TS
import {multipleValidOptions} from 'jest-validate';
import {NODE_MODULES} from './constants';

const NODE_MODULES_REGEXP = replacePathSepForRegex(NODE_MODULES);

export default ({
const initialOptions: Config.InitialOptions = {
automock: false,
bail: (multipleValidOptions(false, 0): any),
bail: multipleValidOptions(false, 0),
browser: false,
cache: true,
cacheDirectory: '/tmp/user/jest',
Expand All @@ -40,6 +38,7 @@ export default ({
statements: 100,
},
},
// @ts-ignore: Missing from initial options... https://github.com/facebook/jest/pull/7923
cwd: '/root',
dependencyExtractor: '<rootDir>/dependencyExtractor.js',
displayName: 'project-name',
Expand Down Expand Up @@ -135,4 +134,6 @@ export default ({
],
],
watchman: true,
}: InitialOptions);
};

export default initialOptions;
Expand Up @@ -38,16 +38,19 @@ describe('getMaxWorkers', () => {
describe('% based', () => {
it('50% = 2 workers', () => {
const argv = {maxWorkers: '50%'};
// @ts-ignore: need to fix the typing
expect(getMaxWorkers(argv)).toBe(2);
});

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

it("0% shouldn't break", () => {
const argv = {maxWorkers: '0%'};
// @ts-ignore: need to fix the typing
expect(getMaxWorkers(argv)).toBe(1);
});
});
Expand Down
Expand Up @@ -5,6 +5,7 @@ import {readConfig} from '../index';
test('readConfig() throws when an object is passed without a file path', () => {
expect(() => {
readConfig(
// @ts-ignore
null /* argv */,
{} /* packageRootOrConfig */,
false /* skipArgvConfigOption */,
Expand Down
Expand Up @@ -4,6 +4,7 @@ import {readConfigs} from '../index';

test('readConfigs() throws when called without project paths', () => {
expect(() => {
// @ts-ignore
readConfigs(null /* argv */, [] /* projectPaths */);
}).toThrowError('jest: No configuration found for any project.');
});
Expand Up @@ -3,8 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import os from 'os';
Expand Down
Expand Up @@ -6,16 +6,17 @@
*
*/

import {Config} from '@jest/types';
import setFromArgv from '../setFromArgv';

test('maps special values to valid options', () => {
const options = {};
const options = {} as Config.InitialOptions;
const argv = {
coverage: true,
env: 'node',
json: true,
watchAll: true,
};
} as Config.Argv;

expect(setFromArgv(options, argv)).toMatchObject({
collectCoverage: true,
Expand All @@ -27,12 +28,12 @@ test('maps special values to valid options', () => {
});

test('maps regular values to themselves', () => {
const options = {};
const options = {} as Config.InitialOptions;
const argv = {
collectCoverageOnlyFrom: ['a', 'b'],
coverageDirectory: 'covDir',
watchman: true,
};
} as Config.Argv;

expect(setFromArgv(options, argv)).toMatchObject({
collectCoverageOnlyFrom: ['a', 'b'],
Expand All @@ -42,11 +43,11 @@ test('maps regular values to themselves', () => {
});

test('works with string objects', () => {
const options = {};
const options = {} as Config.InitialOptions;
const argv = {
moduleNameMapper: '{"types/(.*)": "<rootDir>/src/types/$1"}',
transform: '{"*.js": "<rootDir>/transformer"}',
};
} as Config.Argv;
expect(setFromArgv(options, argv)).toMatchObject({
moduleNameMapper: {
'types/(.*)': '<rootDir>/src/types/$1',
Expand All @@ -58,10 +59,10 @@ test('works with string objects', () => {
});

test('explicit flags override those from --config', () => {
const options = {};
const options = {} as Config.InitialOptions;
const argv = {
config: '{"watch": false}',
watch: true,
};
} as Config.Argv;
expect(setFromArgv(options, argv)).toMatchObject({watch: true});
});
Expand Up @@ -3,8 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import path from 'path';
Expand Down
Expand Up @@ -3,13 +3,10 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

const path = require('path');
const os = require('os');

import path from 'path';
import os from 'os';
import {sync as realpath} from 'realpath-native';

const getCacheDirectory = () => {
Expand Down
Expand Up @@ -3,23 +3,24 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {Argv} from 'types/Argv';

import os from 'os';
import {Config} from '@jest/types';

export default function getMaxWorkers(argv: Argv): number {
export default function getMaxWorkers(
argv: Partial<Pick<Config.Argv, 'maxWorkers' | 'runInBand' | 'watch'>>,
): number {
if (argv.runInBand) {
return 1;
} else if (argv.maxWorkers) {
const parsed = parseInt(argv.maxWorkers, 10);
// TODO: How to type this properly? Should probably use `coerce` from `yargs`
const maxWorkers = (argv.maxWorkers as unknown) as number | string;
const parsed = parseInt(maxWorkers as string, 10);

if (
typeof argv.maxWorkers === 'string' &&
argv.maxWorkers.trim().endsWith('%') &&
typeof maxWorkers === 'string' &&
maxWorkers.trim().endsWith('%') &&
parsed > 0 &&
parsed <= 100
) {
Expand Down