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:
-
Go to
src/web
, and create a file namedweb.test.ts
. -
Now, in order to test the web server, we need to go back to the
createServer
function insrc/web/index.ts
and export theApplication
object it creates insrc/web/index.ts
:
Get hands-on with 1400+ tech skills courses.