Asynchronous Tests

Learn how to utilize the done method in Jest to indicate that an asynchronous test should wait for an asynchronous call to finish before proceeding.

Introduction to asynchronous tests

In JavaScript and TypeScript, a lot of code we write is asynchronous. This means that we have no control over exactly when a callback will be invoked or a Promise will resolve, as we are waiting for an event to occur that is outside of our control.

This often presents problems in our unit testing, where we need to wait for an asynchronous event to complete before we can continue with our test.

As an example of this, consider the following class:

Get hands-on with 1200+ tech skills courses.