Skip to content

Commit 50e6a8a

Browse files
authored
feat: support custom equality testers (#705)
Custom equality testers were added to jest in version 29.4.0. This was done in a way that requires custom matchers to explicitly pass them to use the equals method. Adding this to all relevant matchers so they will respect any custom equality testers that were added to jest.
1 parent d3dfb7e commit 50e6a8a

File tree

54 files changed

+590
-22
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+590
-22
lines changed

src/matchers/toBeEmpty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export function toBeEmpty(actual: unknown) {
33
const { printReceived, matcherHint } = this.utils;
44

55
// @ts-expect-error OK to have implicit any for this.equals
6-
const pass = this.equals({}, actual) || isEmptyIterable(actual);
6+
const pass = this.equals({}, actual, this.customTesters) || isEmptyIterable(actual);
77

88
return {
99
pass,

src/matchers/toBeOneOf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function toBeOneOf<E = unknown>(actual: unknown, expected: readonly E[])
55
const { printReceived, printExpected, matcherHint } = this.utils;
66

77
// @ts-expect-error OK to have implicit any for this.equals
8-
const pass = contains(this.equals, expected, actual);
8+
const pass = contains((a, b) => this.equals(a, b, this.customTesters), expected, actual);
99

1010
return {
1111
pass,

src/matchers/toContainAllEntries.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ export function toContainAllEntries<E = unknown>(
1515
actual !== null &&
1616
!Array.isArray(actual) &&
1717
expected.length == Object.keys(actual as Record<string, unknown>).length &&
18-
// @ts-expect-error containsEntry takes an any type
19-
expected.every(entry => containsEntry(this.equals, actual, entry as [any, any]));
18+
expected.every(entry =>
19+
// @ts-expect-error containsEntry takes an any type
20+
containsEntry((a, b) => this.equals(a, b, this.customTesters), actual, entry as [any, any]),
21+
);
2022

2123
return {
2224
pass,

src/matchers/toContainAllKeys.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ export function toContainAllKeys<E = unknown>(actual: unknown, expected: readonl
77
let pass = false;
88
if (typeof actual === 'object' && actual !== null && !Array.isArray(actual)) {
99
const objectKeys = Object.keys(actual as Record<string, unknown>);
10-
// @ts-expect-error OK to have implicit any for this.equals
11-
pass = objectKeys.length === expected.length && expected.every(key => contains(this.equals, objectKeys, key));
10+
pass =
11+
objectKeys.length === expected.length &&
12+
// @ts-expect-error OK to have implicit any for this.equals
13+
expected.every(key => contains((a, b) => this.equals(a, b, this.customTesters), objectKeys, key));
1214
}
1315

1416
return {

src/matchers/toContainAllValues.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ export function toContainAllValues<E = unknown>(actual: unknown, expected: reado
88
if (typeof actual === 'object' && actual !== null && !Array.isArray(actual)) {
99
const values = Object.keys(actual as Record<string, unknown>).map(k => (actual as Record<string, unknown>)[k]);
1010
pass =
11+
values.length === expected.length &&
1112
// @ts-expect-error OK to have implicit any for this.equals
12-
values.length === expected.length && values.every(objectValue => contains(this.equals, expected, objectValue));
13+
values.every(objectValue => contains((a, b) => this.equals(a, b, this.customTesters), expected, objectValue));
1314
}
1415

1516
return {

src/matchers/toContainAnyEntries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function toContainAnyEntries<E = unknown>(
1414
(actual as Record<string, unknown>)[k],
1515
]);
1616
// @ts-expect-error OK to have implicit any for this.equals
17-
pass = expected.some(entry => contains(this.equals, entries, entry));
17+
pass = expected.some(entry => contains((a, b) => this.equals(a, b, this.customTesters), entries, entry));
1818
}
1919

2020
return {

src/matchers/toContainAnyValues.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function toContainAnyValues<E = unknown>(actual: unknown, expected: reado
1010
k => (actual as Record<string, unknown>)[k],
1111
);
1212
// @ts-expect-error OK to have implicit any for this.equals
13-
pass = expected.some(value => contains(this.equals, objectValues, value));
13+
pass = expected.some(value => contains((a, b) => this.equals(a, b, this.customTesters), objectValues, value));
1414
}
1515

1616
return {

src/matchers/toContainEntries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function toContainEntries<E = unknown>(actual: unknown, expected: readonl
55
const { printReceived, printExpected, matcherHint } = this.utils;
66

77
// @ts-expect-error containsEntry takes an any type
8-
const pass = expected.every(entry => containsEntry(this.equals, actual, entry));
8+
const pass = expected.every(entry => containsEntry((a, b) => this.equals(a, b, this.customTesters), actual, entry));
99

1010
return {
1111
pass,

src/matchers/toContainEntry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function toContainEntry<E = unknown>(actual: unknown, expected: readonly
55
const { printReceived, printExpected, matcherHint } = this.utils;
66

77
// @ts-expect-error containsEntry takes an any type
8-
const pass = containsEntry(this.equals, actual, expected);
8+
const pass = containsEntry((a, b) => this.equals(a, b, this.customTesters), actual, expected);
99

1010
return {
1111
pass,

src/matchers/toContainValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function toContainValue<E = unknown>(actual: unknown, expected: E) {
88
if (typeof actual === 'object' && actual !== null && !Array.isArray(actual)) {
99
const values = Object.keys(actual as Record<string, unknown>).map(k => (actual as Record<string, unknown>)[k]);
1010
// @ts-expect-error OK to have implicit any for this.equals
11-
pass = contains(this.equals, values, expected);
11+
pass = contains((a, b) => this.equals(a, b, this.customTesters), values, expected);
1212
}
1313

1414
return {

0 commit comments

Comments
 (0)