Skip to content

Commit

Permalink
Fix for 4286: Compare Maps and Sets by value rather than order (jestj…
Browse files Browse the repository at this point in the history
…s#4303)

* Add check for Maps & Sets

* check for presence of .size()

* exit early if size doesn't equal

* replace spread with args array
  • Loading branch information
kaylieEB authored and cpojer committed Aug 20, 2017
1 parent fbc81c5 commit aa2829b
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,43 @@ Difference:
Comparing two different types of values. Expected <green>array</> but received <red>number</>."
`;

exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).not.toEqual(Map {1 => "one", 2 => "two"}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expected value to not equal:
<green>Map {1 => \\"one\\", 2 => \\"two\\"}</>
Received:
<red>Map {1 => \\"one\\", 2 => \\"two\\"}</>"
`;

exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).not.toEqual(Map {2 => "two", 1 => "one"}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expected value to not equal:
<green>Map {2 => \\"two\\", 1 => \\"one\\"}</>
Received:
<red>Map {1 => \\"one\\", 2 => \\"two\\"}</>"
`;

exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).toEqual(Map {1 => "one"}) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expected value to equal:
<green>Map {1 => \\"one\\"}</>
Received:
<red>Map {1 => \\"one\\", 2 => \\"two\\"}</>

Difference:

<green>- Expected</>
<red>+ Received</>

<dim> Map {
<dim> 1 => \\"one\\",
<red>+ 2 => \\"two\\",</>
<dim> }"
`;

exports[`.toEqual() {pass: false} expect(Set {1, 2}).not.toEqual(Set {1, 2}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

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

exports[`.toEqual() {pass: false} expect(Set {1, 2}).toEqual(Set {2, 1}) 1`] = `
exports[`.toEqual() {pass: false} expect(Set {1, 2}).not.toEqual(Set {2, 1}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expected value to not equal:
<green>Set {2, 1}</>
Received:
<red>Set {1, 2}</>"
`;

exports[`.toEqual() {pass: false} expect(Set {1, 2}).toEqual(Set {}) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expected value to equal:
<green>Set {2, 1}</>
<green>Set {}</>
Received:
<red>Set {1, 2}</>

Difference:

<dim>Compared values have no visual difference."
<green>- Expected</>
<red>+ Received</>

<green>-Set {}</>
<red>+Set {</>
<red>+ 1,</>
<red>+ 2,</>
<red>+}</>"
`;

exports[`.toEqual() {pass: false} expect(Set {1, 2}).toEqual(Set {1, 2, 3}) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expected value to equal:
<green>Set {1, 2, 3}</>
Received:
<red>Set {1, 2}</>

Difference:

<green>- Expected</>
<red>+ Received</>

<dim> Set {
<dim> 1,
<dim> 2,
<green>- 3,</>
<dim> }"
`;

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

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

Expected value to match object:
<green>Set {2, 1}</>
<green>Set {2}</>
Received:
<red>Set {1, 2}</>
Difference:
<dim>Compared values have no visual difference."
<green>- Expected</>
<red>+ Received</>

<dim> Set {
<red>+ 1,</>
<dim> 2,
<dim> }"
`;

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

exports[`toMatchObject() {pass: true} expect(Set {1, 2}).toMatchObject(Set {2, 1}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toMatchObject(<green>expected</><dim>)

Expected value not to match object:
<green>Set {2, 1}</>
Received:
<red>Set {1, 2}</>"
`;

exports[`toMatchObject() throws expect("44").toMatchObject({}) 1`] = `
"<dim>expect(<red>object</><dim>)[.not].toMatchObject(<green>expected</><dim>)

Expand Down
10 changes: 8 additions & 2 deletions packages/jest-matchers/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ describe('.toEqual()', () => {
[{a: 5}, {b: 6}],
['banana', 'apple'],
[null, undefined],
[new Set([1, 2]), new Set([2, 1])],
[new Set([1, 2]), new Set()],
[new Set([1, 2]), new Set([1, 2, 3])],
[new Map([[1, 'one'], [2, 'two']]), new Map([[1, 'one']])],
[{a: 1, b: 2}, jestExpect.objectContaining({a: 2})],
[false, jestExpect.objectContaining({a: 2})],
[[1, 3], jestExpect.arrayContaining([1, 2])],
Expand Down Expand Up @@ -198,6 +200,9 @@ describe('.toEqual()', () => {
['abc', 'abc'],
[{a: 99}, {a: 99}],
[new Set([1, 2]), new Set([1, 2])],
[new Set([1, 2]), new Set([2, 1])],
[new Map([[1, 'one'], [2, 'two']]), new Map([[1, 'one'], [2, 'two']])],
[new Map([[1, 'one'], [2, 'two']]), new Map([[2, 'two'], [1, 'one']])],
[{a: 1, b: 2}, jestExpect.objectContaining({a: 1})],
[[1, 2, 3], jestExpect.arrayContaining([2, 3])],
['abcd', jestExpect.stringContaining('bc')],
Expand Down Expand Up @@ -834,6 +839,7 @@ describe('toMatchObject()', () => {
[{a: 1, c: 2}, {a: jestExpect.any(Number)}],
[{a: {x: 'x', y: 'y'}}, {a: {x: jestExpect.any(String)}}],
[new Set([1, 2]), new Set([1, 2])],
[new Set([1, 2]), new Set([2, 1])],
[new Date('2015-11-30'), new Date('2015-11-30')],
[{a: new Date('2015-11-30'), b: 'b'}, {a: new Date('2015-11-30')}],
[{a: null, b: 'b'}, {a: null}],
Expand Down Expand Up @@ -866,7 +872,7 @@ describe('toMatchObject()', () => {
[{a: [3, 4, 5], b: 'b'}, {a: {b: jestExpect.any(String)}}],
[[1, 2], [1, 3]],
[[0], [-0]],
[new Set([1, 2]), new Set([2, 1])],
[new Set([1, 2]), new Set([2])],
[new Date('2015-11-30'), new Date('2015-10-10')],
[{a: new Date('2015-11-30'), b: 'b'}, {a: new Date('2015-10-10')}],
[{a: null, b: 'b'}, {a: '4'}],
Expand Down
19 changes: 19 additions & 0 deletions packages/jest-matchers/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ export const iterableEquality = (a: any, b: any) => {
if (a.constructor !== b.constructor) {
return false;
}

if (a.size !== undefined) {
if (a.size !== b.size) {
return false;
} else {
const args = [];
for (const aValue of a) {
args.push(aValue);
}
for (const bValue of b) {
args.push(bValue);
}
const merged = new a.constructor(args);
if (merged.size === a.size) {
return true;
}
}
}

const bIterator = b[IteratorSymbol]();

for (const aValue of a) {
Expand Down

0 comments on commit aa2829b

Please sign in to comment.