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

Update toStrictEqual failure message #7224

Merged
merged 2 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
- `[babel-jest]` Cache includes babel environment variables ([#7239](https://github.com/facebook/jest/pull/7239))
- `[jest-config]` Use strings instead of `RegExp` instances in normalized configuration ([#7251](https://github.com/facebook/jest/pull/7251))
- `[jest-circus]` Make sure to display real duration even if time is mocked ([#7264](https://github.com/facebook/jest/pull/7264))
- `[expect]` Improves the failing message for `toStrictEqual` matcher. ([#7224](https://github.com/facebook/jest/pull/7224))

### Chore & Maintenance

Expand Down
24 changes: 24 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3479,6 +3479,30 @@ Received:
<red>\\"bar\\"</>"
`;

exports[`.toStrictEqual() matches the expected snapshot when it fails 1`] = `
"<dim>expect(</><red>received</><dim>).toStrictEqual(</><green>expected</><dim>)</>

Difference:

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

<dim> Object {</>
<green>- \\"test\\": TestClassA {</>
<green>- \\"a\\": 1,</>
<green>- \\"b\\": 2,</>
<green>- },</>
<red>+ \\"test\\": 2,</>
<dim> }</>"
`;

exports[`.toStrictEqual() matches the expected snapshot when it fails 2`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.toStrictEqual(</><green>expected</><dim>)</>

Expected: <green>{\\"test\\": {\\"a\\": 1, \\"b\\": 2}}</>
Received: <red>{\\"test\\": {\\"a\\": 1, \\"b\\": 2}}</>"
`;

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

Expand Down
14 changes: 14 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ describe('.toStrictEqual()', () => {
}).toStrictEqual({test: new TestClassA(1, 2)});
});

it('matches the expected snapshot when it fails', () => {
expect(() =>
jestExpect({
test: 2,
}).toStrictEqual({test: new TestClassA(1, 2)}),
).toThrowErrorMatchingSnapshot();

expect(() =>
jestExpect({
test: new TestClassA(1, 2),
}).not.toStrictEqual({test: new TestClassA(1, 2)}),
).toThrowErrorMatchingSnapshot();
});

it('does not pass for different types', () => {
expect({
test: new TestClassA(1, 2),
Expand Down
21 changes: 7 additions & 14 deletions packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,27 +628,20 @@ const matchers: MatchersObject = {
true,
);

const hint = matcherHint('.toStrictEqual', undefined, undefined, {
isNot: this.isNot,
});
const message = pass
? () =>
matcherHint('.not.toStrictEqual') +
hint +
'\n\n' +
`Expected value to not equal:\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(received)}`
`Expected: ${printExpected(expected)}\n` +
`Received: ${printReceived(received)}`
: () => {
const diffString = diff(expected, received, {
expand: this.expand,
});
return (
matcherHint('.toStrictEqual') +
'\n\n' +
`Expected value to equal:\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(received)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
);
return hint + (diffString ? `\n\nDifference:\n\n${diffString}` : '');
};

// Passing the the actual and expected objects so that a custom reporter
Expand Down