Skip to content

Commit

Permalink
chore: migrate jest-validate to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 26, 2019
1 parent 3bd384b commit 6624022
Show file tree
Hide file tree
Showing 23 changed files with 118 additions and 121 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -62,6 +62,7 @@
- `[jest-runner]`: Migrate to TypeScript ([#7968](https://github.com/facebook/jest/pull/7968))
- `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964), [#7988](https://github.com/facebook/jest/pull/7988))
- `[@jest/fake-timers]`: Extract FakeTimers class from `jest-util` into a new separate package ([#7987](https://github.com/facebook/jest/pull/7987))
- `[jest-validate]`: Migrate to TypeScript ([#7991](https://github.com/facebook/jest/pull/7991))

### Performance

Expand Down
5 changes: 2 additions & 3 deletions packages/jest-config/src/ReporterValidationErrors.ts
Expand Up @@ -7,7 +7,6 @@
*/

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 @@ -27,7 +26,7 @@ const ERROR = `${BULLET}Reporter Validation Error`;
export function createReporterError(
reporterIndex: number,
reporterValue: Array<Config.ReporterConfig> | string,
): ValidationError {
) {
const errorMessage =
` Reporter at index ${reporterIndex} must be of type:\n` +
` ${chalk.bold.green(validReporterTypes.join(' or '))}\n` +
Expand All @@ -44,7 +43,7 @@ export function createArrayReporterError(
value: string | Object,
expectedType: string,
valueName: string,
): ValidationError {
) {
const errorMessage =
` Unexpected value for ${valueName} ` +
`at index ${valueIndex} of reporter at index ${reporterIndex}\n` +
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/ValidConfig.ts
Expand Up @@ -7,14 +7,14 @@

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);

const initialOptions: Config.InitialOptions = {
automock: false,
// @ts-ignore TODO: type this properly
bail: multipleValidOptions(false, 0),
browser: false,
cache: true,
Expand Down
1 change: 0 additions & 1 deletion packages/jest-config/src/normalize.ts
Expand Up @@ -9,7 +9,6 @@ import crypto from 'crypto';
import path from 'path';
import glob from 'glob';
import {Config} from '@jest/types';
// @ts-ignore: Not migrated to TS
import {ValidationError, validate} from 'jest-validate';
import {clearLine, replacePathSepForGlob} from 'jest-util';
import chalk from 'chalk';
Expand Down
1 change: 0 additions & 1 deletion packages/jest-config/src/utils.ts
Expand Up @@ -7,7 +7,6 @@

import path from 'path';
import {Config} from '@jest/types';
// @ts-ignore: Not migrated to TS
import {ValidationError} from 'jest-validate';
import Resolver from 'jest-resolve';
import chalk from 'chalk';
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-config/tsconfig.json
Expand Up @@ -4,16 +4,16 @@
"rootDir": "src",
"outDir": "build"
},
// TODO: This is missing `jest-validate`, in addition to
// `jest-environment-jsdom` and `jest-jasmine2`, but those are just
// `require.resolve`d, so no real use for their types
// TODO: This is missing jest-environment-jsdom` and `jest-jasmine2`,
// but those are just `require.resolve`d, so no real use for their types
"references": [
{"path": "../jest-environment-node"},
{"path": "../jest-get-type"},
{"path": "../jest-regex-util"},
{"path": "../jest-resolve"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-validate"},
{"path": "../pretty-format"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/cli/index.ts
Expand Up @@ -13,7 +13,6 @@ import yargs from 'yargs';
import {Config} from '@jest/types';
import {JestEnvironment} from '@jest/environment';
import {Console, setGlobal} from 'jest-util';
// @ts-ignore: Not migrated to TS
import {validateCLIOptions} from 'jest-validate';
import {readConfig, deprecationEntries} from 'jest-config';
import {VERSION} from '../version';
Expand All @@ -35,6 +34,7 @@ export function run(cliArgv?: Config.Argv, cliInfo?: Array<string>) {
.version(false)
.options(args.options).argv;

// @ts-ignore: fix this at some point
validateCLIOptions(argv, {...args.options, deprecationEntries});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-runtime/tsconfig.json
Expand Up @@ -4,7 +4,6 @@
"rootDir": "src",
"outDir": "build"
},
// TODO: Missing `jest-validate`
"references": [
{"path": "../jest-config"},
{"path": "../jest-environment"},
Expand All @@ -15,8 +14,9 @@
{"path": "../jest-regex-util"},
{"path": "../jest-resolve"},
{"path": "../jest-snapshot"},
{"path": "../jest-util"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-validate"},
{"path": "../pretty-format"}
]
}
2 changes: 2 additions & 0 deletions packages/jest-validate/package.json
Expand Up @@ -8,7 +8,9 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/types": "^24.1.0",
"camelcase": "^5.0.0",
"chalk": "^2.0.1",
"jest-get-type": "^24.0.0",
Expand Down
Expand Up @@ -6,8 +6,6 @@
*
*/

'use strict';

import validateCLIOptions from '../validateCLIOptions';

test('validates yargs special options', () => {
Expand Down
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
*/

const toString = Object.prototype.toString;
Expand All @@ -23,6 +21,7 @@ function validationConditionSingle(option: any, validOption: any): boolean {
export function getValues(validOption: any) {
if (
Array.isArray(validOption) &&
// @ts-ignore
validOption[MULTIPLE_VALID_OPTIONS_SYMBOL]
) {
return validOption;
Expand All @@ -34,9 +33,11 @@ export function validationCondition(option: any, validOption: any): boolean {
return getValues(validOption).some(e => validationConditionSingle(option, e));
}

// TODO: This should infer the types of its arguments, and return a union type of the types
// See https://github.com/Microsoft/TypeScript/issues/5453
export function multipleValidOptions(...args: Array<any>) {
const options = [...args];
// $FlowFixMe
// @ts-ignore
options[MULTIPLE_VALID_OPTIONS_SYMBOL] = true;
return options;
}
Expand Up @@ -3,19 +3,17 @@
*
* 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 {ValidationOptions} from './types';
import {ValidationOptions} from './types';

import {deprecationWarning} from './deprecated';
import {unknownOptionWarning} from './warnings';
import {errorMessage} from './errors';
import {validationCondition} from './condition';
import {ERROR, DEPRECATION, WARNING} from './utils';

export default ({
const validationOptions: ValidationOptions = {
comment: '',
condition: validationCondition,
deprecate: deprecationWarning,
Expand All @@ -31,4 +29,6 @@ export default ({
warning: WARNING,
},
unknown: unknownOptionWarning,
}: ValidationOptions);
};

export default validationOptions;
Expand Up @@ -3,11 +3,9 @@
*
* 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 {ValidationOptions} from './types';
import {DeprecatedOptions, ValidationOptions} from './types';

import {logValidationWarning, DEPRECATION} from './utils';

Expand All @@ -19,9 +17,9 @@ const deprecationMessage = (message: string, options: ValidationOptions) => {
};

export const deprecationWarning = (
config: Object,
config: {[key: string]: any},
option: string,
deprecatedOptions: Object,
deprecatedOptions: DeprecatedOptions,
options: ValidationOptions,
): boolean => {
if (option in deprecatedOptions) {
Expand Down
Expand Up @@ -3,16 +3,13 @@
*
* 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 {ValidationOptions} from './types';

import chalk from 'chalk';
import getType from 'jest-get-type';
import {formatPrettyObject, ValidationError, ERROR} from './utils';
import {getValues} from './condition';
import {ValidationOptions} from './types';

export const errorMessage = (
option: string,
Expand Down
Expand Up @@ -3,20 +3,18 @@
*
* 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 {ValidationOptions} from './types';
import {ValidationOptions} from './types';

const config: ValidationOptions = {
comment: ' A comment',
condition: (option, validOption) => true,
deprecate: (config, option, deprecatedOptions, options) => false,
condition: () => true,
deprecate: () => false,
deprecatedConfig: {
key: config => {},
key: () => {},
},
error: (option, received, defaultValue, options) => {},
error: () => {},
exampleConfig: {key: 'value', test: 'case'},
recursive: true,
recursiveBlacklist: [],
Expand All @@ -25,7 +23,7 @@ const config: ValidationOptions = {
error: 'Validation Error',
warning: 'Validation Warning',
},
unknown: (config, option, options) => {},
unknown: () => {},
};

export default config;
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 {
Expand All @@ -17,7 +15,7 @@ import validate from './validate';
import validateCLIOptions from './validateCLIOptions';
import {multipleValidOptions} from './condition';

module.exports = {
export = {
ValidationError,
createDidYouMeanMessage,
format,
Expand Down
44 changes: 0 additions & 44 deletions packages/jest-validate/src/types.js

This file was deleted.

44 changes: 44 additions & 0 deletions packages/jest-validate/src/types.ts
@@ -0,0 +1,44 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

type Title = {
deprecation?: string;
error?: string;
warning?: string;
};

export type DeprecatedOptions = Record<string, Function>;

export type ValidationOptions = {
comment?: string;
condition?: (option: any, validOption: any) => boolean;
deprecate?: (
config: {[key: string]: any},
option: string,
deprecatedOptions: DeprecatedOptions,
options: ValidationOptions,
) => boolean;
deprecatedConfig?: DeprecatedOptions;
error?: (
option: string,
received: any,
defaultValue: any,
options: ValidationOptions,
path?: Array<string>,
) => void;
exampleConfig: {[key: string]: any};
recursive?: boolean;
recursiveBlacklist?: Array<string>;
title?: Title;
unknown?: (
config: {[key: string]: any},
exampleConfig: {[key: string]: any},
option: string,
options: ValidationOptions,
path?: Array<string>,
) => void;
};

0 comments on commit 6624022

Please sign in to comment.