Skip to content

Commit 4a8072d

Browse files
feat: add Jest matchers utils (#1464)
* feat: add formatMessage * feat: add TS extends * chore: remove old Jest Native tests
1 parent 8987414 commit 4a8072d

File tree

6 files changed

+49
-99
lines changed

6 files changed

+49
-99
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"@babel/preset-typescript": "^7.18.6",
4343
"@callstack/eslint-config": "^13.0.1",
4444
"@relmify/jest-serializer-strip-ansi": "^1.0.2",
45-
"@testing-library/jest-native": "^5.4.0",
4645
"@types/jest": "^29.2.3",
4746
"@types/react": "^18.2.15",
4847
"@types/react-test-renderer": "^18.0.0",

src/__tests__/jest-native.test.tsx

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/matchers/extend-expect.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { TextMatch, TextMatchOptions } from '../matches';
2+
3+
export interface JestNativeMatchers<R> {
4+
toBeOnTheScreen(): R;
5+
toHaveTextContent(text: TextMatch, options?: TextMatchOptions): R;
6+
}
7+
8+
// Implicit Jest global `expect`.
9+
declare global {
10+
namespace jest {
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12+
interface Matchers<R, T = {}> extends JestNativeMatchers<R> {}
13+
}
14+
}
15+
16+
// Explicit `@jest/globals` `expect` matchers.
17+
declare module '@jest/expect' {
18+
interface Matchers<R extends void | Promise<void>>
19+
extends JestNativeMatchers<R> {}
20+
}

src/matchers/extend-expect.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference path="./extend-expect.d.ts" />
2+
13
import { toBeOnTheScreen } from './to-be-on-the-screen';
24

35
expect.extend({

src/matchers/utils.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import {
3+
EXPECTED_COLOR,
34
RECEIVED_COLOR,
45
matcherHint,
56
printWithType,
67
printReceived,
8+
stringify,
79
} from 'jest-matcher-utils';
810
import prettyFormat, { plugins } from 'pretty-format';
911
import redent from 'redent';
@@ -89,3 +91,25 @@ export function formatElement(element: ReactTestInstance | null) {
8991
2
9092
);
9193
}
94+
95+
export function formatMessage(
96+
matcher: string,
97+
expectedLabel: string,
98+
expectedValue: string | RegExp,
99+
receivedLabel: string,
100+
receivedValue: string | null
101+
) {
102+
return [
103+
`${matcher}\n`,
104+
`${expectedLabel}:\n${EXPECTED_COLOR(
105+
redent(formatValue(expectedValue), 2)
106+
)}`,
107+
`${receivedLabel}:\n${RECEIVED_COLOR(
108+
redent(formatValue(receivedValue), 2)
109+
)}`,
110+
].join('\n');
111+
}
112+
113+
function formatValue(value: unknown) {
114+
return typeof value === 'string' ? value : stringify(value);
115+
}

yarn.lock

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,17 +1832,6 @@
18321832
dependencies:
18331833
"@sinonjs/commons" "^3.0.0"
18341834

1835-
"@testing-library/jest-native@^5.4.0":
1836-
version "5.4.2"
1837-
resolved "https://registry.yarnpkg.com/@testing-library/jest-native/-/jest-native-5.4.2.tgz#6b0c987cc57f8d900763e763025d00d26ccbc85f"
1838-
integrity sha512-Vo/CE1uvCVH1H8YPoOEXLXVsm+BjzSQTq35+wkri1fr0O5D+A2WZ+m3ni5g6f1OCzNKNGIAHmisBEWkDs1P1mw==
1839-
dependencies:
1840-
chalk "^4.1.2"
1841-
jest-diff "^29.0.1"
1842-
jest-matcher-utils "^29.0.1"
1843-
pretty-format "^29.0.3"
1844-
redent "^3.0.0"
1845-
18461835
"@types/babel__core@^7.1.14":
18471836
version "7.20.1"
18481837
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.20.1.tgz#916ecea274b0c776fec721e333e55762d3a9614b"
@@ -4654,7 +4643,7 @@ jest-config@^29.6.1:
46544643
slash "^3.0.0"
46554644
strip-json-comments "^3.1.1"
46564645

4657-
jest-diff@^29.0.1, jest-diff@^29.6.1:
4646+
jest-diff@^29.6.1:
46584647
version "29.6.1"
46594648
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-29.6.1.tgz#13df6db0a89ee6ad93c747c75c85c70ba941e545"
46604649
integrity sha512-FsNCvinvl8oVxpNLttNQX7FAq7vR+gMDGj90tiP7siWw1UdakWUGqrylpsYrpvj908IYckm5Y0Q7azNAozU1Kg==
@@ -4741,7 +4730,7 @@ jest-leak-detector@^29.6.1:
47414730
jest-get-type "^29.4.3"
47424731
pretty-format "^29.6.1"
47434732

4744-
jest-matcher-utils@^29.0.1, jest-matcher-utils@^29.6.1:
4733+
jest-matcher-utils@^29.6.1:
47454734
version "29.6.1"
47464735
resolved "https://registry.yarnpkg.com/jest-matcher-utils/-/jest-matcher-utils-29.6.1.tgz#6c60075d84655d6300c5d5128f46531848160b53"
47474736
integrity sha512-SLaztw9d2mfQQKHmJXKM0HCbl2PPVld/t9Xa6P9sgiExijviSp7TnZZpw2Fpt+OI3nwUO/slJbOfzfUMKKC5QA==
@@ -6290,7 +6279,7 @@ pretty-format@^26.5.2, pretty-format@^26.6.2:
62906279
ansi-styles "^4.0.0"
62916280
react-is "^17.0.1"
62926281

6293-
pretty-format@^29.0.0, pretty-format@^29.0.3, pretty-format@^29.6.1:
6282+
pretty-format@^29.0.0, pretty-format@^29.6.1:
62946283
version "29.6.1"
62956284
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-29.6.1.tgz#ec838c288850b7c4f9090b867c2d4f4edbfb0f3e"
62966285
integrity sha512-7jRj+yXO0W7e4/tSJKoR7HRIHLPPjtNaUGG2xxKQnGvPNRkgWcQ0AZX6P4KBRJN4FcTBWb3sa7DVUJmocYuoog==

0 commit comments

Comments
 (0)