Skip to content

Commit 7906f1d

Browse files
authored
chore: migrate jest-matcher-util to TypeScript (#7835)
* chore: migrate jest-matcher-util to TypeScript * link to PR * unknown and fix flow
1 parent 080998e commit 7906f1d

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `[jest-diff]`: Migrate to TypeScript ([#7824](https://github.com/facebook/jest/pull/7824))
1717
- `[jest-leak-detector]`: Migrate to TypeScript ([#7825](https://github.com/facebook/jest/pull/7825))
1818
- `[jest-changed-files]`: Migrate to TypeScript ([#7827](https://github.com/facebook/jest/pull/7827))
19+
- `[jest-matcher-utils]`: Migrate to TypeScript ([#7835](https://github.com/facebook/jest/pull/7835))
1920

2021
### Performance
2122

packages/jest-circus/src/formatNodeAssertErrors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import type {DiffOptions} from 'jest-diff';
1212
import type {Event, State} from 'types/Circus';
1313

14+
// $FlowFixMe: Converted to TS
1415
import {diff, printExpected, printReceived} from 'jest-matcher-utils';
1516
import chalk from 'chalk';
1617
// $FlowFixMe: Converted to TS

packages/jest-matcher-utils/src/__tests__/index.test.js renamed to packages/jest-matcher-utils/src/__tests__/index.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('.stringify()', () => {
3737
});
3838

3939
test('circular references', () => {
40-
const a = {};
40+
const a: any = {};
4141
a.a = a;
4242
expect(stringify(a)).toBe('{"a": [Circular]}');
4343
});
@@ -75,8 +75,8 @@ describe('.stringify()', () => {
7575
});
7676

7777
test('reduces maxDepth if stringifying very large objects', () => {
78-
const big = {a: 1, b: {}};
79-
const small = {a: 1, b: {}};
78+
const big: any = {a: 1, b: {}};
79+
const small: any = {a: 1, b: {}};
8080
for (let i = 0; i < 10000; i += 1) {
8181
big.b[i] = 'test';
8282
}
@@ -93,18 +93,21 @@ describe('.stringify()', () => {
9393
describe('.ensureNumbers()', () => {
9494
test('dont throw error when variables are numbers', () => {
9595
expect(() => {
96+
// @ts-ignore
9697
ensureNumbers(1, 2);
9798
}).not.toThrow();
9899
});
99100

100101
test('throws error when expected is not a number', () => {
101102
expect(() => {
103+
// @ts-ignore
102104
ensureNumbers(1, 'not_a_number');
103105
}).toThrowErrorMatchingSnapshot();
104106
});
105107

106108
test('throws error when received is not a number', () => {
107109
expect(() => {
110+
// @ts-ignore
108111
ensureNumbers('not_a_number', 3);
109112
}).toThrowErrorMatchingSnapshot();
110113
});
@@ -113,6 +116,7 @@ describe('.ensureNumbers()', () => {
113116
describe('.ensureNoExpected()', () => {
114117
test('dont throw error when undefined', () => {
115118
expect(() => {
119+
// @ts-ignore
116120
ensureNoExpected(undefined);
117121
}).not.toThrow();
118122
});

packages/jest-matcher-utils/src/index.js renamed to packages/jest-matcher-utils/src/index.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
import type {MatcherHintOptions} from 'types/Matchers';
11-
128
import chalk from 'chalk';
139
import jestDiff from 'jest-diff';
1410
import getType from 'jest-get-type';
@@ -31,6 +27,14 @@ const PLUGINS = [
3127
AsymmetricMatcher,
3228
];
3329

30+
export type MatcherHintOptions = {
31+
comment?: string;
32+
isDirectExpectCall?: boolean;
33+
isNot?: boolean;
34+
promise?: string;
35+
secondArgument?: string;
36+
};
37+
3438
export const EXPECTED_COLOR = chalk.green;
3539
export const RECEIVED_COLOR = chalk.red;
3640
const DIM_COLOR = chalk.dim;
@@ -60,7 +64,7 @@ export const SUGGEST_TO_CONTAIN_EQUAL = chalk.dim(
6064
'Looks like you wanted to test for object/array equality with the stricter `toContain` matcher. You probably need to use `toContainEqual` instead.',
6165
);
6266

63-
export const stringify = (object: any, maxDepth?: number = 10): string => {
67+
export const stringify = (object: unknown, maxDepth: number = 10): string => {
6468
const MAX_LENGTH = 10000;
6569
let result;
6670

@@ -87,15 +91,15 @@ export const stringify = (object: any, maxDepth?: number = 10): string => {
8791
export const highlightTrailingWhitespace = (text: string): string =>
8892
text.replace(/\s+$/gm, chalk.inverse('$&'));
8993

90-
export const printReceived = (object: any) =>
94+
export const printReceived = (object: unknown) =>
9195
RECEIVED_COLOR(highlightTrailingWhitespace(stringify(object)));
92-
export const printExpected = (value: any) =>
96+
export const printExpected = (value: unknown) =>
9397
EXPECTED_COLOR(highlightTrailingWhitespace(stringify(value)));
9498

9599
export const printWithType = (
96100
name: string, // 'Expected' or 'Received'
97-
value: any,
98-
print: (value: any) => string, // printExpected or printReceived
101+
value: unknown,
102+
print: (value: unknown) => string, // printExpected or printReceived
99103
) => {
100104
const type = getType(value);
101105
const hasType =
@@ -107,7 +111,7 @@ export const printWithType = (
107111
};
108112

109113
export const ensureNoExpected = (
110-
expected: any,
114+
expected: unknown,
111115
matcherName: string,
112116
options?: MatcherHintOptions,
113117
) => {
@@ -126,7 +130,7 @@ export const ensureNoExpected = (
126130
}
127131
};
128132

129-
export const ensureActualIsNumber = (actual: any, matcherName: string) => {
133+
export const ensureActualIsNumber = (actual: unknown, matcherName: string) => {
130134
matcherName || (matcherName = 'This matcher');
131135
if (typeof actual !== 'number') {
132136
throw new Error(
@@ -139,7 +143,10 @@ export const ensureActualIsNumber = (actual: any, matcherName: string) => {
139143
}
140144
};
141145

142-
export const ensureExpectedIsNumber = (expected: any, matcherName: string) => {
146+
export const ensureExpectedIsNumber = (
147+
expected: unknown,
148+
matcherName: string,
149+
) => {
143150
matcherName || (matcherName = 'This matcher');
144151
if (typeof expected !== 'number') {
145152
throw new Error(
@@ -153,8 +160,8 @@ export const ensureExpectedIsNumber = (expected: any, matcherName: string) => {
153160
};
154161

155162
export const ensureNumbers = (
156-
actual: any,
157-
expected: any,
163+
actual: unknown,
164+
expected: unknown,
158165
matcherName: string,
159166
) => {
160167
ensureActualIsNumber(actual, matcherName);
@@ -164,7 +171,7 @@ export const ensureNumbers = (
164171
// Sometimes, e.g. when comparing two numbers, the output from jest-diff
165172
// does not contain more information than the `Expected:` / `Received:` already gives.
166173
// In those cases, we do not print a diff to make the output shorter and not redundant.
167-
const shouldPrintDiff = (actual: any, expected: any) => {
174+
const shouldPrintDiff = (actual: unknown, expected: unknown) => {
168175
if (typeof actual === 'number' && typeof expected === 'number') {
169176
return false;
170177
}
@@ -184,7 +191,7 @@ export const pluralize = (word: string, count: number) =>
184191
// return function which given each string, returns the label:
185192
// string, colon, space, and enough padding spaces to align the value.
186193

187-
type PrintLabel = string => string;
194+
type PrintLabel = (string: string) => string;
188195

189196
export const getLabelPrinter = (...strings: Array<string>): PrintLabel => {
190197
const maxLength = strings.reduce(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build"
6+
},
7+
"references": [
8+
{"path": "../jest-diff"},
9+
{"path": "../jest-get-type"},
10+
{"path": "../pretty-format"}
11+
]
12+
}

0 commit comments

Comments
 (0)