Test Your Promises

Now let’s explore testing for promises in code running within Node.js. The same techniques work for client-side code that use promises as well. We’ll start with a short introduction to promises and then dive into testing code that uses promises.

A Short Introduction to Promises

Before digging into testing promises, let’s review how promises works—I promise to keep it short.

Traditionally asynchronous functions have used callbacks. But callbacks have a few drawbacks. Callbacks have to be registered before the asynchronous functions generate any response. For example, when using an instance of XMLHttpRequest, calling the send function before registering a callback with the onreadystatechange property may result in some ...

Get Test-Driving JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.