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

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…

Contents