4 Testing back-end applications

 

This chapter covers:

  • Structuring the test environment for you back-end
  • Testing your server's routes and middlewares
  • Dealing with external dependencies and services
  • Eliminating non-determinism
  • How to test your backend efficiently, cutting costs and maintaining quality

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 front-end and back-end 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: back-ends written in JavaScript.

What you have already learned about organising 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 back-end is significantly different from testing other kinds of programs like standalone modules or front-end 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 middlewares

4.3 Dealing with external dependencies

4.3.1 Integrations with databases

4.3.2 Integrations with other APIs

4.4 Eliminating non-determinism

4.4.1 Parallelism and shared resources

4.4.2 Dealing with time

4.5 Reducing costs while preserving quality

4.5.1 Reducing overlap between tests

4.5.2 Creating transitive guarantees

4.5.3 Turning assertions into pre-conditions

4.6 Summary