Gain confidence in your code through rigorous testing strategies from unit to end-to-end.
Welcome to the world of unit testing in NestJS! In this section, we'll explore how to write effective unit tests using Jest. Unit testing is a crucial part of modern software development, ensuring that your code works as expected and catches issues early.
First, ensure you have the necessary dependencies installed:
npm install --save-dev jest ts-jest @types/jest @nestjs/jest
Create a Jest configuration file ( jest.config.ts ) in your project root:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: {
'ts-jest': {
tsConfigFile: './tsconfig.json'
}
},
transformIgnorePatterns: ['<rootDir>/node_modules/']
};
Each service should have a corresponding test file. For example, for src/users/services/user.service.ts , create src/users/services/user.service.spec.ts .
When writing unit tests, you want to isolate the code under test from its dependencies. This is where mocking comes in.
Jest provides powerful tools for spying on function calls and making assertions.
it('should call the repository save method', async () => {
const mockUser = { id: 1, email: 'test@example.com' };
jest.spyOn(userRepository, 'save').mockResolvedValue(mockUser);
const result = await userService.createUser(mockUser);
expect(userRepository.save).toHaveBeenCalledWith(mockUser);
expect(result).toEqual(mockUser);
});
Snapshot testing is useful for verifying that your components render correctly. While more common in UI testing, it can also be used for service layer testing.
it('should return the correct user object', () => {
const mockUser = { id: 1, email: 'test@example.com' };
jest.spyOn(userRepository, 'findOne').mockResolvedValue(mockUser);
const result = await userService.findOne(1);
expect(result).toMatchSnapshot();
});
// src/auth/services/jwt.service.ts
export class JwtService {
sign(payload: object, options?: SignOptions): string;
verify(token: string, options?: VerifyOptions): Promise<object>;
}
// src/auth/services/jwt.service.spec.ts
import { JwtService } from './jwt.service';
describe('JwtService', () => {
const mockPayload = { id: 1, email: 'test@example.com' };
beforeEach(() => {
// Setup any common mocks or state here
});
describe('sign method', () => {
it('should return a valid JWT token', () => {
const jwtService = new JwtService();
const token = jwtService.sign(mockPayload);
expect(token).toBeDefined();
// Additional assertions as needed
});
});
describe('verify method', () => {
it('should correctly verify a valid token', async () => {
const jwtService = new JwtService();
const token = jwtService.sign(mockPayload);
const result = await jwtService.verify(token);
expect(result).toEqual(mockPayload);
});
});
});
Question 1 of 5