From the course: Node.js: Testing and Code Quality
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Testing promises with Jest
From the course: Node.js: Testing and Code Quality
Testing promises with Jest
- [Instructor] We've tested callbacks with Jest, but that's not the only kind of asynchronous code. What about promises? Jest supports promises natively, which is a great feature. To test a promise, just return the promise and put the assertion in the .then or .catch. You do not need to use the callback named done. If you're catching rejections, then you should also use expect.assertions with the number of assertions to expect to specify how many assertions there were supposed to be. If you don't, then a fulfilled promise would pass. There's two additional chaining matchers that can simplify writing tests. The resolves matcher will cause Jest to wait for the promise to resolve. If the promise is rejected, then the test fails. The rejects matcher works in a similar way. If a promise is fulfilled, then the test fails. When using the rejects matcher, you don't have to specify the number of assertions. We'll use this syntax in…
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
-
-
-
-