Testing for NestJSs
Testing in NestJS uses Jest and the @nestjs/testing package to build isolated test modules. Unit tests replace dependencies with mocks, while end-to-end tests boot the app and send HTTP requests with supertest, so teams can verify behaviour without running every real service.
Stage: Quality and Delivery · Part of the NestJS roadmap · Last reviewed 19 November 2026
What to learn in Testing
- Create a testing module with Test.createTestingModule and compile it
- Mock a provider and override it in a unit test
- Write an end-to-end test of a controller with supertest
- Test guards and pipes by exercising them in isolation
- Reset database state between end-to-end tests
Testing interview questions
How do you mock a dependency in a NestJS unit test?
Register the dependency token in the testing module with useValue or useFactory returning a fake object, such as jest.fn methods. Nest injects the fake in place of the real provider, so the class under test runs without touching a database.
When should you prefer e2e tests over unit tests?
Use e2e tests to check that routing, pipes, guards and serialisation work together, such as an authenticated POST returning the right status. Unit tests suit pure business logic. Use fewer, slower e2e tests for the main flows.
Where Testing fits in the NestJS roadmap
Testing belongs to the Quality and Delivery stage (advanced level). Production.
Other units in this stage
Back to the full NestJS roadmap