Careeroza
Testing Strategy
The Test Pyramid for a NestJS API
/\
/e2e\ few: whole app over HTTP (Supertest), real or test DB
/------\
/integr. \ some: module + real database (Testcontainers / docker)
/----------\
/ unit \ many: one class, dependencies mocked, milliseconds each
/--------------\| Command | Runs |
|---|---|
npm test | Unit tests: src/**/*.spec.ts |
npm run test:watch | Unit tests on file change |
npm run test:cov | Unit tests + coverage report |
npm run test:e2e | test/**/*.e2e-spec.ts |
Why DI Makes Testing Easy
Services receive their dependencies through the constructor. In a test you pass fakes instead of the real database, payment gateway or SMS sender. No network, no database, fast and repeatable.