Writing unit tests
We’ve already written some unit tests for our calculateBasket.js
module as part of our introduction to testing. This simple example showcased synchronous API testing, which proved to be an ideal starting point. The reason it was so straightforward is that our calculateBasketTotal
function is both synchronous and pure: it relies on no external dependencies and produces no side effects, simply taking a clear input and returning an output.
While this simplicity makes for great learning material, real-life JavaScript projects often diverge from this ideal. We’ll explore the challenges that arise when testing asynchronous code and functions that rely on external dependencies or produce side effects. For instance, in many applications, we use callbacks, promises, or async/await to interact with external systems such as databases. These interfaces introduce complexities that can make testing more difficult.
Another reason real-life projects are more...