Mistake: Using toBe for Array
Find the Mistake Easy
Question
What is wrong with this array assertion?
Code
test('array check', () => {
expect([1, 2, 3]).toBe([1, 2, 3]);
});
Hint
toBe checks reference equality, not content equality.
Explanation
The mistake is using toBe to compare arrays. Two separately created arrays are different references in memory, so toBe fails even though they have the same elements. The fix: use toEqual for deep equality: expect([1, 2, 3]).toEqual([1, 2, 3]);.
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.