Skip to content

Commit

Permalink
Support asymmetric matching in toMatchObject (#2787)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfarina authored and cpojer committed Feb 3, 2017
1 parent 5ed366d commit 2571b49
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
Expand Up @@ -2605,6 +2605,23 @@ Difference:
 ]"
`;

exports[`toMatchObject() {pass: false} expect({"a": "a", "c": "d"}).toMatchObject({"a": Any<Number>}) 1`] = `
"expect(received).toMatchObject(expected)

Expected value to match object:
{\\"a\\": Any<Number>}
Received:
{\\"a\\": \\"a\\", \\"c\\": \\"d\\"}
Difference:
- Expected
+ Received

 Object {
- \\"a\\": Any<Number>,
+ \\"a\\": \\"a\\",
 }"
`;

exports[`toMatchObject() {pass: false} expect({"a": "b", "c": "d"}).toMatchObject({"a": "b!", "c": "d"}) 1`] = `
"expect(received).toMatchObject(expected)

Expand Down Expand Up @@ -2790,6 +2807,29 @@ Difference:
 }"
`;

exports[`toMatchObject() {pass: false} expect({"a": [3, 4, 5], "b": "b"}).toMatchObject({"a": {"b": Any<String>}}) 1`] = `
"expect(received).toMatchObject(expected)

Expected value to match object:
{\\"a\\": {\\"b\\": Any<String>}}
Received:
{\\"a\\": [3, 4, 5], \\"b\\": \\"b\\"}
Difference:
- Expected
+ Received

 Object {
- \\"a\\": Object {
- \\"b\\": Any<String>,
- },
+ \\"a\\": Array [
+ 3,
+ 4,
+ 5,
+ ],
 }"
`;

exports[`toMatchObject() {pass: false} expect({"a": 1, "b": 1, "c": 1, "d": {"e": {"f": 555}}}).toMatchObject({"d": {"e": {"f": 222}}}) 1`] = `
"expect(received).toMatchObject(expected)

Expand Down Expand Up @@ -2983,6 +3023,24 @@ Received:
{\\"a\\": [3, 4, 5], \\"b\\": \\"b\\"}"
`;

exports[`toMatchObject() {pass: true} expect({"a": {"x": "x", "y": "y"}}).toMatchObject({"a": {"x": Any<String>}}) 1`] = `
"expect(received).not.toMatchObject(expected)

Expected value not to match object:
{\\"a\\": {\\"x\\": Any<String>}}
Received:
{\\"a\\": {\\"x\\": \\"x\\", \\"y\\": \\"y\\"}}"
`;

exports[`toMatchObject() {pass: true} expect({"a": 1, "c": 2}).toMatchObject({"a": Any<Number>}) 1`] = `
"expect(received).not.toMatchObject(expected)

Expected value not to match object:
{\\"a\\": Any<Number>}
Received:
{\\"a\\": 1, \\"c\\": 2}"
`;

exports[`toMatchObject() {pass: true} expect({"a": 2015-11-30T00:00:00.000Z, "b": "b"}).toMatchObject({"a": 2015-11-30T00:00:00.000Z}) 1`] = `
"expect(received).not.toMatchObject(expected)

Expand Down
4 changes: 4 additions & 0 deletions packages/jest-matchers/src/__tests__/matchers-test.js
Expand Up @@ -679,6 +679,8 @@ describe('toMatchObject()', () => {
[{a: 'b', t: {x: {r: 'r'}, z: 'z'}}, {t: {x: {r: 'r'}}}],
[{a: [3, 4, 5], b: 'b'}, {a: [3, 4, 5]}],
[{a: [3, 4, 5, 'v'], b: 'b'}, {a: [3, 4, 5, 'v']}],
[{a: 1, c: 2}, {a: jestExpect.any(Number)}],
[{a: {x: 'x', y: 'y'}}, {a: {x: jestExpect.any(String)}}],
[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 All @@ -698,12 +700,14 @@ describe('toMatchObject()', () => {
[
[{a: 'b', c: 'd'}, {e: 'b'}],
[{a: 'b', c: 'd'}, {a: 'b!', c: 'd'}],
[{a: 'a', c: 'd'}, {a: jestExpect.any(Number)}],
[{a: 'b', t: {x: {r: 'r'}, z: 'z'}}, {a: 'b', t: {z: [3]}}],
[{a: 'b', t: {x: {r: 'r'}, z: 'z'}}, {t: {l: {r: 'r'}}}],
[{a: [3, 4, 5], b: 'b'}, {a: [3, 4, 5, 6]}],
[{a: [3, 4, 5], b: 'b'}, {a: [3, 4]}],
[{a: [3, 4, 'v'], b: 'b'}, {a: ['v']}],
[{a: [3, 4, 5], b: 'b'}, {a: {b: 4}}],
[{a: [3, 4, 5], b: 'b'}, {a: {b: jestExpect.any(String)}}],
[[1, 2], [1, 3]],
[new Date('2015-11-30'), new Date('2015-10-10')],
[{a: new Date('2015-11-30'), b: 'b'}, {a: new Date('2015-10-10')}],
Expand Down
1 change: 1 addition & 0 deletions packages/jest-matchers/src/jasmine-utils.js
Expand Up @@ -297,4 +297,5 @@ module.exports = {
hasProperty,
isA,
isUndefined,
asymmetricMatch,
};
7 changes: 7 additions & 0 deletions packages/jest-matchers/src/matchers.js
Expand Up @@ -31,6 +31,8 @@ const {
} = require('jest-matcher-utils');
const {
equals,
asymmetricMatch,
isUndefined,
} = require('./jasmine-utils');

type ContainIterable = (
Expand Down Expand Up @@ -561,6 +563,11 @@ const matchers: MatchersObject = {

const compare = (expected: any, received: any): boolean => {

const asymmetricResult = asymmetricMatch(expected, received);
if (!isUndefined(asymmetricResult)) {
return asymmetricResult;
}

if (typeof received !== typeof expected) {
return false;
}
Expand Down

0 comments on commit 2571b49

Please sign in to comment.