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
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Survey of Node.js testing frameworks5m 5s
-
(Locked)
Install Jest in a Node.js project2m 25s
-
(Locked)
What and where to unit test?4m 46s
-
(Locked)
Writing your first Jest unit test4m 36s
-
(Locked)
Running a test suite with Jest6m 5s
-
(Locked)
Testing asynchronous callbacks with Jest5m 42s
-
(Locked)
Writing callback test suites with Jest6m 32s
-
(Locked)
Testing promises with Jest7m 52s
-
(Locked)
Challenge: Test async/await with Jest49s
-
(Locked)
Solution: Test async/await with Jest2m 51s
-
-
-
-