Mistake: toBe on Object
Find the Mistake Easy
Question
What is wrong with this assertion?
Code
test('object check', () => {
expect({ a: 1 }).toBe({ a: 1 });
});
Hint
toBe checks reference equality, not deep equality.
Explanation
The mistake is using toBe to compare objects. toBe checks reference equality — two separate object literals are different references even if they have the same content. The fix is to use toEqual which performs deep equality: expect({ a: 1 }).toEqual({ a: 1 });.
Related Resources
Related Lessons
- Jest Basics
Getting started with Jest.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.