What's the difference between `toBe()` and `toEqual()`?

Intermediate

Answer

toBe() uses Object.is() for exact equality (reference equality for objects), while toEqual() performs deep equality checking:

test('toBe vs toEqual', () => {
  const obj1 = { name: 'John' };
  const obj2 = { name: 'John' };
  const obj3 = obj1;
  
  // toBe checks reference equality
  expect(obj1).toBe(obj3); // ✓ Same reference
  expect(obj1).toBe(obj2); // ✗ Different references
  
  // toEqual checks deep equality
  expect(obj1).toEqual(obj2); // ✓ Same content
  expect(obj1).toEqual(obj3); // ✓ Same content
  
  // For primitives, they work similarly
  expect(5).toBe(5); // ✓
  expect(5).toEqual(5); // ✓
});