Skip to main content

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

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.

Back to the full NestJS roadmap