toBe Used on Object (Reference Inequality)
Error message
Expected: {a: 1}, Received: {a: 1} — Object.is equality failed
Quick summary
toBe uses Object.is (reference equality). Two separately created objects are different references, so toBe fails even for identical content. Use toEqual for deep equality.
Why this happens
Using toBe to compare objects or arrays instead of toEqual.
Misunderstanding the difference between reference equality and deep equality.
Minimal example
test('object check', () => {
expect({ a: 1 }).toBe({ a: 1 }); // fails — different references
});test('object check', () => {
expect({ a: 1 }).toEqual({ a: 1 }); // passes — deep equality
});Use toEqual for deep equality comparison of objects. toBe checks reference equality.
How to diagnose
Check if the assertion uses toBe on an object or array.
Read the error message — it shows both values look the same but toBe still fails.
Understand that toBe checks === (reference), toEqual checks deep equality.
How to fix
Replace toBe with toEqual for object and array comparisons.
Use toStrictEqual for even stricter equality (checks type and structure).
Keep toBe for primitive values (numbers, strings, booleans).
How to prevent
Remember: toBe for primitives, toEqual for objects/arrays.
Use toStrictEqual when you need exact type matching.
Related Resources
Related Glossary
Related Lessons
- Jest Basics
Getting started with Jest.