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

[WIP] Make toBe matcher error message more helpful for objects and arrays #4277

Merged
merged 2 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -232,27 +232,13 @@ Received:
exports[`.toBe() fails for: [] and [] 1`] = `
"<dim>expect(<red>received</><dim>).toBe(<green>expected</><dim>)

Expected value to be (using ===):
<green>[]</>
Received:
<red>[]</>

Difference:

<dim>Compared values have no visual difference."
Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
`;

exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = `
"<dim>expect(<red>received</><dim>).toBe(<green>expected</><dim>)

Expected value to be (using ===):
<green>{\\"a\\": 1}</>
Received:
<red>{\\"a\\": 1}</>

Difference:

<dim>Compared values have no visual difference."
Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
`;

exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = `
Expand All @@ -277,14 +263,7 @@ Difference:
exports[`.toBe() fails for: {} and {} 1`] = `
"<dim>expect(<red>received</><dim>).toBe(<green>expected</><dim>)

Expected value to be (using ===):
<green>{}</>
Received:
<red>{}</>

Difference:

<dim>Compared values have no visual difference."
Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
`;

exports[`.toBe() fails for: 1 and 2 1`] = `
Expand Down
12 changes: 12 additions & 0 deletions packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ const matchers: MatchersObject = {
`Received:\n` +
` ${printReceived(received)}`
: () => {
if (
getType(received) === getType(expected) &&
(getType(received) === 'object' || getType(expected) === 'array') &&
equals(received, expected, [iterableEquality])
) {
return (
matcherHint('.toBe') +
'\n\n' +
'Looks like you wanted to test for object/array equity with strict `toBe` matcher. You probably need to use `toEqual` instead.'
Copy link
Member

Choose a reason for hiding this comment

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

equity? Shouldn't it be "equality"?

I think it's still useful to keep some of the information from before, at least "Expected value to be (using ===) but they are different objects/arrays. […]”. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

screen shot 2017-08-17 at 9 51 10 am

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if we just add the new message below and leave the rest as it was? Just a new line as extra information

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think that is preferable. Can you keep it on the same line as the "no visual difference" sentence?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

);
}

const diffString = diff(expected, received, {
expand: this.expand,
});
Expand Down