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-validate to TypeScript #7991

Merged
merged 2 commits into from Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -69,6 +69,7 @@
- `[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-repl]`: Migrate to TypeScript ([#8000](https://github.com/facebook/jest/pull/8000))
- `[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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeysal do you know how to type this? Essentially, the return-type should be one a union of the types of all arguments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand, wouldn't that typing be a lie? multipleValidOptions doesn't return one of the argument (in that case the return type would be the union of them), but an array of all of them.

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
5 changes: 3 additions & 2 deletions packages/jest-config/tsconfig.json
Expand Up @@ -4,8 +4,8 @@
"rootDir": "src",
"outDir": "build"
},
// TODO: This is missing `jest-validate`, in addition to and `jest-jasmine2`,
// but those are just `require.resolve`d, so no real use for their types
// TODO: This is missing `jest-jasmine2`, but that is just
// `require.resolve`d, so no real use for its types
"references": [
{"path": "../jest-environment-jsdom"},
{"path": "../jest-environment-node"},
Expand All @@ -14,6 +14,7 @@
{"path": "../jest-resolve"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-validate"},
{"path": "../pretty-format"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-repl/src/cli/index.ts
Expand Up @@ -9,7 +9,6 @@

import Runtime from 'jest-runtime';
import yargs from 'yargs';
// @ts-ignore: Wait for jest-validate to get migrated
import {validateCLIOptions} from 'jest-validate';
import {deprecationEntries} from 'jest-config';
import * as args from './args';
Expand All @@ -21,6 +20,7 @@ const REPL_SCRIPT = require.resolve('./repl.js');
export = function() {
const argv = yargs.usage(args.usage).options(args.options).argv;

// @ts-ignore: not the same arguments
validateCLIOptions(argv, {...args.options, deprecationEntries});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error here is nuts, I gave up :p


argv._ = [REPL_SCRIPT];
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-repl/tsconfig.json
Expand Up @@ -8,8 +8,7 @@
{"path": "../jest-config"},
{"path": "../jest-runtime"},
{"path": "../jest-transform"},
{"path": "../jest-types"}
// TODO: Wait for this to get migrated
// {"path": "../jest-validate"}
{"path": "../jest-types"},
{"path": "../jest-validate"}
]
}
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.