Skip to content

Commit 8cba75e

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

3 files changed

Lines changed: 35 additions & 12 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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
import type {MatchersObject} from 'types/Matchers';
1212

13+
import chalk from 'chalk';
1314
import diff from 'jest-diff';
1415
import getType from 'jest-get-type';
1516
import {escapeStrForRegex} from 'jest-regex-util';
1617
import {
1718
EXPECTED_COLOR,
1819
RECEIVED_COLOR,
20+
SUGGEST_TO_EQUAL,
1921
ensureNoExpected,
2022
ensureNumbers,
2123
matcherHint,
@@ -67,29 +69,24 @@ const matchers: MatchersObject = {
6769
`Received:\n` +
6870
` ${printReceived(received)}`
6971
: () => {
70-
if (
72+
const suggestToEqual =
7173
getType(received) === getType(expected) &&
7274
(getType(received) === 'object' || getType(expected) === 'array') &&
7375
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-
}
8176

8277
const diffString = diff(expected, received, {
8378
expand: this.expand,
8479
});
80+
8581
return (
8682
matcherHint('.toBe') +
8783
'\n\n' +
8884
`Expected value to be (using ===):\n` +
8985
` ${printExpected(expected)}\n` +
9086
`Received:\n` +
9187
` ${printReceived(received)}` +
92-
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
88+
(diffString ? `\n\nDifference:\n\n${diffString}` : '') +
89+
(suggestToEqual ? ` ${SUGGEST_TO_EQUAL}` : '')
9390
);
9491
};
9592

0 commit comments

Comments
 (0)