diff --git a/CHANGELOG.md b/CHANGELOG.md index 34b0e950f185..0050a9f04365 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - `[jest-diff]`: Migrate to TypeScript ([#7824](https://github.com/facebook/jest/pull/7824)) - `[jest-leak-detector]`: Migrate to TypeScript ([#7825](https://github.com/facebook/jest/pull/7825)) - `[jest-changed-files]`: Migrate to TypeScript ([#7827](https://github.com/facebook/jest/pull/7827)) +- `[jest-matcher-utils]`: Migrate to TypeScript ([#7835](https://github.com/facebook/jest/pull/7835)) ### Performance diff --git a/packages/jest-circus/src/formatNodeAssertErrors.js b/packages/jest-circus/src/formatNodeAssertErrors.js index e7f685f852f7..ef84986451b6 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.js +++ b/packages/jest-circus/src/formatNodeAssertErrors.js @@ -11,6 +11,7 @@ import type {DiffOptions} from 'jest-diff'; import type {Event, State} from 'types/Circus'; +// $FlowFixMe: Converted to TS import {diff, printExpected, printReceived} from 'jest-matcher-utils'; import chalk from 'chalk'; // $FlowFixMe: Converted to TS diff --git a/packages/jest-matcher-utils/src/__tests__/__snapshots__/index.test.js.snap b/packages/jest-matcher-utils/src/__tests__/__snapshots__/index.test.ts.snap similarity index 100% rename from packages/jest-matcher-utils/src/__tests__/__snapshots__/index.test.js.snap rename to packages/jest-matcher-utils/src/__tests__/__snapshots__/index.test.ts.snap diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.js b/packages/jest-matcher-utils/src/__tests__/index.test.ts similarity index 97% rename from packages/jest-matcher-utils/src/__tests__/index.test.js rename to packages/jest-matcher-utils/src/__tests__/index.test.ts index 703ab50fa0d0..a78976c11a3c 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.js +++ b/packages/jest-matcher-utils/src/__tests__/index.test.ts @@ -37,7 +37,7 @@ describe('.stringify()', () => { }); test('circular references', () => { - const a = {}; + const a: any = {}; a.a = a; expect(stringify(a)).toBe('{"a": [Circular]}'); }); @@ -75,8 +75,8 @@ describe('.stringify()', () => { }); test('reduces maxDepth if stringifying very large objects', () => { - const big = {a: 1, b: {}}; - const small = {a: 1, b: {}}; + const big: any = {a: 1, b: {}}; + const small: any = {a: 1, b: {}}; for (let i = 0; i < 10000; i += 1) { big.b[i] = 'test'; } @@ -93,18 +93,21 @@ describe('.stringify()', () => { describe('.ensureNumbers()', () => { test('dont throw error when variables are numbers', () => { expect(() => { + // @ts-ignore ensureNumbers(1, 2); }).not.toThrow(); }); test('throws error when expected is not a number', () => { expect(() => { + // @ts-ignore ensureNumbers(1, 'not_a_number'); }).toThrowErrorMatchingSnapshot(); }); test('throws error when received is not a number', () => { expect(() => { + // @ts-ignore ensureNumbers('not_a_number', 3); }).toThrowErrorMatchingSnapshot(); }); @@ -113,6 +116,7 @@ describe('.ensureNumbers()', () => { describe('.ensureNoExpected()', () => { test('dont throw error when undefined', () => { expect(() => { + // @ts-ignore ensureNoExpected(undefined); }).not.toThrow(); }); diff --git a/packages/jest-matcher-utils/src/index.js b/packages/jest-matcher-utils/src/index.ts similarity index 90% rename from packages/jest-matcher-utils/src/index.js rename to packages/jest-matcher-utils/src/index.ts index a7b040e867a5..ef5121d7210a 100644 --- a/packages/jest-matcher-utils/src/index.js +++ b/packages/jest-matcher-utils/src/index.ts @@ -3,12 +3,8 @@ * * 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 {MatcherHintOptions} from 'types/Matchers'; - import chalk from 'chalk'; import jestDiff from 'jest-diff'; import getType from 'jest-get-type'; @@ -31,6 +27,14 @@ const PLUGINS = [ AsymmetricMatcher, ]; +export type MatcherHintOptions = { + comment?: string; + isDirectExpectCall?: boolean; + isNot?: boolean; + promise?: string; + secondArgument?: string; +}; + export const EXPECTED_COLOR = chalk.green; export const RECEIVED_COLOR = chalk.red; const DIM_COLOR = chalk.dim; @@ -60,7 +64,7 @@ export const SUGGEST_TO_CONTAIN_EQUAL = chalk.dim( 'Looks like you wanted to test for object/array equality with the stricter `toContain` matcher. You probably need to use `toContainEqual` instead.', ); -export const stringify = (object: any, maxDepth?: number = 10): string => { +export const stringify = (object: unknown, maxDepth: number = 10): string => { const MAX_LENGTH = 10000; let result; @@ -87,15 +91,15 @@ export const stringify = (object: any, maxDepth?: number = 10): string => { export const highlightTrailingWhitespace = (text: string): string => text.replace(/\s+$/gm, chalk.inverse('$&')); -export const printReceived = (object: any) => +export const printReceived = (object: unknown) => RECEIVED_COLOR(highlightTrailingWhitespace(stringify(object))); -export const printExpected = (value: any) => +export const printExpected = (value: unknown) => EXPECTED_COLOR(highlightTrailingWhitespace(stringify(value))); export const printWithType = ( name: string, // 'Expected' or 'Received' - value: any, - print: (value: any) => string, // printExpected or printReceived + value: unknown, + print: (value: unknown) => string, // printExpected or printReceived ) => { const type = getType(value); const hasType = @@ -107,7 +111,7 @@ export const printWithType = ( }; export const ensureNoExpected = ( - expected: any, + expected: unknown, matcherName: string, options?: MatcherHintOptions, ) => { @@ -126,7 +130,7 @@ export const ensureNoExpected = ( } }; -export const ensureActualIsNumber = (actual: any, matcherName: string) => { +export const ensureActualIsNumber = (actual: unknown, matcherName: string) => { matcherName || (matcherName = 'This matcher'); if (typeof actual !== 'number') { throw new Error( @@ -139,7 +143,10 @@ export const ensureActualIsNumber = (actual: any, matcherName: string) => { } }; -export const ensureExpectedIsNumber = (expected: any, matcherName: string) => { +export const ensureExpectedIsNumber = ( + expected: unknown, + matcherName: string, +) => { matcherName || (matcherName = 'This matcher'); if (typeof expected !== 'number') { throw new Error( @@ -153,8 +160,8 @@ export const ensureExpectedIsNumber = (expected: any, matcherName: string) => { }; export const ensureNumbers = ( - actual: any, - expected: any, + actual: unknown, + expected: unknown, matcherName: string, ) => { ensureActualIsNumber(actual, matcherName); @@ -164,7 +171,7 @@ export const ensureNumbers = ( // Sometimes, e.g. when comparing two numbers, the output from jest-diff // does not contain more information than the `Expected:` / `Received:` already gives. // In those cases, we do not print a diff to make the output shorter and not redundant. -const shouldPrintDiff = (actual: any, expected: any) => { +const shouldPrintDiff = (actual: unknown, expected: unknown) => { if (typeof actual === 'number' && typeof expected === 'number') { return false; } @@ -184,7 +191,7 @@ export const pluralize = (word: string, count: number) => // return function which given each string, returns the label: // string, colon, space, and enough padding spaces to align the value. -type PrintLabel = string => string; +type PrintLabel = (string: string) => string; export const getLabelPrinter = (...strings: Array): PrintLabel => { const maxLength = strings.reduce( diff --git a/packages/jest-matcher-utils/tsconfig.json b/packages/jest-matcher-utils/tsconfig.json new file mode 100644 index 000000000000..2248b5ff7fa1 --- /dev/null +++ b/packages/jest-matcher-utils/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "references": [ + {"path": "../jest-diff"}, + {"path": "../jest-get-type"}, + {"path": "../pretty-format"} + ] +}