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 files should be colocated with the code they test:
Test Structure
Tests follow the Given-When-Then pattern:
Mocking
Module Mocking
Dependency Mocking
Method Spying
Note: Mocks must be “hoisted” (placed at the top level before relevant imports).
Coverage
Generate coverage reports:
Coverage results are output to project-specific directories.
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/afterEach hooks
- Avoid testing private methods - Test through public interfaces
Last modified on March 4, 2026