API with NestJS #103. Integration tests with Prisma

NestJS

This entry is part 103 of 180 in the API with NestJS

In the previous part of this series, we learned how to write unit tests in a NestJS project with Prisma. Unit tests help verify if individual components of our system work as expected on their own. However, while useful, they don’t guarantee that our API functions correctly as a whole. In this article, we deal with it by implementing integration tests.

You can find all of the code from this article in this repository.

Introducing integration tests

An integration test verifies if multiple parts of our application work together. We can do that by testing the integration of two or more pieces of our system.

Let’s investigate the method in our .

authentication.service.ts

When we look closely, we can distinguish a few cases. The method:

  • should throw an error if the user can’t be found,
  • should throw an error if the provided password is not valid,
  • should return the user if the user is found and the provided password is correct.

In the previous article, we tested the in isolation and mocked the . This time, let’s test how they work together.

Even though we are writing an integration test, it does not necessarily mean we want to include every part of our system. For example, in this particular case, we still want to mock our class to avoid making actual database calls.

authentication.service.test.ts

First, we provide a Jest function in our mock to be able to change the implementation per test.

By doing that, we can alter the method to cover all of the cases:

  • the method returns the requested user,
  • the method does not find the user.
authentication.service.test.ts

The AuthenticationService
when the getAuthenticatedUser method is called
and the user can be found in the database
and a correct password is provided
✓ should return the new user
and an incorrect password is provided
✓ should throw the BadRequestException
and the user can not be found in the database
✓ should throw the BadRequestException

By not mocking the in the above tests, we ensured that it integrates as expected with the . Whenever we call the method in our test, it uses the actual method under the hood.

Testing controllers using API calls

Another approach we could take to our integration testing is to perform HTTP requests to our API. This allows us to test multiple application layers, starting with the controllers.

To perform the tests, we need the SuperTest library.

Let’s start by testing the most basic case with registering the new user.

authentication.controller.test.ts

The AuthenticationController
when the register endpoint is called
and valid data is provided
and the user is successfully created in the database
✓ should return the new user without the password

In the above code, we create a testing module that includes the . This way, we can make an HTTP request to the endpoint.

Besides the most basic case, we can also test what happens if the email is already taken. To do that, we must ensure our mocked method throws the .

authentication.controller.test.ts

The AuthenticationController
when the register endpoint is called
and valid data is provided
and the user is successfully created in the database
✓ should return the new user without the password
and the email is already taken
✓ should result in 400 Bad Request

Testing the validation

Our NestJS application validates the data sent when making POST requests. For example, we check if the provided registration data contains a valid email, name, and password.

register.dto.ts

When testing the above, it is crucial to attach the to our testing module.

The AuthenticationController
when the register endpoint is called
and valid data is provided
and the user is successfully created in the database
✓ should return the new user without the password
and the email is already taken
✓ should result in 400 Bad Request
and the email is missing
✓ should result in 400 Bad Request

Summary

In this article, we’ve gone through the idea of writing integration tests. As an example, we’ve used a NestJS application using Prisma. When doing so, we had to learn how to mock Prisma properly, including throwing errors. All of the above will definitely help us ensure that our app works as expected.

Series Navigation<< API with NestJS #102. Writing unit tests with PrismaAPI with NestJS #104. Writing transactions with Prisma >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments