Wednesday, September 9, 2015

Promises in JavaScript Unit Tests: the Definitive Guide

Promises are becoming a common part of JavaScript code. The native Promise object is already supported by all the major browsers including Chrome, Firefox, and Safari.

Despite making asynchronous code simpler, dealing with promises in unit tests is a hassle. You need to wire your test’s assertions into the callbacks of the promise, which adds extra code into the test. In this way the test itself becomes a bit complicated and it’s harder to see what’s happening.

In this article, I'll show you how to fix this issue and discuss about useful patterns which are able to simplify common promise-scenarios in tests' stage.

I've created an example project that you can download from my website which shows the techniques introduced in this article.

Getting Started

For this project I'll use Mocha as the testing framework and the Chai library to provide the assertions. You'll understand why in a moment.

Continue reading %Promises in JavaScript Unit Tests: the Definitive Guide%


by Jani Hartikainen via SitePoint

No comments:

Post a Comment