Skip to content

Commit b02325a

Browse files
committed
Make toBe matcher error message more helpful for objects and arrays
1 parent 417182b commit b02325a

2 files changed

Lines changed: 15 additions & 24 deletions

File tree

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

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

235-
Expected value to be (using ===):
236-
<green>[]</>
237-
Received:
238-
<red>[]</>
239-
240-
Difference:
241-
242-
<dim>Compared values have no visual difference."
235+
Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
243236
`;
244237

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

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."
241+
Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
256242
`;
257243

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

280-
Expected value to be (using ===):
281-
<green>{}</>
282-
Received:
283-
<red>{}</>
284-
285-
Difference:
286-
287-
<dim>Compared values have no visual difference."
266+
Looks like you wanted to test for object/array equity with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead."
288267
`;
289268

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

packages/jest-matchers/src/matchers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ const matchers: MatchersObject = {
6767
`Received:\n` +
6868
` ${printReceived(received)}`
6969
: () => {
70+
if (
71+
getType(received) === getType(expected) &&
72+
(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+
}
81+
7082
const diffString = diff(expected, received, {
7183
expand: this.expand,
7284
});

0 commit comments

Comments
 (0)