Testing asynchronous features

If you have made the tests pass, you are probably thinking that you are done. But actually, you are not. The problem is that we have designed a synchronous API. This is not feasible in a JS application, because JS is single-threaded. Any I/O will cause our system to block, and we need to process the requests one at a time. This is not acceptable in a server or in a UI application. So, we need to change our design to use an asynchronous API, but how do we test an asynchronous API?

Testing a callback-based API

Instead of returning the value directly, we can change our API to use callbacks. The same thing applies to our DAO. After all, the DAO will perform IO, so it needs to be asynchronous. So, let's change our test. ...

Get Learning Behavior-driven Development with JavaScript 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.