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
    /--------------\
CommandRuns
npm testUnit tests: src/**/*.spec.ts
npm run test:watchUnit tests on file change
npm run test:covUnit tests + coverage report
npm run test:e2etest/**/*.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.