When to Use
Use this skill when:
- Writing unit tests for handlers, services, or aggregates
- Creating e2e tests for API endpoints
- Mocking NestJS dependencies (repositories, services, event bus)
- Testing CQRS commands and queries
- Testing Aurora-generated code
- Setting up test fixtures and factories
Testing Philosophy in Aurora/NestJS
Unit Tests: Test individual components in isolation
- Handlers (Command/Query), Services, Aggregates/Value Objects, Mappers
e2e Tests: Test complete flows through the API layer
- REST endpoints, GraphQL resolvers, Authentication, Database interactions
Detailed References
- Unit Testing Patterns — Command/query handlers, mocking repos, mocking external services, bus mocks
- E2E Testing Patterns — REST controllers, GraphQL resolvers
- Aurora-Specific Testing — Value objects, aggregates, test organization
Best Practices
✅ DO
- Isolate tests: Each test should be independent
- Use descriptive names: "should throw error when price is negative"
- Follow AAA pattern: Arrange, Act, Assert
- Mock external dependencies: Database, APIs, services
- Test edge cases: Null, undefined, empty arrays, boundaries
- Test error paths: Exceptions, validation errors
- Use factories/fixtures: Reusable test data builders
- Keep tests fast: Unit tests < 100ms, e2e < 1s
- Clean up: Use
afterEach to reset state
- Test one thing: One assertion per test (when possible)
❌ DON'T
- Don't test implementation details: Test behavior, not internals
- Don't test framework code: Trust NestJS, TypeORM, etc.
- Don't share state: Between tests or describe blocks
- Don't use real database: In unit tests (use mocks)
- Don't skip tests: Fix or remove broken tests
- Don't test getters/setters: Unless they have logic
- Don't duplicate tests: Avoid redundant test cases
- Don't test generated code: Trust Aurora generation (test custom logic only)
- Don't ignore coverage: Aim for >80% on custom code
Coverage Guidelines
Target Coverage:
- Custom handlers: 100% (all custom logic)
- Services: 90%+ (critical business logic)
- Aggregates: 90%+ (domain rules)
- Value Objects: 80%+ (validation logic)
- Controllers/Resolvers: 80%+ (e2e coverage acceptable)
- Generated code: Skip (trust Aurora)
Run Coverage:
npm run test:cov # Unit tests with coverage
npm run test:e2e # e2e tests
npm run test:watch # Watch mode for TDD
Jest Configuration
jest.config.js (typical Aurora setup):
module.exports = {
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: 'src',
testRegex: '.*\\.spec\\.ts$',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
collectCoverageFrom: [
'**/*.(t|j)s',
'!**/*.module.ts',
'!**/*.index.ts',
'!**/node_modules/**',
'!**/dist/**',
'!**/infrastructure/seeds/**',
],
coverageDirectory: '../coverage',
testEnvironment: 'node',
moduleNameMapper: {
'^@app/(.*)$': '<rootDir>/$1',
'^@core/(.*)$': '<rootDir>/@core/$1',
'^@api/(.*)$': '<rootDir>/@api/$1',
},
};
Quick Reference
| Task |
Pattern |
| Test handler |
Test.createTestingModule() + mock repository |
| Mock repository |
Use custom mock class implementing interface |
| Test validation |
Expect exception to be thrown |
| e2e REST |
supertest + app.getHttpServer() |
| e2e GraphQL |
supertest + GraphQL query string |
| Test aggregate |
Call methods + verify events |
| Test VO |
Constructor + validation rules |
| Coverage |
npm run test:cov |
Remember
- Unit tests = Fast, isolated, mock dependencies
- e2e tests = Slow, integrated, real dependencies
- Test custom logic only: Don't test Aurora-generated code
- Mark your code: Use
#region AI-generated code in handlers
- TDD when possible: Write test → Implement → Refactor
1---2name: jest-nestjs3description: Jest + NestJS testing patterns for unit and e2e tests in Aurora projects. Trigger: When writing tests, mocking dependencies, or implementing test coverage in NestJS/Aurora.4license: MIT5---6
7## When to Use
8
9Use this skill when:
10- Writing unit tests for handlers, services, or aggregates
11- Creating e2e tests for API endpoints
12- Mocking NestJS dependencies (repositories, services, event bus)
13- Testing CQRS commands and queries
14- Testing Aurora-generated code
15- Setting up test fixtures and factories
16
17## Testing Philosophy in Aurora/NestJS
18
19**Unit Tests**: Test individual components in isolation
20- Handlers (Command/Query), Services, Aggregates/Value Objects, Mappers
21
22**e2e Tests**: Test complete flows through the API layer
23- REST endpoints, GraphQL resolvers, Authentication, Database interactions
24
25## Detailed References
26
27- [Unit Testing Patterns](unit-testing.md) — Command/query handlers, mocking repos, mocking external services, bus mocks
28- [E2E Testing Patterns](e2e-testing.md) — REST controllers, GraphQL resolvers
29- [Aurora-Specific Testing](aurora-testing.md) — Value objects, aggregates, test organization
30
31## Best Practices
32
33### ✅ DO
34
351. **Isolate tests**: Each test should be independent
362. **Use descriptive names**: "should throw error when price is negative"
373. **Follow AAA pattern**: Arrange, Act, Assert
384. **Mock external dependencies**: Database, APIs, services
395. **Test edge cases**: Null, undefined, empty arrays, boundaries
406. **Test error paths**: Exceptions, validation errors
417. **Use factories/fixtures**: Reusable test data builders
428. **Keep tests fast**: Unit tests < 100ms, e2e < 1s
439. **Clean up**: Use `afterEach` to reset state
4410. **Test one thing**: One assertion per test (when possible)
45
46### ❌ DON'T
47
481. **Don't test implementation details**: Test behavior, not internals
492. **Don't test framework code**: Trust NestJS, TypeORM, etc.
503. **Don't share state**: Between tests or describe blocks
514. **Don't use real database**: In unit tests (use mocks)
525. **Don't skip tests**: Fix or remove broken tests
536. **Don't test getters/setters**: Unless they have logic
547. **Don't duplicate tests**: Avoid redundant test cases
558. **Don't test generated code**: Trust Aurora generation (test custom logic only)
569. **Don't ignore coverage**: Aim for >80% on custom code
57
58## Coverage Guidelines
59
60**Target Coverage:**
61- Custom handlers: 100% (all custom logic)
62- Services: 90%+ (critical business logic)
63- Aggregates: 90%+ (domain rules)
64- Value Objects: 80%+ (validation logic)
65- Controllers/Resolvers: 80%+ (e2e coverage acceptable)
66- Generated code: Skip (trust Aurora)
67
68**Run Coverage:**
69```bash
70npm run test:cov # Unit tests with coverage
71npm run test:e2e # e2e tests
72npm run test:watch # Watch mode for TDD
73```
74
75## Jest Configuration
76
77**jest.config.js** (typical Aurora setup):
78
79```javascript
80module.exports = {
81 moduleFileExtensions: ['js', 'json', 'ts'],
82 rootDir: 'src',
83 testRegex: '.*\\.spec\\.ts$',
84 transform: {
85 '^.+\\.(t|j)s$': 'ts-jest',
86 },
87 collectCoverageFrom: [
88 '**/*.(t|j)s',
89 '!**/*.module.ts',
90 '!**/*.index.ts',
91 '!**/node_modules/**',
92 '!**/dist/**',
93 '!**/infrastructure/seeds/**',
94 ],
95 coverageDirectory: '../coverage',
96 testEnvironment: 'node',
97 moduleNameMapper: {
98 '^@app/(.*)$': '<rootDir>/$1',
99 '^@core/(.*)$': '<rootDir>/@core/$1',
100 '^@api/(.*)$': '<rootDir>/@api/$1',
101 },
102};
103```
104
105## Quick Reference
106
107| Task | Pattern |
108|------|---------|
109| Test handler | `Test.createTestingModule()` + mock repository |
110| Mock repository | Use custom mock class implementing interface |
111| Test validation | Expect exception to be thrown |
112| e2e REST | `supertest` + `app.getHttpServer()` |
113| e2e GraphQL | `supertest` + GraphQL query string |
114| Test aggregate | Call methods + verify events |
115| Test VO | Constructor + validation rules |
116| Coverage | `npm run test:cov` |
117
118## Remember
119
120- **Unit tests** = Fast, isolated, mock dependencies
121- **e2e tests** = Slow, integrated, real dependencies
122- **Test custom logic only**: Don't test Aurora-generated code
123- **Mark your code**: Use `#region AI-generated code` in handlers
124- **TDD when possible**: Write test → Implement → Refactor