Skip to content

Commit

Permalink
Add support for Error objects in toMatchObject (#4339)
Browse files Browse the repository at this point in the history
* Add support for Errors in toMatchObject()

* Move subsetEqualityMatcher and isObjectWithKeys to the utils directory

* Remove files that were created... somehow...

* Add types for subsetEquality parameters
  • Loading branch information
jseminck authored and cpojer committed Aug 24, 2017
1 parent 53c0ccc commit 441bd0a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2914,6 +2914,21 @@ Difference:
<dim> ]"
`;

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

Expected value to match object:
<green>[Error: bar]</>
Received:
<red>[Error: foo]</>
Difference:
<green>- Expected</>
<red>+ Received</>

<green>-[Error: bar]</>
<red>+[Error: foo]</>"
`;

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

Expand Down Expand Up @@ -3295,6 +3310,15 @@ Received:
<red>[1, 2]</>"
`;

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

Expected value not to match object:
<green>[Error: foo]</>
Received:
<red>[Error: foo]</>"
`;

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

Expand Down
2 changes: 2 additions & 0 deletions packages/jest-matchers/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ describe('toMatchObject()', () => {
[[1, 2], [1, 2]],
[{a: undefined}, {a: undefined}],
[[], []],
[new Error('foo'), new Error('foo')],
].forEach(([n1, n2]) => {
it(`{pass: true} expect(${stringify(n1)}).toMatchObject(${stringify(
n2,
Expand Down Expand Up @@ -883,6 +884,7 @@ describe('toMatchObject()', () => {
[{}, {a: undefined}],
[[1, 2, 3], [2, 3, 1]],
[[1, 2, 3], [1, 2, 2]],
[new Error('foo'), new Error('bar')],
].forEach(([n1, n2]) => {
it(`{pass: false} expect(${stringify(n1)}).toMatchObject(${stringify(
n2,
Expand Down
17 changes: 1 addition & 16 deletions packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
getPath,
hasOwnProperty,
iterableEquality,
subsetEquality,
} from './utils';
import {equals} from './jasmine_utils';

Expand All @@ -39,22 +40,6 @@ type ContainIterable =
| DOMTokenList
| HTMLCollection<any>;

const isObjectWithKeys = a =>
a !== null &&
typeof a === 'object' &&
!(a instanceof Array) &&
!(a instanceof Date);
const subsetEquality = (object, subset) => {
if (!isObjectWithKeys(object) || !isObjectWithKeys(subset)) {
return undefined;
}
return Object.keys(subset).every(
key =>
hasOwnProperty(object, key) &&
equals(object[key], subset[key], [iterableEquality, subsetEquality]),
);
};

const matchers: MatchersObject = {
toBe(received: any, expected: number) {
const pass = received === expected;
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 @@ -143,6 +143,25 @@ export const iterableEquality = (a: any, b: any) => {
return true;
};

const isObjectWithKeys = a =>
a !== null &&
typeof a === 'object' &&
!(a instanceof Error) &&
!(a instanceof Array) &&
!(a instanceof Date);

export const subsetEquality = (object: Object, subset: Object) => {
if (!isObjectWithKeys(object) || !isObjectWithKeys(subset)) {
return undefined;
}

return Object.keys(subset).every(
key =>
hasOwnProperty(object, key) &&
equals(object[key], subset[key], [iterableEquality, subsetEquality]),
);
};

export const partition = <T>(
items: Array<T>,
predicate: T => boolean,
Expand Down

0 comments on commit 441bd0a

Please sign in to comment.