Skip to main content

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

  1. Test behavior, not implementation - Focus on inputs and outputs
  2. Use descriptive test names - Describe the scenario being tested
  3. One assertion concept per test - Keep tests focused
  4. Follow Given-When-Then - Structure tests clearly
  5. Colocate tests with code - Keep test files next to implementation
  6. Use factory functions for test data creation
  7. Clean up resources in afterAll/afterEach hooks
  8. Avoid testing private methods - Test through public interfaces
Last modified on March 4, 2026