Skip to content

Commit 0da21e6

Browse files
committed
Just add a message at the bottom instead of replacing it
1 parent b02325a commit 0da21e6

3 files changed

Lines changed: 35 additions & 13 deletions

File tree

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: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,27 @@ Received:
232232
exports[`.toBe() fails for: [] and [] 1`] = `
233233
"<dim>expect(<red>received</><dim>).toBe(<green>expected</><dim>)
234234

235-
Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
235+
Expected value to be (using ===):
236+
<green>[]</>
237+
Received:
238+
<red>[]</>
239+
240+
Difference:
241+
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."
236243
`;
237244

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

241-
Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
248+
Expected value to be (using ===):
249+
<green>{\\"a\\": 1}</>
250+
Received:
251+
<red>{\\"a\\": 1}</>
252+
253+
Difference:
254+
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."
242256
`;
243257

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

266-
Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
280+
Expected value to be (using ===):
281+
<green>{}</>
282+
Received:
283+
<red>{}</>
284+
285+
Difference:
286+
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."
267288
`;
268289

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

packages/jest-matchers/src/matchers.js

Lines changed: 6 additions & 10 deletions
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,29 +68,24 @@ const matchers: MatchersObject = {
6768
`Received:\n` +
6869
` ${printReceived(received)}`
6970
: () => {
70-
if (
71+
const suggestToEqual =
7172
getType(received) === getType(expected) &&
7273
(getType(received) === 'object' || getType(expected) === 'array') &&
73-
equals(received, expected, [iterableEquality])
74-
) {
75-
return (
76-
matcherHint('.toBe') +
77-
'\n\n' +
78-
'Looks like you wanted to test for object/array equity with strict `toBe` matcher. You probably need to use `toEqual` instead.'
79-
);
80-
}
74+
equals(received, expected, [iterableEquality]);
8175

8276
const diffString = diff(expected, received, {
8377
expand: this.expand,
8478
});
79+
8580
return (
8681
matcherHint('.toBe') +
8782
'\n\n' +
8883
`Expected value to be (using ===):\n` +
8984
` ${printExpected(expected)}\n` +
9085
`Received:\n` +
9186
` ${printReceived(received)}` +
92-
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
87+
(diffString ? `\n\nDifference:\n\n${diffString}` : '') +
88+
(suggestToEqual ? ` ${SUGGEST_TO_EQUAL}` : '')
9389
);
9490
};
9591

0 commit comments

Comments
 (0)