Skip to content

Commit aa2829b

Browse files
kaylieEBcpojer
authored andcommitted
Fix for 4286: Compare Maps and Sets by value rather than order (jestjs#4303)
* Add check for Maps & Sets * check for presence of .size() * exit early if size doesn't equal * replace spread with args array
1 parent fbc81c5 commit aa2829b

3 files changed

Lines changed: 121 additions & 8 deletions

File tree

packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,43 @@ Difference:
20952095
Comparing two different types of values. Expected <green>array</> but received <red>number</>."
20962096
`;
20972097

2098+
exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).not.toEqual(Map {1 => "one", 2 => "two"}) 1`] = `
2099+
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
2100+
2101+
Expected value to not equal:
2102+
<green>Map {1 => \\"one\\", 2 => \\"two\\"}</>
2103+
Received:
2104+
<red>Map {1 => \\"one\\", 2 => \\"two\\"}</>"
2105+
`;
2106+
2107+
exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).not.toEqual(Map {2 => "two", 1 => "one"}) 1`] = `
2108+
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
2109+
2110+
Expected value to not equal:
2111+
<green>Map {2 => \\"two\\", 1 => \\"one\\"}</>
2112+
Received:
2113+
<red>Map {1 => \\"one\\", 2 => \\"two\\"}</>"
2114+
`;
2115+
2116+
exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).toEqual(Map {1 => "one"}) 1`] = `
2117+
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
2118+
2119+
Expected value to equal:
2120+
<green>Map {1 => \\"one\\"}</>
2121+
Received:
2122+
<red>Map {1 => \\"one\\", 2 => \\"two\\"}</>
2123+
2124+
Difference:
2125+
2126+
<green>- Expected</>
2127+
<red>+ Received</>
2128+
2129+
<dim> Map {
2130+
<dim> 1 => \\"one\\",
2131+
<red>+ 2 => \\"two\\",</>
2132+
<dim> }"
2133+
`;
2134+
20982135
exports[`.toEqual() {pass: false} expect(Set {1, 2}).not.toEqual(Set {1, 2}) 1`] = `
20992136
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
21002137

@@ -2104,17 +2141,53 @@ Received:
21042141
<red>Set {1, 2}</>"
21052142
`;
21062143

2107-
exports[`.toEqual() {pass: false} expect(Set {1, 2}).toEqual(Set {2, 1}) 1`] = `
2144+
exports[`.toEqual() {pass: false} expect(Set {1, 2}).not.toEqual(Set {2, 1}) 1`] = `
2145+
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)
2146+
2147+
Expected value to not equal:
2148+
<green>Set {2, 1}</>
2149+
Received:
2150+
<red>Set {1, 2}</>"
2151+
`;
2152+
2153+
exports[`.toEqual() {pass: false} expect(Set {1, 2}).toEqual(Set {}) 1`] = `
21082154
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
21092155

21102156
Expected value to equal:
2111-
<green>Set {2, 1}</>
2157+
<green>Set {}</>
21122158
Received:
21132159
<red>Set {1, 2}</>
21142160

21152161
Difference:
21162162

2117-
<dim>Compared values have no visual difference."
2163+
<green>- Expected</>
2164+
<red>+ Received</>
2165+
2166+
<green>-Set {}</>
2167+
<red>+Set {</>
2168+
<red>+ 1,</>
2169+
<red>+ 2,</>
2170+
<red>+}</>"
2171+
`;
2172+
2173+
exports[`.toEqual() {pass: false} expect(Set {1, 2}).toEqual(Set {1, 2, 3}) 1`] = `
2174+
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)
2175+
2176+
Expected value to equal:
2177+
<green>Set {1, 2, 3}</>
2178+
Received:
2179+
<red>Set {1, 2}</>
2180+
2181+
Difference:
2182+
2183+
<green>- Expected</>
2184+
<red>+ Received</>
2185+
2186+
<dim> Set {
2187+
<dim> 1,
2188+
<dim> 2,
2189+
<green>- 3,</>
2190+
<dim> }"
21182191
`;
21192192

21202193
exports[`.toEqual() {pass: false} expect(false).toEqual(ObjectContaining {"a": 2}) 1`] = `
@@ -3187,15 +3260,21 @@ Difference:
31873260
<red>+2015-11-30T00:00:00.000Z</>"
31883261
`;
31893262

3190-
exports[`toMatchObject() {pass: false} expect(Set {1, 2}).toMatchObject(Set {2, 1}) 1`] = `
3263+
exports[`toMatchObject() {pass: false} expect(Set {1, 2}).toMatchObject(Set {2}) 1`] = `
31913264
"<dim>expect(<red>received</><dim>).toMatchObject(<green>expected</><dim>)
31923265

31933266
Expected value to match object:
3194-
<green>Set {2, 1}</>
3267+
<green>Set {2}</>
31953268
Received:
31963269
<red>Set {1, 2}</>
31973270
Difference:
3198-
<dim>Compared values have no visual difference."
3271+
<green>- Expected</>
3272+
<red>+ Received</>
3273+
3274+
<dim> Set {
3275+
<red>+ 1,</>
3276+
<dim> 2,
3277+
<dim> }"
31993278
`;
32003279

32013280
exports[`toMatchObject() {pass: true} expect([]).toMatchObject([]) 1`] = `
@@ -3351,6 +3430,15 @@ Received:
33513430
<red>Set {1, 2}</>"
33523431
`;
33533432

3433+
exports[`toMatchObject() {pass: true} expect(Set {1, 2}).toMatchObject(Set {2, 1}) 1`] = `
3434+
"<dim>expect(<red>received</><dim>).not.toMatchObject(<green>expected</><dim>)
3435+
3436+
Expected value not to match object:
3437+
<green>Set {2, 1}</>
3438+
Received:
3439+
<red>Set {1, 2}</>"
3440+
`;
3441+
33543442
exports[`toMatchObject() throws expect("44").toMatchObject({}) 1`] = `
33553443
"<dim>expect(<red>object</><dim>)[.not].toMatchObject(<green>expected</><dim>)
33563444

packages/jest-matchers/src/__tests__/matchers.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ describe('.toEqual()', () => {
167167
[{a: 5}, {b: 6}],
168168
['banana', 'apple'],
169169
[null, undefined],
170-
[new Set([1, 2]), new Set([2, 1])],
170+
[new Set([1, 2]), new Set()],
171+
[new Set([1, 2]), new Set([1, 2, 3])],
172+
[new Map([[1, 'one'], [2, 'two']]), new Map([[1, 'one']])],
171173
[{a: 1, b: 2}, jestExpect.objectContaining({a: 2})],
172174
[false, jestExpect.objectContaining({a: 2})],
173175
[[1, 3], jestExpect.arrayContaining([1, 2])],
@@ -198,6 +200,9 @@ describe('.toEqual()', () => {
198200
['abc', 'abc'],
199201
[{a: 99}, {a: 99}],
200202
[new Set([1, 2]), new Set([1, 2])],
203+
[new Set([1, 2]), new Set([2, 1])],
204+
[new Map([[1, 'one'], [2, 'two']]), new Map([[1, 'one'], [2, 'two']])],
205+
[new Map([[1, 'one'], [2, 'two']]), new Map([[2, 'two'], [1, 'one']])],
201206
[{a: 1, b: 2}, jestExpect.objectContaining({a: 1})],
202207
[[1, 2, 3], jestExpect.arrayContaining([2, 3])],
203208
['abcd', jestExpect.stringContaining('bc')],
@@ -834,6 +839,7 @@ describe('toMatchObject()', () => {
834839
[{a: 1, c: 2}, {a: jestExpect.any(Number)}],
835840
[{a: {x: 'x', y: 'y'}}, {a: {x: jestExpect.any(String)}}],
836841
[new Set([1, 2]), new Set([1, 2])],
842+
[new Set([1, 2]), new Set([2, 1])],
837843
[new Date('2015-11-30'), new Date('2015-11-30')],
838844
[{a: new Date('2015-11-30'), b: 'b'}, {a: new Date('2015-11-30')}],
839845
[{a: null, b: 'b'}, {a: null}],
@@ -866,7 +872,7 @@ describe('toMatchObject()', () => {
866872
[{a: [3, 4, 5], b: 'b'}, {a: {b: jestExpect.any(String)}}],
867873
[[1, 2], [1, 3]],
868874
[[0], [-0]],
869-
[new Set([1, 2]), new Set([2, 1])],
875+
[new Set([1, 2]), new Set([2])],
870876
[new Date('2015-11-30'), new Date('2015-10-10')],
871877
[{a: new Date('2015-11-30'), b: 'b'}, {a: new Date('2015-10-10')}],
872878
[{a: null, b: 'b'}, {a: '4'}],

packages/jest-matchers/src/utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ export const iterableEquality = (a: any, b: any) => {
110110
if (a.constructor !== b.constructor) {
111111
return false;
112112
}
113+
114+
if (a.size !== undefined) {
115+
if (a.size !== b.size) {
116+
return false;
117+
} else {
118+
const args = [];
119+
for (const aValue of a) {
120+
args.push(aValue);
121+
}
122+
for (const bValue of b) {
123+
args.push(bValue);
124+
}
125+
const merged = new a.constructor(args);
126+
if (merged.size === a.size) {
127+
return true;
128+
}
129+
}
130+
}
131+
113132
const bIterator = b[IteratorSymbol]();
114133

115134
for (const aValue of a) {

0 commit comments

Comments
 (0)