4 Testing backend applications

 

This chapter covers

  • Structuring the test environment for your backend
  • Testing your server’s routes and middleware
  • Dealing with databases in your tests
  • Managing dependencies on external services

For many years, JavaScript was known as a client-side language only. It used to run only within browsers. Once Node.js appeared, circa 2009, people started writing both the frontend and backend of their applications using JavaScript.

Node.js enabled an entirely new kind of JavaScript application and heavily contributed to shaping the JavaScript testing ecosystem. Because JavaScript developers were now able to implement different types of applications, they also had to come up with new ways of testing them and new tools for doing so.

In this chapter, I will focus on how to test the most notable kind of application that Node has enabled: backends written in JavaScript.

What you have already learned about organizing tests, assertions, and test doubles will still be crucial in this chapter. In it, you will learn the nuances associated with applying these techniques in the context of a server.

Testing a backend is significantly different from testing other kinds of programs like standalone modules or frontend applications. It involves dealing with the filesystem, databases, HTTP requests, and third-party services.

4.1 Structuring a testing environment

4.1.1 End-to-end testing

4.1.2 Integration testing

4.1.3 Unit testing

4.2 Testing HTTP endpoints

4.2.1 Testing middleware

4.3 Dealing with external dependencies

4.3.1 Integrations with databases

4.3.2 Integrations with other APIs

Summary