April 2018
Intermediate to advanced
298 pages
6h 34m
English
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', ...