O'Reilly logo

React 16 Tooling by Adam Boduch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Value equality

When working with objects and arrays, checking for equality can be painful. You typically can't use strict equality because you're comparing references, which are always different. If it's the values that you're trying to compare, you need to iterate over the object or collection and compare the values, keys, and indexes individually.

Since no one in their right mind wants to do all of this work to perform a simple test. Jest provides the toEqual() method, which compares object properties and array values for you:

describe('value equality', () => { 
  it('objects are the same', () => { 
    expect({ 
      one: 1, 
      two: 2 
    }).toEqual({ 
      one: 1, 
      two: 2, 
    });    expect({ 
      one: 1, 
      two: 2 
    }).not.toBe({ 
      one: 1, 
      two: 2 }); }); it('arrays are the same', ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required