Testing with React’s Jest and Enzyme when simulated clicks call a function that calls a promise

Updated answer: using async / await leads to cleaner code. Old code below. I’ve successfully solved this problem by combining the following elements: Mock out the promise and make it resolve immediately Make the test asynchronous by marking the test function async After simulating the click, wait until the next macrotask to give the promise … Read more

How to make Jest wait for all asynchronous code to finish execution before expecting an assertion

Updated for Jest 27+ For jest 27+, you can also use process.nextTick: await new Promise(process.nextTick); (Thanks to Adrian Godong in the comments) Original Answer Here’s a snippet that waits until pending Promises are resolved: const flushPromises = () => new Promise(setImmediate); Note that setImmediate is a non-standard feature (and is not expected to become standard). … Read more