Skip to content

Commit

Permalink
Make toBe matcher error message more helpful for objects and arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeliog committed Aug 16, 2017
1 parent 417182b commit b02325a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
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.'
);
}

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

0 comments on commit b02325a

Please sign in to comment.