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