Skip to content

Commit

Permalink
Update toStrictEqual failure message
Browse files Browse the repository at this point in the history
  • Loading branch information
Alcedo Nathaniel De Guzman Jr committed Oct 23, 2018
1 parent cdf7a22 commit 2195f81
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
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

0 comments on commit 2195f81

Please sign in to comment.