Skip to content

Commit 64f8dbc

Browse files
rogeliogcpojer
authored andcommitted
[WIP] Make toBe matcher error message more helpful for objects and arrays (#4277)
* Make toBe matcher error message more helpful for objects and arrays * Just add a message at the bottom instead of replacing it
1 parent 8a4216e commit 64f8dbc

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

packages/jest-matcher-utils/src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ const NUMBERS = [
4949
'thirteen',
5050
];
5151

52+
const SUGGEST_TO_EQUAL = chalk.dim(
53+
'Looks like you wanted to test for object/array equality with strict `toBe` matcher. You probably need to use `toEqual` instead.',
54+
);
55+
5256
const stringify = (object: any, maxDepth?: number = 10): string => {
5357
const MAX_LENGTH = 10000;
5458
let result;
@@ -165,6 +169,7 @@ module.exports = {
165169
EXPECTED_COLOR,
166170
RECEIVED_BG,
167171
RECEIVED_COLOR,
172+
SUGGEST_TO_EQUAL,
168173
ensureActualIsNumber,
169174
ensureExpectedIsNumber,
170175
ensureNoExpected,

packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ Received:
239239

240240
Difference:
241241

242-
<dim>Compared values have no visual difference."
242+
<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."
243243
`;
244244

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

253253
Difference:
254254

255-
<dim>Compared values have no visual difference."
255+
<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."
256256
`;
257257

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

285285
Difference:
286286

287-
<dim>Compared values have no visual difference."
287+
<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."
288288
`;
289289

290290
exports[`.toBe() fails for: 1 and 2 1`] = `

packages/jest-matchers/src/matchers.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {escapeStrForRegex} from 'jest-regex-util';
1616
import {
1717
EXPECTED_COLOR,
1818
RECEIVED_COLOR,
19+
SUGGEST_TO_EQUAL,
1920
ensureNoExpected,
2021
ensureNumbers,
2122
matcherHint,
@@ -67,17 +68,24 @@ const matchers: MatchersObject = {
6768
`Received:\n` +
6869
` ${printReceived(received)}`
6970
: () => {
71+
const suggestToEqual =
72+
getType(received) === getType(expected) &&
73+
(getType(received) === 'object' || getType(expected) === 'array') &&
74+
equals(received, expected, [iterableEquality]);
75+
7076
const diffString = diff(expected, received, {
7177
expand: this.expand,
7278
});
79+
7380
return (
7481
matcherHint('.toBe') +
7582
'\n\n' +
7683
`Expected value to be (using ===):\n` +
7784
` ${printExpected(expected)}\n` +
7885
`Received:\n` +
7986
` ${printReceived(received)}` +
80-
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
87+
(diffString ? `\n\nDifference:\n\n${diffString}` : '') +
88+
(suggestToEqual ? ` ${SUGGEST_TO_EQUAL}` : '')
8189
);
8290
};
8391

0 commit comments

Comments
 (0)