O'Reilly logo

Learning Behavior-driven Development with JavaScript by Enrique Amodeo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required