| name | testing-nestjs-applications |
| description | Tests NestJS applications at unit, integration, and e2e levels using Test.createTestingModule and Supertest with hand-rolled fakes. Use when writing tests for use cases, controllers, or full HTTP flows. |
| license | MIT |
Testing NestJS Applications
When to use
- Writing tests for use cases, controllers, or full HTTP flows
- Setting up test strategy for a NestJS feature
- Replacing mocks with hand-rolled fakes
Core rules
- Unit tests: domain entities with pure assertions (no NestJS testing module)
- Use-cases tested with hand-rolled fakes (not jest.mock)
- Integration tests:
Test.createTestingModule with real or fake providers
- E2E tests: Supertest against bootstrapped app
- No
jest.mock for internal modules; only for third-party if needed
Reference shape (TypeScript)
Unit Test (Domain Entity)
import { User } from './user.entity';
describe('User', () => {
it('creates with valid email', () => {
const email = Email.create('test@example.com');
const user = User.create('1', email);
expect(user.id).toBe('1');
});
});
Integration Test (Use-Case with Fake)
import { Test } from '@nestjs/testing';
import { CreateUserUseCase } from './create-user.use-case';
import { FakeUserRepository } from './fakes/fake-user.repository';
describe('CreateUserUseCase', () => {
let useCase: CreateUserUseCase;
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
CreateUserUseCase,
{ provide: 'UserRepository', useClass: FakeUserRepository },
],
}).compile();
useCase = module.get(CreateUserUseCase);
});
it('creates user', async () => {
const result = await useCase.execute({ id: '1', email: 'test@example.com' });
expect(result.success).toBe(true);
});
});
Examples — Do
const module = await Test.createTestingModule({
controllers: [UserController],
providers: [CreateUserUseCase, { provide: 'UserRepository', useClass: FakeUserRepository }],
}).compile();
Examples — Don't
jest.mock('../../application/user.repository');
Checklist
See reference/test-patterns.md for full patterns.