From the course: Node.js: Testing and Code Quality

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Testing asynchronous callbacks with Jest

Testing asynchronous callbacks with Jest

From the course: Node.js: Testing and Code Quality

Testing asynchronous callbacks with Jest

- [Instructor] Jest supports asynchronous code without the use of any third-party plugins. Three styles are supported. The first are callbacks, which have been around for quite a while. There's also promises which are more modern and a flexible technique. Finally, there's async/await, which is syntactic sugar for promises. We'll look at callbacks and promises first, then the challenge will be to use async/await. Let's start testing asynchronous callbacks with Jest. By default, Jest tests complete at the end of execution. An asynchronous callback won't be called until the function executes. Therefore, if we wrote a test like we did before, the test will already be completed before we can check to see if it succeeded or not. Here's an example of an asynchronous callback. We'll start with a simple function toTest which takes a callback. I'm going to use set immediate to asynchronously execute the callback, and…

Contents