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

Approximate equality

There are times where checking for the exact value of something in your code makes no difference and could be more work than is worthwhile. For example, you might only need to make sure that a value is present. You might also need to perform the inverse—to make sure that there is no value. Something versus nothing in JavaScript terminology is truthy versus falsy.

To check for truthy or falsy values in your Jest unit tests, you would use the isTruthy() or isFalsy() methods respectively:

describe('approximate equality', () => { 
  it('1 is truthy', () => { 
    expect(1).toBeTruthy(); 
    expect(1).not.toBeFalsy(); 
  }); 
 
  it('\'\' is falsy', () => { 
    expect('').toBeFalsy(); 
    expect('').not.toBeTruthy(); 
  }); 
});

The value 1 isn't true, but ...

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