| 1 | # Testing Requirements |
| 2 | |
| 3 | ## Minimum Test Coverage: 80% |
| 4 | |
| 5 | Test Types (ALL required): |
| 6 | 1. **Unit Tests** - Individual functions, utilities, services |
| 7 | 2. **Integration Tests** - API endpoints, database operations |
| 8 | 3. **E2E Tests** - Critical user flows |
| 9 | |
| 10 | ## Running Tests |
| 11 | |
| 12 | ```bash |
| 13 | # Run tests for a specific project |
| 14 | pnpm nx run <project>:test |
| 15 | |
| 16 | # Run E2E tests |
| 17 | pnpm nx run e2e:e2e |
| 18 | |
| 19 | # Run affected tests |
| 20 | pnpm nx affected -t test |
| 21 | ``` |
| 22 | |
| 23 | ## Test File Conventions |
| 24 | |
| 25 | - Test files: `*.spec.ts`, colocated with source files |
| 26 | - Test runner: Vitest (via `@nx/vite:test`) |
| 27 | - E2E infrastructure: Docker Compose (`e2e/docker-compose.e2e.yml`) |
| 28 | |
| 29 | ## NestJS Testing Patterns |
| 30 | |
| 31 | **Unit testing services:** |
| 32 | ```typescript |
| 33 | const module = await Test.createTestingModule({ |
| 34 | providers: [ |
| 35 | OrderService, |
| 36 | { provide: OrderRepository, useValue: mockOrderRepository }, |
| 37 | ], |
| 38 | }).compile() |
| 39 | ``` |
| 40 | |
| 41 | **Unit testing controllers:** Mock services, verify routing and VO transformation. |
| 42 | |
| 43 | **Repository tests:** Test in integration/E2E only (repositories are thin wrappers over Mongoose). |
| 44 | |
| 45 | ## Test-Driven Development |
| 46 | |
| 47 | MANDATORY workflow: |
| 48 | 1. Write test first (RED) |
| 49 | 2. Run test - it should FAIL |
| 50 | 3. Write minimal implementation (GREEN) |
| 51 | 4. Run test - it should PASS |
| 52 | 5. Refactor (IMPROVE) |
| 53 | 6. Verify coverage (80%+) |
| 54 | |
| 55 | ## Troubleshooting Test Failures |
| 56 | |
| 57 | 1. Use **tdd-guide** agent |
| 58 | 2. Check test isolation |
| 59 | 3. Verify mocks are correct |
| 60 | 4. Fix implementation, not tests (unless tests are wrong) |
| 61 |