Testing Your API with Lab

In this section, we will have a brief look at writing unit tests for Hapi.js APIs. Testing is a huge topic that perhaps requires a whole course on its own, but in this section, we will be introducing the essential parts to get you up and running.

Let's first underscore the importance of writing unit tests for your API:

  • Maintainability: This is what I consider as the most important value of adding tests to your software. When you have tests, you can be confident to come back months later and, modify your code without worrying whether you broke anything by your update.
  • Requirement specifications: Tests make sure that your code meets the requirements. For our example, we started off by implementing the requirements since we wanted to pass across some basic concepts. But in practice, it is way better to start off with the tests before implementing your routes.
  • Automated testing: You realized that in our previous examples, we kept checking our API client (Insomnia) to see if our API was working correctly; this can be a little cumbersome. With tests, you don't have to worry about this once you have written correct tests.

Hapi.js conventionally uses Lab (https://github.com/hapijs/lab) as its testing framework. We're going to write a few tests for our API in the next exercise.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset