Testing Guide
The CoW Protocol BFF uses Jest v29 as its testing framework, integrated with NX for efficient test execution. The setup includes TypeScript support via ts-jest and runs in a Node.js environment.Running Tests
Basic Commands
Targeting Specific Files
Test Organization
File Naming Conventions
- Unit tests:
*.spec.ts - E2E tests:
*.e2e-spec.ts
Test Structure
Tests follow the Given-When-Then pattern:Mocking
Module Mocking
Dependency Mocking
Method Spying
Coverage
Generate coverage reports:NX Caching
NX provides intelligent caching to avoid re-running unchanged tests:Integration Testing
Testing with Database (TypeORM)
Testing API Endpoints (Fastify)
Testing Notifications
Send test notifications to verify the full pipeline:CI Integration
GitHub Actions automatically runs tests on commits. The CI configuration uses NX’s affected command to only test changed projects:Best Practices
- Test behavior, not implementation - Focus on inputs and outputs
- Use descriptive test names - Describe the scenario being tested
- One assertion concept per test - Keep tests focused
- Follow Given-When-Then - Structure tests clearly
- Colocate tests with code - Keep test files next to implementation
- Use factory functions for test data creation
- Clean up resources in
afterAll/afterEachhooks - Avoid testing private methods - Test through public interfaces