Skip to content

Commit 8d4f461

Browse files
committed
fix Map equality test
jestjs#4303 added code to check Set equality in an order-independent way, but applied that logic to all iterable objects. This change limits the scope of the special-case to just Set and Map, and corrects the Map case to test that both keys and values are equal.
1 parent c4844a6 commit 8d4f461

3 files changed

Lines changed: 165 additions & 7 deletions

File tree

packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,35 @@ Received:
19541954
<red>[1, 2, 3]</>"
19551955
`;
19561956

1957+
exports[`.toEqual() {pass: false} expect([1, 2]).not.toEqual([1, 2]) 1`] = `
1958+
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
1959+
1960+
Expected value to not equal:
1961+
<green>[1, 2]</>
1962+
Received:
1963+
<red>[1, 2]</>"
1964+
`;
1965+
1966+
exports[`.toEqual() {pass: false} expect([1, 2]).toEqual([2, 1]) 1`] = `
1967+
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
1968+
1969+
Expected value to equal:
1970+
<green>[2, 1]</>
1971+
Received:
1972+
<red>[1, 2]</>
1973+
1974+
Difference:
1975+
1976+
<green>- Expected</>
1977+
<red>+ Received</>
1978+
1979+
<dim> Array [
1980+
<red>+ 1,</>
1981+
<dim> 2,
1982+
<green>- 1,</>
1983+
<dim> ]"
1984+
`;
1985+
19571986
exports[`.toEqual() {pass: false} expect([1, 3]).toEqual(ArrayContaining [1, 2]) 1`] = `
19581987
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
19591988

@@ -1975,6 +2004,34 @@ Difference:
19752004
<dim> ]"
19762005
`;
19772006

2007+
exports[`.toEqual() {pass: false} expect([1]).not.toEqual([1]) 1`] = `
2008+
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
2009+
2010+
Expected value to not equal:
2011+
<green>[1]</>
2012+
Received:
2013+
<red>[1]</>"
2014+
`;
2015+
2016+
exports[`.toEqual() {pass: false} expect([1]).toEqual([2]) 1`] = `
2017+
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
2018+
2019+
Expected value to equal:
2020+
<green>[2]</>
2021+
Received:
2022+
<red>[1]</>
2023+
2024+
Difference:
2025+
2026+
<green>- Expected</>
2027+
<red>+ Received</>
2028+
2029+
<dim> Array [
2030+
<green>- 2,</>
2031+
<red>+ 1,</>
2032+
<dim> ]"
2033+
`;
2034+
19782035
exports[`.toEqual() {pass: false} expect([Function anonymous]).not.toEqual(Any<Function>) 1`] = `
19792036
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
19802037

@@ -2051,6 +2108,15 @@ Received:
20512108
<red>{\\"a\\": 99}</>"
20522109
`;
20532110

2111+
exports[`.toEqual() {pass: false} expect({}).not.toEqual({}) 1`] = `
2112+
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
2113+
2114+
Expected value to not equal:
2115+
<green>{}</>
2116+
Received:
2117+
<red>{}</>"
2118+
`;
2119+
20542120
exports[`.toEqual() {pass: false} expect(0).toEqual(-0) 1`] = `
20552121
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
20562122

@@ -2095,6 +2161,66 @@ Difference:
20952161
Comparing two different types of values. Expected <green>array</> but received <red>number</>."
20962162
`;
20972163

2164+
exports[`.toEqual() {pass: false} expect(Map {"a" => 0}).toEqual(Map {"b" => 0}) 1`] = `
2165+
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
2166+
2167+
Expected value to equal:
2168+
<green>Map {\\"b\\" => 0}</>
2169+
Received:
2170+
<red>Map {\\"a\\" => 0}</>
2171+
2172+
Difference:
2173+
2174+
<green>- Expected</>
2175+
<red>+ Received</>
2176+
2177+
<dim> Map {
2178+
<green>- \\"b\\" => 0,</>
2179+
<red>+ \\"a\\" => 0,</>
2180+
<dim> }"
2181+
`;
2182+
2183+
exports[`.toEqual() {pass: false} expect(Map {"v" => 1}).toEqual(Map {"v" => 2}) 1`] = `
2184+
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
2185+
2186+
Expected value to equal:
2187+
<green>Map {\\"v\\" => 2}</>
2188+
Received:
2189+
<red>Map {\\"v\\" => 1}</>
2190+
2191+
Difference:
2192+
2193+
<green>- Expected</>
2194+
<red>+ Received</>
2195+
2196+
<dim> Map {
2197+
<green>- \\"v\\" => 2,</>
2198+
<red>+ \\"v\\" => 1,</>
2199+
<dim> }"
2200+
`;
2201+
2202+
exports[`.toEqual() {pass: false} expect(Map {}).not.toEqual(Map {}) 1`] = `
2203+
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
2204+
2205+
Expected value to not equal:
2206+
<green>Map {}</>
2207+
Received:
2208+
<red>Map {}</>"
2209+
`;
2210+
2211+
exports[`.toEqual() {pass: false} expect(Map {}).toEqual(Set {}) 1`] = `
2212+
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
2213+
2214+
Expected value to equal:
2215+
<green>Set {}</>
2216+
Received:
2217+
<red>Map {}</>
2218+
2219+
Difference:
2220+
2221+
Comparing two different types of values. Expected <green>set</> but received <red>map</>."
2222+
`;
2223+
20982224
exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).not.toEqual(Map {1 => "one", 2 => "two"}) 1`] = `
20992225
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
21002226

@@ -2132,6 +2258,15 @@ Difference:
21322258
<dim> }"
21332259
`;
21342260

2261+
exports[`.toEqual() {pass: false} expect(Set {}).not.toEqual(Set {}) 1`] = `
2262+
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
2263+
2264+
Expected value to not equal:
2265+
<green>Set {}</>
2266+
Received:
2267+
<red>Set {}</>"
2268+
`;
2269+
21352270
exports[`.toEqual() {pass: false} expect(Set {1, 2}).not.toEqual(Set {1, 2}) 1`] = `
21362271
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
21372272

packages/expect/src/__tests__/matchers.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,14 @@ describe('.toEqual()', () => {
167167
[{a: 5}, {b: 6}],
168168
['banana', 'apple'],
169169
[null, undefined],
170+
[[1], [2]],
171+
[[1, 2], [2, 1]],
172+
[new Map(), new Set()],
170173
[new Set([1, 2]), new Set()],
171174
[new Set([1, 2]), new Set([1, 2, 3])],
172175
[new Map([[1, 'one'], [2, 'two']]), new Map([[1, 'one']])],
176+
[new Map([['a', 0]]), new Map([['b', 0]])],
177+
[new Map([['v', 1]]), new Map([['v', 2]])],
173178
[{a: 1, b: 2}, jestExpect.objectContaining({a: 2})],
174179
[false, jestExpect.objectContaining({a: 2})],
175180
[[1, 3], jestExpect.arrayContaining([1, 2])],
@@ -198,9 +203,14 @@ describe('.toEqual()', () => {
198203
[true, true],
199204
[1, 1],
200205
['abc', 'abc'],
206+
[[1], [1]],
207+
[[1, 2], [1, 2]],
208+
[{}, {}],
201209
[{a: 99}, {a: 99}],
210+
[new Set(), new Set()],
202211
[new Set([1, 2]), new Set([1, 2])],
203212
[new Set([1, 2]), new Set([2, 1])],
213+
[new Map(), new Map()],
204214
[new Map([[1, 'one'], [2, 'two']]), new Map([[1, 'one'], [2, 'two']])],
205215
[new Map([[1, 'one'], [2, 'two']]), new Map([[2, 'two'], [1, 'one']])],
206216
[{a: 1, b: 2}, jestExpect.objectContaining({a: 1})],

packages/expect/src/utils.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,29 @@ export const iterableEquality = (a: any, b: any) => {
114114
if (a.size !== undefined) {
115115
if (a.size !== b.size) {
116116
return false;
117-
} else {
118-
const args = [];
117+
} else if (a instanceof Set) {
118+
let allFound = true;
119119
for (const aValue of a) {
120-
args.push(aValue);
120+
if (!b.has(aValue)) {
121+
allFound = false;
122+
break;
123+
}
121124
}
122-
for (const bValue of b) {
123-
args.push(bValue);
125+
if (allFound) {
126+
return true;
127+
}
128+
} else if (a instanceof Map) {
129+
let allFound = true;
130+
for (const aEntry of a) {
131+
if (
132+
!b.has(aEntry[0]) ||
133+
!equals(aEntry[1], b.get(aEntry[0]), [iterableEquality])
134+
) {
135+
allFound = false;
136+
break;
137+
}
124138
}
125-
const merged = new a.constructor(args);
126-
if (merged.size === a.size) {
139+
if (allFound) {
127140
return true;
128141
}
129142
}

0 commit comments

Comments
 (0)