API with NestJS #168. Integration tests with the Drizzle ORM

Uncategorized

This entry is part 168 of 174 in the API with NestJS

Writing tests for our application helps ensure it works as intended and is reliable. So far, we have written unit tests for our NestJS application that uses the Drizzle ORM. Unit tests help us check if a particular class of a single function functions properly on its own. While unit tests are important, they are not enough. Even if each piece of our system works well alone, it does not yet mean it functions together with other parts of our system.

Introducing integration tests

To test how two or more pieces of our application work together, we write integration tests. Let’s take a look at the method we wrote in the previous parts of this series.

authentication.service.ts

It hashes the provided password and calls the method from the under the hood.

users.service.ts

The method creates the user in the database and handles the error that could happen when we try to sign up a new user with an email already in our database.

There are a few things in the method we could test with integration tests:

  • whether it hashes the password,
  • if it returns the created user if the provided data is valid
  • if it throws the error thrown by the method.

Since we want to test how the integrates with the , we won’t be mocking the . Instead, let’s mock the to ensure we’re not using the real database in our tests.

Writing integration tests does not mean we want to check how all parts of our system work together. Those tests are called end-to-end (E2E) tests and should mimic a real system as close as possible.

An important thing to notice is that we’re chaining the , , and functions.

To handle that in our tests, we can use the method.

Since hashing a password with bcrypt includes a random salt, we should mock the bcrypt library to produce the same output consistently.

authentication.service.test.ts

Testing the result of the method

Now, let’s test if the method returns a valid user in various cases. In the first case, the returns a valid user. In the second case, it throws an error because the email has already been taken.

To handle that, we must adjust our for each test using the and functions.

authentication.service.test.ts

Testing controllers

An alternative approach to writing integration tests is to make HTTP requests to our API. By doing that, we can test multiple layers of our application, from the controllers to the services. To do that, we need to install the SuperTest library.

First, we need to initialize our NestJS application in our tests. We will need the variable to perform the tests with the SuperTest library.

categories.controller.test.ts

Now, let’s use the SuperTest library to make GET requests to our API. When doing that, we can tackle various scenarios, such as categories with a given ID being available or not. To do that, we need to mock our accordingly.

categories.controller.test.ts

POST requests and authentication

We can also make POST requests and send data using the request body to add new categories. However, we must consider that our API requires the user to authenticate before doing that. To deal with that, we need to mock the .

categories.controller.test.ts

Thanks to the above, we can test if adding new categories works as expected.

categories.controller.test.ts

Summary

In this article, we explored the concept of integration tests in NestJS applications using the Drizzle ORM. We discussed why they’re important and how they benefit us. We also implemented two different approaches to integration tests, one of which involved using the SuperTest library to simulate HTTP requests. Thanks to the above, we can test our API more effectively and have a more reliable, stable application.

Series Navigation<< API with NestJS #167. Unit tests with the Drizzle ORMAPI with NestJS #169. Unique IDs with UUIDs using Drizzle ORM and PostgreSQL >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments