Testing the Web Server

Learn how to create a web module and test if it connects to the web server.

We'll cover the following

Previously, we wrote a unit test and an integration test, but we still haven’t written any tests for our application’s interface—the web part of it, which is using HTTP. That’s what we’ll do in this lesson. We’ll learn how can we test the logic living in the web layer in isolation, without using any other modules.

So far, we have learned how to test different parts of the application. We started with the business logic, which tests how it integrated with the modules that interacted with persistency (the repository), but the web layer still has no tests.

It’s true that those tests are very important, but we can agree that if the web layer fails, the user will not have access to any of that logic.

That’s what we’ll do in this lesson. We’ll spin up our web server, mock its dependencies, and make a few requests to it to ensure the web unit is working.

Creating a web module’s unit test

Let’s start by creating the web module’s unit test by following these steps:

  1. Go to src/web, and create a file named web.test.ts.

  2. Now, in order to test the web server, we need to go back to the createServer function in src/web/index.ts and export the Application object it creates in src/web/index.ts:

Get hands-on with 1200+ tech skills courses.