Unit Tests
Purpose
Generate unit tests using Jest with proper mocking of dependencies, AAA pattern (Arrange-Act-Assert), and descriptive test names.
Why Jest Instead of node:test?
This repository uses Jest for ALL testing instead of Node.js's built-in node:test runner:
- •Issue: nestjs/nest#14130 - NestJS
@nestjs/testingis incompatible withnode:test - •Impact:
node:testcannot properly injectQueryBus,CommandBus, or other NestJS providers - •Solution: Jest provides full compatibility with NestJS's testing utilities and DI system
When to Use
- •Testing business logic in isolation
- •Testing command/query handlers
- •Testing repository methods
- •Testing service methods
- •Unit testing utilities
What It Generates
Directory Structure
code
apps/api/src/modules/{feature}/__tests__/**/*.test.ts
Tests are co-located with source code but cleanly separated in __tests__/ subdirectory.
Patterns Enforced
AAA Pattern
All tests follow Arrange-Act-Assert:
- •Arrange: Set up test data and mocks
- •Act: Execute the function under test
- •Assert: Verify expected outcomes
Descriptive Test Names
Test names should describe:
- •What is being tested
- •The expected outcome
- •Edge cases covered
Example: should create user when valid data provided
Mocking
- •Mock external dependencies
- •Mock database calls
- •Mock event bus
- •Verify mock calls
Coverage Target
- •80%+ code coverage
- •Test success and failure paths
- •Test edge cases
Usage Example
bash
/skill test-unit --name=CreateUserHandler --type=command-handler --scenarios='success,duplicate-email,database-error'
Related Files
- •Test Integration - Integration tests
- •Test E2E - E2E tests
- •Feature CQRS - Handlers to test