Chapter 10. Testing with Lab, Code, and server.inject()

 

This chapter covers

  • Writing and running tests with Lab
  • Writing assertions with Code
  • Testing hapi servers
  • Using test spies, stubs, and mocks

Testing is a fundamental practice in the production of quality software, but there are a lot of dogma and strong opinions around testing. Regardless of whether you believe in test-first development or you write all your tests after you’ve finished building features, you should be writing tests.

The hapi contributors themselves are fully committed to testing every line of code they write. All hapi.js core projects are tested to 100% coverage. This 100% rule is enforced by default by hapi’s test runner, called Lab.

Aside from its use in the development of hapi itself, Lab is a tool that can be used by application developers to test their own software. In fact many of the tools that are used to write hapi itself are available for you to use to achieve the same level of quality and confidence in your code. These utilities include Lab (the test runner) and Code (an assertion library). There are also several other features built into hapi to make testing easier, such as the server.inject() method. This chapter covers these tools, along with some other important testing-related concepts like spies, stubs, and mocks.

10.1. Introduction to Lab

10.2. Making assertions with the Code assertion library

10.3. Testing hapi servers with server.inject()

10.4. Leveling up Lab

10.5. Testing difficult-to-test code with stubs, spies, and monkey-patching

10.6. Summary

sitemap