From ccea646e76efd73654468ac32b3cb3a88778f7f4 Mon Sep 17 00:00:00 2001 From: Josh Rosenstein Date: Wed, 15 May 2019 03:24:54 -0400 Subject: [PATCH] General Type Maintenance (#8462) --- packages/babel-plugin-jest-hoist/src/index.ts | 7 +-- packages/expect/src/asymmetricMatchers.ts | 6 +-- packages/expect/src/index.ts | 9 ++-- packages/expect/src/jasmineUtils.ts | 3 +- packages/expect/src/jestMatchersObject.ts | 2 +- packages/expect/src/types.ts | 10 ++-- packages/expect/src/utils.ts | 4 +- .../jest-circus/src/formatNodeAssertErrors.ts | 4 +- .../jest-cli/src/init/generate_config_file.ts | 2 +- packages/jest-cli/src/init/types.ts | 2 +- packages/jest-config/src/setFromArgv.ts | 2 +- packages/jest-config/src/utils.ts | 2 +- packages/jest-core/src/FailedTestsCache.ts | 2 +- packages/jest-core/src/TestScheduler.ts | 4 +- .../jest-core/src/getNoTestFoundVerbose.ts | 2 +- packages/jest-docblock/src/index.ts | 2 +- packages/jest-each/src/table/template.ts | 47 ++++++++++++++++--- packages/jest-environment/src/index.ts | 2 +- .../jest-fake-timers/src/jestFakeTimers.ts | 4 +- packages/jest-haste-map/src/ModuleMap.ts | 2 +- .../src/assertionErrorMessage.ts | 4 +- packages/jest-jasmine2/src/jasmine/Env.ts | 4 +- .../src/jasmine/JsApiReporter.ts | 4 +- .../jest-jasmine2/src/jasmine/createSpy.ts | 3 +- .../jest-jasmine2/src/jasmine/spyRegistry.ts | 4 +- packages/jest-jasmine2/src/types.ts | 3 +- packages/jest-mock/src/index.ts | 6 +-- .../src/__tests__/dependency_resolver.test.ts | 2 +- packages/jest-resolve/src/index.ts | 2 +- packages/jest-runtime/src/index.ts | 10 ++-- .../jest-snapshot/src/inline_snapshots.ts | 8 ++-- packages/jest-snapshot/src/types.ts | 2 +- packages/jest-snapshot/src/utils.ts | 2 +- packages/jest-source-map/src/types.ts | 2 +- packages/jest-types/src/Config.ts | 2 +- packages/jest-util/src/deepCyclicCopy.ts | 2 +- packages/jest-validate/src/deprecated.ts | 2 +- packages/jest-validate/src/types.ts | 8 ++-- packages/jest-validate/src/validate.ts | 6 +-- packages/jest-validate/src/warnings.ts | 4 +- packages/jest-worker/src/Farm.ts | 2 +- packages/pretty-format/src/collections.ts | 10 +--- packages/pretty-format/src/index.ts | 12 +++-- .../src/plugins/ReactTestComponent.ts | 2 +- 44 files changed, 126 insertions(+), 98 deletions(-) diff --git a/packages/babel-plugin-jest-hoist/src/index.ts b/packages/babel-plugin-jest-hoist/src/index.ts index 6ea02185ca98..81d0a4cefc65 100644 --- a/packages/babel-plugin-jest-hoist/src/index.ts +++ b/packages/babel-plugin-jest-hoist/src/index.ts @@ -88,9 +88,10 @@ const IDVisitor = { blacklist: ['TypeAnnotation', 'TSTypeAnnotation', 'TSTypeReference'], }; -const FUNCTIONS: { - [key: string]: (args: Array) => boolean; -} = Object.create(null); +const FUNCTIONS: Record< + string, + (args: Array) => boolean +> = Object.create(null); FUNCTIONS.mock = (args: Array) => { if (args.length === 1) { diff --git a/packages/expect/src/asymmetricMatchers.ts b/packages/expect/src/asymmetricMatchers.ts index f9907882de10..9ff897315936 100644 --- a/packages/expect/src/asymmetricMatchers.ts +++ b/packages/expect/src/asymmetricMatchers.ts @@ -158,8 +158,8 @@ class ObjectContaining extends AsymmetricMatcher> { for (const property in this.sample) { if ( hasProperty(other, property) && - equals((this.sample as any)[property], other[property]) && - !emptyObject((this.sample as any)[property]) && + equals(this.sample[property], other[property]) && + !emptyObject(this.sample[property]) && !emptyObject(other[property]) ) { return false; @@ -171,7 +171,7 @@ class ObjectContaining extends AsymmetricMatcher> { for (const property in this.sample) { if ( !hasProperty(other, property) || - !equals((this.sample as any)[property], other[property]) + !equals(this.sample[property], other[property]) ) { return false; } diff --git a/packages/expect/src/index.ts b/packages/expect/src/index.ts index ea42b38998bf..e3fbcc70282c 100644 --- a/packages/expect/src/index.ts +++ b/packages/expect/src/index.ts @@ -299,7 +299,7 @@ const makeThrowingMatcher = ( const handlError = (error: Error) => { if ( - (matcher as any)[INTERNAL_MATCHER_FLAG] === true && + matcher[INTERNAL_MATCHER_FLAG] === true && !(error instanceof JestAssertionError) && error.name !== 'PrettyFormatPluginError' && // Guard for some environments (browsers) that do not support this feature. @@ -314,10 +314,7 @@ const makeThrowingMatcher = ( let potentialResult: ExpectationResult; try { - potentialResult = matcher.apply( - matcherContext, - ([actual] as any).concat(args), - ); + potentialResult = matcher.call(matcherContext, actual, ...args); if (isPromise(potentialResult)) { const asyncResult = potentialResult as AsyncExpectationResult; @@ -340,7 +337,7 @@ const makeThrowingMatcher = ( }; expect.extend = (matchers: MatchersObject): void => - setMatchers(matchers, false, expect as any); + setMatchers(matchers, false, expect); expect.anything = anything; expect.any = any; diff --git a/packages/expect/src/jasmineUtils.ts b/packages/expect/src/jasmineUtils.ts index c3a9e70505a8..1c80689d0bc8 100644 --- a/packages/expect/src/jasmineUtils.ts +++ b/packages/expect/src/jasmineUtils.ts @@ -208,7 +208,8 @@ function keys( return keys.concat( (Object.getOwnPropertySymbols(o) as Array).filter( symbol => - (Object.getOwnPropertyDescriptor(o, symbol) as any).enumerable, + (Object.getOwnPropertyDescriptor(o, symbol) as PropertyDescriptor) + .enumerable, ), ); })(obj); diff --git a/packages/expect/src/jestMatchersObject.ts b/packages/expect/src/jestMatchersObject.ts index e9563113c219..7a35146558e3 100644 --- a/packages/expect/src/jestMatchersObject.ts +++ b/packages/expect/src/jestMatchersObject.ts @@ -17,7 +17,7 @@ const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object'); // Jest may override the stack trace of Errors thrown by internal matchers. export const INTERNAL_MATCHER_FLAG = Symbol.for('$$jest-internal-matcher'); -if (!(global as any)[JEST_MATCHERS_OBJECT]) { +if (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) { Object.defineProperty(global, JEST_MATCHERS_OBJECT, { value: { matchers: Object.create(null), diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 4d583a74d62a..04f3950cbbf9 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -7,6 +7,7 @@ */ import {Config} from '@jest/types'; import * as jestMatcherUtils from 'jest-matcher-utils'; +import {INTERNAL_MATCHER_FLAG} from './jestMatchersObject'; export type SyncExpectationResult = { pass: boolean; @@ -17,11 +18,10 @@ export type AsyncExpectationResult = Promise; export type ExpectationResult = SyncExpectationResult | AsyncExpectationResult; -export type RawMatcherFn = ( - received: any, - expected: any, - options?: any, -) => ExpectationResult; +export type RawMatcherFn = { + (received: any, expected: any, options?: any): ExpectationResult; + [INTERNAL_MATCHER_FLAG]?: boolean; +}; export type ThrowingMatcherFn = (actual: any) => void; export type PromiseMatcherFn = (actual: any) => Promise; diff --git a/packages/expect/src/utils.ts b/packages/expect/src/utils.ts index 2c0f8837a5a3..f8aa8aec01d3 100644 --- a/packages/expect/src/utils.ts +++ b/packages/expect/src/utils.ts @@ -49,7 +49,7 @@ export const hasOwnProperty = (object: object, key: string) => hasGetterFromConstructor(object, key); export const getPath = ( - object: object, + object: Record, propertyPath: string | Array, ): GetPath => { if (!Array.isArray(propertyPath)) { @@ -59,7 +59,7 @@ export const getPath = ( if (propertyPath.length) { const lastProp = propertyPath.length === 1; const prop = propertyPath[0]; - const newObject = (object as any)[prop]; + const newObject = object[prop]; if (!lastProp && (newObject === null || newObject === undefined)) { // This is not the last prop in the chain. If we keep recursing it will diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index 3ad52ac72117..2084d335fae9 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -20,14 +20,14 @@ interface AssertionErrorWithStack extends AssertionError { stack: string; } -const assertOperatorsMap: {[key: string]: string} = { +const assertOperatorsMap: Record = { '!=': 'notEqual', '!==': 'notStrictEqual', '==': 'equal', '===': 'strictEqual', }; -const humanReadableOperators: {[key: string]: string} = { +const humanReadableOperators: Record = { deepEqual: 'to deeply equal', deepStrictEqual: 'to deeply and strictly equal', equal: 'to be equal', diff --git a/packages/jest-cli/src/init/generate_config_file.ts b/packages/jest-cli/src/init/generate_config_file.ts index 961c4610ce71..f53393d3fbc4 100644 --- a/packages/jest-cli/src/init/generate_config_file.ts +++ b/packages/jest-cli/src/init/generate_config_file.ts @@ -31,7 +31,7 @@ const stringifyOption = ( ); }; -const generateConfigFile = (results: {[key: string]: unknown}): string => { +const generateConfigFile = (results: Record): string => { const {coverage, clearMocks, environment} = results; const overrides: Record = {}; diff --git a/packages/jest-cli/src/init/types.ts b/packages/jest-cli/src/init/types.ts index 81184fb1aabd..90dfeed163ad 100644 --- a/packages/jest-cli/src/init/types.ts +++ b/packages/jest-cli/src/init/types.ts @@ -9,5 +9,5 @@ import {Config} from '@jest/types'; export type ProjectPackageJson = { jest?: Partial; - scripts?: {[key: string]: string}; + scripts?: Record; }; diff --git a/packages/jest-config/src/setFromArgv.ts b/packages/jest-config/src/setFromArgv.ts index b7f90b6de1d4..07428fed59b7 100644 --- a/packages/jest-config/src/setFromArgv.ts +++ b/packages/jest-config/src/setFromArgv.ts @@ -16,7 +16,7 @@ export default function setFromArgv( ): Config.InitialOptions { const argvToOptions = Object.keys(argv) .filter(key => argv[key] !== undefined && specialArgs.indexOf(key) === -1) - .reduce((options: {[key: string]: unknown}, key) => { + .reduce((options: Record, key) => { switch (key) { case 'coverage': options.collectCoverage = argv[key]; diff --git a/packages/jest-config/src/utils.ts b/packages/jest-config/src/utils.ts index 52555438d359..ddf7daaa0ada 100644 --- a/packages/jest-config/src/utils.ts +++ b/packages/jest-config/src/utils.ts @@ -84,7 +84,7 @@ const _replaceRootDirInObject = ( }; type OrArray = T | Array; -type ReplaceRootDirConfigObj = {[key: string]: Config.Path}; +type ReplaceRootDirConfigObj = Record; type ReplaceRootDirConfigValues = | OrArray | OrArray diff --git a/packages/jest-core/src/FailedTestsCache.ts b/packages/jest-core/src/FailedTestsCache.ts index 46848927913d..eb9f4fb4f440 100644 --- a/packages/jest-core/src/FailedTestsCache.ts +++ b/packages/jest-core/src/FailedTestsCache.ts @@ -9,7 +9,7 @@ import {Test} from 'jest-runner'; import {Config} from '@jest/types'; import {TestResult} from '@jest/test-result'; -type TestMap = {[key: string]: {[key: string]: boolean}}; +type TestMap = Record>; export default class FailedTestsCache { private _enabledTestsMap?: TestMap; diff --git a/packages/jest-core/src/TestScheduler.ts b/packages/jest-core/src/TestScheduler.ts index 3c7724b2d3ea..34f5ec44c116 100644 --- a/packages/jest-core/src/TestScheduler.ts +++ b/packages/jest-core/src/TestScheduler.ts @@ -217,7 +217,7 @@ export default class TestScheduler { } private _partitionTests( - testRunners: {[key: string]: TestRunner}, + testRunners: Record, tests: Array, ) { if (Object.keys(testRunners).length > 1) { @@ -327,7 +327,7 @@ export default class TestScheduler { */ private _getReporterProps( reporter: string | Config.ReporterConfig, - ): {path: string; options: {[key: string]: unknown}} { + ): {path: string; options: Record} { if (typeof reporter === 'string') { return {options: this._options, path: reporter}; } else if (Array.isArray(reporter)) { diff --git a/packages/jest-core/src/getNoTestFoundVerbose.ts b/packages/jest-core/src/getNoTestFoundVerbose.ts index 5e0177a9c3d1..80c8db9a6994 100644 --- a/packages/jest-core/src/getNoTestFoundVerbose.ts +++ b/packages/jest-core/src/getNoTestFoundVerbose.ts @@ -17,7 +17,7 @@ export default function getNoTestFoundVerbose( if (key === 'roots' && config.roots.length === 1) { return null; } - const value = (config as {[key: string]: unknown})[key]; + const value = (config as Record)[key]; if (value) { const valueAsString = Array.isArray(value) ? value.join(', ') diff --git a/packages/jest-docblock/src/index.ts b/packages/jest-docblock/src/index.ts index 5bbb279feb0b..ea4228d5db5e 100644 --- a/packages/jest-docblock/src/index.ts +++ b/packages/jest-docblock/src/index.ts @@ -8,7 +8,7 @@ import {EOL} from 'os'; import detectNewline from 'detect-newline'; -type Pragmas = {[key: string]: string | Array}; +type Pragmas = Record>; const commentEndRe = /\*\/$/; const commentStartRe = /^\/\*\*/; diff --git a/packages/jest-each/src/table/template.ts b/packages/jest-each/src/table/template.ts index a618f163bd43..0602d028feee 100644 --- a/packages/jest-each/src/table/template.ts +++ b/packages/jest-each/src/table/template.ts @@ -11,7 +11,7 @@ import {isPrimitive} from 'jest-get-type'; import {Global} from '@jest/types'; import {EachTests} from '../bind'; -type Template = {[key: string]: unknown}; +type Template = Record; type Templates = Array