# NestJS Testing

> Unit and E2E testing strategies with Docker.

- Skill: `majiayu000/nestjs-testing-2` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds add majiayu000/nestjs-testing-2`
- Raw SKILL.md: https://api.skillmd.com/api/skills/majiayu000/nestjs-testing-2/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: DevOps & Infra
- Author: majiayu000 (https://skillmd.com/u/majiayu000)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/majiayu000/nestjs-testing-2

---


# NestJS Testing Standards

## **Priority: P2 (MAINTENANCE)**

Unit testing, integration testing, and E2E testing patterns for NestJS applications.

1. **Unit Tests**: Isolated logic (Services). Mock **all** dependencies (`jest.fn()`).
2. **E2E Tests**: Full lifecycle (`test/app.e2e-spec.ts`).
   - **Rule**: Use a **real** Test Database (Docker). Never mock the database in E2E.
   - **Idempotency**: **Mandatory** cleanup. Use a transaction rollback strategy or explicit `TRUNCATE` in `afterEach` to ensure tests don't leak state.

## E2E Best Practices (Pro)

- **Overriding**: Use `.overrideProvider(AuthGuard).useValue({ canActivate: () => true })` to bypass security in functional flow tests.
- **Factories**: Use Factory patterns (e.g. `mockUserFactory()`) to generate DTOs/Entities. Avoid hardcoded JSON literals.

## Tools

- **Runner**: Jest (swc/ts-jest).
- **Mocks**: `jest.fn()`, `jest.spyOn()`.

## Setup Pattern

```typescript
// Standard Setup
const module: TestingModule = await Test.createTestingModule({
  providers: [
    UsersService,
    { provide: getRepositoryToken(User), useValue: mockRepo },
  ],
}).compile();
```

