6 min read
Original source

Integration tests with the Drizzle ORM

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…

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 import { Injectable } from '@nestjs/common'; import { UsersService } from '../users/users.service'; import * as bcrypt from 'bcrypt'; import { SignUpDto } from './dto/sign-up.dto'; @Injectable() export class AuthenticationService { constructor(private readonly usersService: UsersService) {} async signUp(signUpData: SignUpDto) { const hashedPassword = await bcrypt.hash(signUpData.password, 10); return this.usersService.create({ name: signUpData.name, email: signUpData.email, phoneNumber: signUpData.phoneNumber, password: hashedPassword, address: signUpData.address, }); } // ... }It hashes the provided password and calls the create method from the UsersService under the hood. users.service.ts import { Injectable } from '@nestjs/common'; import { UserDto } from './user.dto'; import { DrizzleService } from '../database/drizzle.service'; import { databaseSchema } from '../database/database-schema'; import { PostgresErrorCode } from '../database/postgres-error-code.enum'; import { UserAlreadyExistsException } from './user-already-exists.exception'; import { isDatabaseError } from '../database/databse-error'; @Injectable() export class UsersService { constructor(private readonly drizzleService: DrizzleService) {} async create(user: UserDto) { try { const createdUsers = await this.drizzleService.db .insert(databaseSchema.users) .values(user) .returning(); return createdUsers.pop(); } catch (error) { if ( isDatabaseError(error) && error.code === PostgresErrorCode.UniqueViolation ) { throw new UserAlreadyExistsException(user.email); } throw error; } } // ... }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.const createdUsers = await this.drizzleService.db .insert(databaseSchema.users) .values(user) .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 import { AuthenticationService } from './authentication.service'; import { JwtModule } from '@nestjs/jwt'; import { ConfigModule } from '@nestjs/config'; import { Test } from '@nestjs/testing'; import { UsersService } from '../users/users.service'; import { SignUpDto } from './dto/sign-up.dto'; import { DrizzleService } from '../database/drizzle.service'; jest.mock('bcrypt', () => ({ hash: () => { return Promise.resolve('hashed-password'); }, })); describe('The AuthenticationService', () => { let authenticationService: AuthenticationService; let drizzleInsertReturningMock: jest.Mock; let drizzleInsertValuesMock: jest.Mock; let signUpData: SignUpDto; beforeEach(async () => { drizzleInsertValuesMock = jest.fn().mockReturnThis(); drizzleInsertReturningMock = jest.fn().mockResolvedValue([]); signUpData = { email: 'john@smith.com', name: 'John', password: 'strongPassword123', phoneNumber: '123456789', }; const module = await Test.createTestingModule({ providers: [ AuthenticationService, UsersService, { provide: DrizzleService, useValue: { db: { insert: jest.fn().mockReturnThis(), values: drizzleInsertValuesMock, returning: drizzleInsertReturningMock, }, }, }, ], imports: [ ConfigModule.forRoot(), JwtModule.register({ secretOrPrivateKey: 'Secret key', }), ], }).compile(); authenticationService = await module.get(AuthenticationService); }); describe('when the signUp function is called', () => { it('should insert the user using the Drizzle ORM', async () => { await authenticationService.signUp(signUpData); expect(drizzleInsertValuesMock).toHaveBeenCalledWith({ ...signUpData, password: 'hashed-password', }); }); }); }); 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 import { AuthenticationService } from './authentication.service'; import { JwtModule } from '@nestjs/jwt'; import { ConfigModule } from '@nestjs/config'; import { Test } from '@nestjs/testing'; import { UsersService } from '../users/users.service'; import { SignUpDto } from './dto/sign-up.dto'; import { DrizzleService } from '../database/drizzle.service'; import { InferSelectModel } from 'drizzle-orm'; import { databaseSchema } from '../database/database-schema'; import { DatabaseError } from '../database/databse-error'; import { PostgresErrorCode } from '../database/postgres-error-code.enum'; import { UserAlreadyExistsException } from '../users/user-already-exists.exception'; jest.mock('bcrypt', () => ({ hash: () => { return Promise.resolve('hashed-password'); }, })); describe('The AuthenticationService', () => { let authenticationService: AuthenticationService; let drizzleInsertReturningMock: jest.Mock; let drizzleInsertValuesMock: jest.Mock; let signUpData: SignUpDto; beforeEach(async () => { drizzleInsertValuesMock = jest.fn().mockReturnThis(); drizzleInsertReturningMock = jest.fn().mockResolvedValue([]); signUpData = { email: 'john@smith.com', name: 'John', password: 'strongPassword123', phoneNumber: '123456789', }; const module = await Test.createTestingModule({ providers: [ AuthenticationService, UsersService, { provide: DrizzleService, useValue: { db: { insert: jest.fn().mockReturnThis(), values: drizzleInsertValuesMock, returning: drizzleInsertReturningMock, }, }, }, ], imports: [ ConfigModule.forRoot(), JwtModule.register({ secretOrPrivateKey: 'Secret key', }), ], }).compile(); authenticationService = await module.get(AuthenticationService); }); // ... describe('when the DrizzleService returns a valid user', () => { let createdUser: InferSelectModel; beforeEach(() => { createdUser = { ...signUpData, id: 1, addressId: null, }; drizzleInsertReturningMock.mockResolvedValue([createdUser]); }); it('should return the user as well', async () => { const result = await authenticationService.signUp(signUpData); expect(result).toBe(createdUser); }); }); describe('when the DrizzleService throws the UniqueViolation error', () => { beforeEach(() => { const databaseError: DatabaseError = { code: PostgresErrorCode.UniqueViolation, table: 'users', detail: 'Key (email)=(john@smith.com) already exists.', }; drizzleInsertReturningMock.mockImplementation(() => { throw databaseError; }); }); it('should throw the ConflictException', () => { return expect(async () => { await authenticationService.signUp(signUpData); }).rejects.toThrow(UserAlreadyExistsException); }); }); }); 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.npm install supertest @types/supertestFirst, 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 import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; import { CategoriesService } from './categories.service'; import { DrizzleService } fro

Integration tests with the Drizzle ORM | NestJS.io