Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Error objects in toMatchObject #4339

Merged
merged 4 commits into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snapshots 1`] = `
Object {
"apple": "original value",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test('snapshots', () => expect({apple: "updated value"}).toMatchSnapshot());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs Jest's Copyright header, and a trailing newline please.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I don't understand the purpose of this test. Why is it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my tests went badly. I was running on Windows and a bunch of stuff was failing. Will fix now that I'm back on the mac.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I believe these files got added because I interrupted a test run, and it creates/deletes files during tests but when you interrupt it doesn't clean up properly. Mystery solved 👌

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, subset) => {
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