Skip to content

Commit

Permalink
[WIP] Make toBe matcher error message more helpful for objects and ar…
Browse files Browse the repository at this point in the history
…rays (#4277)

* Make toBe matcher error message more helpful for objects and arrays

* Just add a message at the bottom instead of replacing it
  • Loading branch information
rogeliog authored and cpojer committed Aug 17, 2017
1 parent 8a4216e commit 64f8dbc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const NUMBERS = [
'thirteen',
];

const SUGGEST_TO_EQUAL = chalk.dim(
'Looks like you wanted to test for object/array equality with strict `toBe` matcher. You probably need to use `toEqual` instead.',
);

const stringify = (object: any, maxDepth?: number = 10): string => {
const MAX_LENGTH = 10000;
let result;
Expand Down Expand Up @@ -165,6 +169,7 @@ module.exports = {
EXPECTED_COLOR,
RECEIVED_BG,
RECEIVED_COLOR,
SUGGEST_TO_EQUAL,
ensureActualIsNumber,
ensureExpectedIsNumber,
ensureNoExpected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Received:

Difference:

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

exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = `
Expand All @@ -252,7 +252,7 @@ Received:

Difference:

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

exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = `
Expand Down Expand Up @@ -284,7 +284,7 @@ Received:

Difference:

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

exports[`.toBe() fails for: 1 and 2 1`] = `
Expand Down
10 changes: 9 additions & 1 deletion packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {escapeStrForRegex} from 'jest-regex-util';
import {
EXPECTED_COLOR,
RECEIVED_COLOR,
SUGGEST_TO_EQUAL,
ensureNoExpected,
ensureNumbers,
matcherHint,
Expand Down Expand Up @@ -67,17 +68,24 @@ const matchers: MatchersObject = {
`Received:\n` +
` ${printReceived(received)}`
: () => {
const suggestToEqual =
getType(received) === getType(expected) &&
(getType(received) === 'object' || getType(expected) === 'array') &&
equals(received, expected, [iterableEquality]);

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

return (
matcherHint('.toBe') +
'\n\n' +
`Expected value to be (using ===):\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(received)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
(diffString ? `\n\nDifference:\n\n${diffString}` : '') +
(suggestToEqual ? ` ${SUGGEST_TO_EQUAL}` : '')
);
};

Expand Down

0 comments on commit 64f8dbc

Please sign in to comment.