Skip to Content
React 16 Tooling
book

React 16 Tooling

by Adam Boduch, Christopher Pitt
April 2018
Intermediate to advanced content levelIntermediate to advanced
298 pages
6h 34m
English
Packt Publishing
Content preview from React 16 Tooling

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pro React 16

Pro React 16

Adam Freeman

Publisher Resources

ISBN: 9781788835015Supplemental Content