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

Asynchronous assertions

Jest anticipates that you'll have asynchronous code to test. This is why it provides APIs to make this aspect of writing unit tests feel natural. In the previous section, we wrote tests that performed assertions within a then() callback and called done() when all of the asynchronous testing was completed. In this section, we're going to look at another approach.

Jest allows you to return promise expectations from your unit test functions and will handle them accordingly. Let's refactor the readFile() tests that you wrote in the previous section:

import fs from 'fs'; import readFile from './readFile'; jest.mock('fs'); describe('readFile', () => { it('calls fs.readFile', () => { fs.readFile.mockReset(); fs.readFile.mockImplementation((path, ...

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