Nest.js Tutorial

Integration tests with the Drizzle ORM

Marcin Wanago
Uncategorized

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 signUp method we wrote in the previous parts of this series.

authentication.service.ts
1import { Injectable } from '@nestjs/common';
2import { UsersService } from '../users/users.service';
3import * as bcrypt from 'bcrypt';
4import { SignUpDto } from './dto/sign-up.dto';
5 
6@Injectable()
7export class AuthenticationService {
8  constructor(private readonly usersService: UsersService) {}
9 
10  async signUp(signUpData: SignUpDto) {
11    const hashedPassword = await bcrypt.hash(signUpData.password, 10);
12    return this.usersService.create({
13      name: signUpData.name,
14      email: signUpData.email,
15      phoneNumber: signUpData.phoneNumber,
16      password: hashedPassword,
17      address: signUpData.address,
18    });
19  }
20 
21  // ...
22}

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

users.service.ts
1import { Injectable } from '@nestjs/common';
2import { UserDto } from './user.dto';
3import { DrizzleService } from '../database/drizzle.service';
4import { databaseSchema } from '../database/database-schema';
5import { PostgresErrorCode } from '../database/postgres-error-code.enum';
6import { UserAlreadyExistsException } from './user-already-exists.exception';
7import { isDatabaseError } from '../database/databse-error';
8 
9@Injectable()
10export class UsersService {
11  constructor(private readonly drizzleService: DrizzleService) {}
12 
13  async create(user: UserDto) {
14    try {
15      const createdUsers = await this.drizzleService.db
16        .insert(databaseSchema.users)
17        .values(user)
18        .returning();
19 
20      return createdUsers.pop();
21    } catch (error) {
22      if (
23        isDatabaseError(error) &&
24        error.code === PostgresErrorCode.UniqueViolation
25      ) {
26        throw new UserAlreadyExistsException(user.email);
27      }
28      throw error;
29    }
30  }
31 
32  // ...
33}

The create 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 signUp 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 UserAlreadyExistsException error thrown by the create method.

Since we want to test how the AuthenticationService integrates with the UsersService, we won’t be mocking the UsersService. Instead, let’s mock the DrizzleService 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 insert, values, and returning functions.

1const createdUsers = await this.drizzleService.db
2  .insert(databaseSchema.users)
3  .values(user)
4  .returning();

To handle that in our tests, we can use the mockReturnThis() 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
1import { AuthenticationService } from './authentication.service';
2import { JwtModule } from '@nestjs/jwt';
3import { ConfigModule } from '@nestjs/config';
4import { Test } from '@nestjs/testing';
5import { UsersService } from '../users/users.service';
6import { SignUpDto } from './dto/sign-up.dto';
7import { DrizzleService } from '../database/drizzle.service';
8 
9jest.mock('bcrypt', () => ({
10  hash: () => {
11    return Promise.resolve('hashed-password');
12  },
13}));
14 
15describe('The AuthenticationService', () => {
16  let authenticationService: AuthenticationService;
17  let drizzleInsertReturningMock: jest.Mock;
18  let drizzleInsertValuesMock: jest.Mock;
19  let signUpData: SignUpDto;
20  beforeEach(async () => {
21    drizzleInsertValuesMock = jest.fn().mockReturnThis();
22    drizzleInsertReturningMock = jest.fn().mockResolvedValue([]);
23    signUpData = {
24      email: 'john@smith.com',
25      name: 'John',
26      password: 'strongPassword123',
27      phoneNumber: '123456789',
28    };
29    const module = await Test.createTestingModule({
30      providers: [
31        AuthenticationService,
32        UsersService,
33        {
34          provide: DrizzleService,
35          useValue: {
36            db: {
37              insert: jest.fn().mockReturnThis(),
38              values: drizzleInsertValuesMock,
39              returning: drizzleInsertReturningMock,
40            },
41          },
42        },
43      ],
44      imports: [
45        ConfigModule.forRoot(),
46        JwtModule.register({
47          secretOrPrivateKey: 'Secret key',
48        }),
49      ],
50    }).compile();
51 
52    authenticationService = await module.get(AuthenticationService);
53  });
54  describe('when the signUp function is called', () => {
55    it('should insert the user using the Drizzle ORM', async () => {
56      await authenticationService.signUp(signUpData);
57      expect(drizzleInsertValuesMock).toHaveBeenCalledWith({
58        ...signUpData,
59        password: 'hashed-password',
60      });
61    });
62  });
63});

Testing the result of the method

Now, let’s test if the signUp method returns a valid user in various cases. In the first case, the DrizzleService 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 drizzleInsertReturningMock for each test using the mockResolvedValue and mockImplementation functions.

authentication.service.test.ts
1import { AuthenticationService } from './authentication.service';
2import { JwtModule } from '@nestjs/jwt';
3import { ConfigModule } from '@nestjs/config';
4import { Test } from '@nestjs/testing';
5import { UsersService } from '../users/users.service';
6import { SignUpDto } from './dto/sign-up.dto';
7import { DrizzleService } from '../database/drizzle.service';
8import { InferSelectModel } from 'drizzle-orm';
9import { databaseSchema } from '../database/database-schema';
10import { DatabaseError } from '../database/databse-error';
11import { PostgresErrorCode } from '../database/postgres-error-code.enum';
12import { UserAlreadyExistsException } from '../users/user-already-exists.exception';
13 
14jest.mock('bcrypt', () => ({
15  hash: () => {
16    return Promise.resolve('hashed-password');
17  },
18}));
19 
20describe('The AuthenticationService', () => {
21  let authenticationService: AuthenticationService;
22  let drizzleInsertReturningMock: jest.Mock;
23  let drizzleInsertValuesMock: jest.Mock;
24  let signUpData: SignUpDto;
25  beforeEach(async () => {
26    drizzleInsertValuesMock = jest.fn().mockReturnThis();
27    drizzleInsertReturningMock = jest.fn().mockResolvedValue([]);
28    signUpData = {
29      email: 'john@smith.com',
30      name: 'John',
31      password: 'strongPassword123',
32      phoneNumber: '123456789',
33    };
34    const module = await Test.createTestingModule({
35      providers: [
36        AuthenticationService,
37        UsersService,
38        {
39          provide: DrizzleService,
40          useValue: {
41            db: {
42              insert: jest.fn().mockReturnThis(),
43              values: drizzleInsertValuesMock,
44              returning: drizzleInsertReturningMock,
45            },
46          },
47        },
48      ],
49      imports: [
50        ConfigModule.forRoot(),
51        JwtModule.register({
52          secretOrPrivateKey: 'Secret key',
53        }),
54      ],
55    }).compile();
56 
57    authenticationService = await module.get(AuthenticationService);
58  });
59  
60  // ...
61  
62  describe('when the DrizzleService returns a valid user', () => {
63    let createdUser: InferSelectModel<typeof databaseSchema.users>;
64    beforeEach(() => {
65      createdUser = {
66        ...signUpData,
67        id: 1,
68        addressId: null,
69      };
70      drizzleInsertReturningMock.mockResolvedValue([createdUser]);
71    });
72    it('should return the user as well', async () => {
73      const result = await authenticationService.signUp(signUpData);
74      expect(result).toBe(createdUser);
75    });
76  });
77  
78  describe('when the DrizzleService throws the UniqueViolation error', () => {
79    beforeEach(() => {
80      const databaseError: DatabaseError = {
81        code: PostgresErrorCode.UniqueViolation,
82        table: 'users',
83        detail: 'Key (email)=(john@smith.com) already exists.',
84      };
85      drizzleInsertReturningMock.mockImplementation(() => {
86        throw databaseError;
87      });
88    });
89    it('should throw the ConflictException', () => {
90      return expect(async () => {
91        await authenticationService.signUp(signUpData);
92      }).rejects.toThrow(UserAlreadyExistsException);
93    });
94  });
95});

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.

1npm install supertest @types/supertest

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

categories.controller.test.ts
1import { INestApplication } from '@nestjs/common';
2import { Test } from '@nestjs/testing';
3import { CategoriesService } from './categories.service';
4import { DrizzleService } from '../database/drizzle.service';
5import { CategoriesController } from './categories.controller';
6 
7describe('The CategoriesController', () => {
8  let app: INestApplication;
9  let findFirstMock: jest.Mock;
10  beforeEach(async () => {
11    findFirstMock = jest.fn();
12    const module = await Test.createTestingModule({
13      providers: [
14        CategoriesService,
15        {
16          provide: DrizzleService,
17          useValue: {
18            db: {
19              query: {
20                categories: {
21                  findFirst: findFirstMock,
22                },
23              },
24            },
25          },
26        },
27      ],
28      controllers: [CategoriesController],
29      imports: [],
30    }).compile();
31 
32    app = module.createNestApplication();
33    await app.init();
34  });
35  // ...
36});

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 DrizzleService accordingly.

categories.controller.test.ts
1import * as request from 'supertest';
2import { INestApplication } from '@nestjs/common';
3 
4describe('The CategoriesController', () => {
5  let app: INestApplication;
6  let findFirstMock: jest.Mock;
7  // ...
8  describe('when the GET /categories/:id endpoint is called', () => {
9    describe('and the category with a given id exists', () => {
10      beforeEach(() => {
11        findFirstMock.mockResolvedValue({
12          id: 1,
13          name: 'My category',
14          categoriesArticles: [],
15        });
16      });
17      it('should respond with the category', () => {
18        return request(app.getHttpServer()).get('/categories/1').expect({
19          id: 1,
20          name: 'My category',
21          articles: [],
22        });
23      });
24    });
25    describe('and the category with a given id does not exist', () => {
26      beforeEach(() => {
27        findFirstMock.mockResolvedValue(undefined);
28      });
29      it('should respond with the 404 status', () => {
30        return request(app.getHttpServer()).get('/categories/2').expect(404);
31      });
32    });
33  });
34});

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 JwtAuthenticationGuard.

categories.controller.test.ts
1import { ExecutionContext, INestApplication } from '@nestjs/common';
2import { Test } from '@nestjs/testing';
3import { CategoriesService } from './categories.service';
4import { DrizzleService } from '../database/drizzle.service';
5import { CategoriesController } from './categories.controller';
6import { JwtAuthenticationGuard } from '../authentication/jwt-authentication.guard';
7 
8describe('The CategoriesController', () => {
9  let app: INestApplication;
10  let findFirstMock: jest.Mock;
11  let drizzleInsertReturningMock: jest.Mock;
12  beforeEach(async () => {
13    drizzleInsertReturningMock = jest.fn().mockResolvedValue([]);
14    findFirstMock = jest.fn();
15    const module = await Test.createTestingModule({
16      providers: [
17        CategoriesService,
18        {
19          provide: DrizzleService,
20          useValue: {
21            db: {
22              query: {
23                categories: {
24                  findFirst: findFirstMock,
25                },
26              },
27              insert: jest.fn().mockReturnThis(),
28              values: jest.fn().mockReturnThis(),
29              returning: drizzleInsertReturningMock,
30            },
31          },
32        },
33      ],
34      controllers: [CategoriesController],
35      imports: [],
36    })
37      .overrideGuard(JwtAuthenticationGuard)
38      .useValue({
39        canActivate: (context: ExecutionContext) => {
40          const req = context.switchToHttp().getRequest();
41          req.user = {
42            id: 1,
43            name: 'John Smith',
44          };
45          return true;
46        },
47      })
48      .compile();
49 
50    app = module.createNestApplication();
51    await app.init();
52  });
53  // ...
54});

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

categories.controller.test.ts
1import * as request from 'supertest';
2import { ExecutionContext, INestApplication } from '@nestjs/common';
3import { Test } from '@nestjs/testing';
4import { CategoriesService } from './categories.service';
5import { DrizzleService } from '../database/drizzle.service';
6import { CategoriesController } from './categories.controller';
7import { JwtAuthenticationGuard } from '../authentication/jwt-authentication.guard';
8import { CategoryDto } from './dto/category.dto';
9 
10describe('The CategoriesController', () => {
11  let app: INestApplication;
12  let findFirstMock: jest.Mock;
13  let drizzleInsertReturningMock: jest.Mock;
14  beforeEach(async () => {
15    drizzleInsertReturningMock = jest.fn().mockResolvedValue([]);
16    findFirstMock = jest.fn();
17    const module = await Test.createTestingModule({
18      providers: [
19        CategoriesService,
20        {
21          provide: DrizzleService,
22          useValue: {
23            db: {
24              query: {
25                categories: {
26                  findFirst: findFirstMock,
27                },
28              },
29              insert: jest.fn().mockReturnThis(),
30              values: jest.fn().mockReturnThis(),
31              returning: drizzleInsertReturningMock,
32            },
33          },
34        },
35      ],
36      controllers: [CategoriesController],
37      imports: [],
38    })
39      .overrideGuard(JwtAuthenticationGuard)
40      .useValue({
41        canActivate: (context: ExecutionContext) => {
42          const req = context.switchToHttp().getRequest();
43          req.user = {
44            id: 1,
45            name: 'John Smith',
46          };
47          return true;
48        },
49      })
50      .compile();
51 
52    app = module.createNestApplication();
53    await app.init();
54  });
55  
56  // ...
57  
58  describe('when the POST /categories endpoint is called', () => {
59    describe('and the correct data is provided', () => {
60      let categoryData: CategoryDto;
61      beforeEach(() => {
62        categoryData = {
63          name: 'New category',
64        };
65        drizzleInsertReturningMock.mockResolvedValue([
66          {
67            id: 2,
68            ...categoryData,
69          },
70        ]);
71      });
72      it('should respond with the new category', () => {
73        return request(app.getHttpServer())
74          .post('/categories')
75          .send(categoryData)
76          .expect({
77            id: 2,
78            ...categoryData,
79          });
80      });
81    });
82  });
83});

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.