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