Chapter 10. Tests: The key to confident code

 

This chapter covers

  • Assertions, custom assertions, and automated testing
  • Ensuring things fail as expected
  • Mocha and TAP
  • Testing web applications
  • Continuous integration
  • Database fixtures

Imagine that you wanted to add a new currency to an online shop. First you’d add a test to define the expected calculations: subtotal, tax, and the total. Then you’d write code to make this test pass. This chapter will help you learn how to write tests by looking at Node’s built-in features for testing: the assert module and test scripts that you can set in your package.json file. We also introduce two major test frameworks: Mocha and node-tap.

10.1. Introduction to testing with Node

10.2. Writing simple tests with assertions

10.3. Test harnesses

10.4. Test frameworks

10.5. Tools for tests

10.6. Further reading

10.7. Summary