Skip to main content
This guide ensures all code development follows TDD principles with comprehensive test coverage.

Core Principles

Tests BEFORE Code

ALWAYS write tests first, then implement code to make tests pass.

80%+ Coverage

Minimum 80% coverage (unit + integration + E2E). All edge cases covered.

Three Test Types

Unit, integration, and E2E tests. All required.

When to Use TDD

  • Writing new features or functionality
  • Fixing bugs or issues
  • Refactoring existing code
  • Adding API endpoints
  • Creating new components

Test Types

Unit Tests

Unit Tests

Test individual functions and components in isolation.Coverage:
  • Individual functions and utilities
  • Component logic
  • Pure functions
  • Helpers and utilities

Integration Tests

Integration Tests

Test how different parts of the system work together.Coverage:
  • API endpoints
  • Database operations
  • Service interactions
  • External API calls

E2E Tests (Playwright)

E2E Tests

Test complete user workflows in a real browser.Coverage:
  • Critical user flows
  • Complete workflows
  • Browser automation
  • UI interactions

The TDD Workflow

1

Write User Journeys

2

Generate Test Cases

3

Run Tests (They Should Fail)

RED phase: Tests must fail first to verify they’re testing correctly.
4

Implement Code

Write minimal code to make tests pass:
5

Run Tests Again

GREEN phase: Tests pass with minimal implementation.
6

Refactor

Improve code quality while keeping tests green:
  • Remove duplication
  • Improve naming
  • Optimize performance
  • Enhance readability
IMPROVE phase: Refactor with confidence. Tests protect against regressions.
7

Verify Coverage

Testing Patterns

Unit Test Pattern (Jest/Vitest)

API Integration Test Pattern

E2E Test Pattern (Playwright)

Test File Organization

Mocking External Services

Supabase Mock

Redis Mock

OpenAI Mock

Test Coverage Verification

Run Coverage Report

Coverage Thresholds

All thresholds set to 80%. Tests fail if coverage drops below this.

Common Testing Mistakes

❌ Testing Implementation Details

❌ Brittle Selectors

❌ No Test Isolation

Continuous Testing

Watch Mode During Development

Pre-Commit Hook

CI/CD Integration

Best Practices

Write Tests First

Always TDD. No exceptions.

One Assert Per Test

Focus on single behavior.

Descriptive Test Names

Explain what’s tested.

Arrange-Act-Assert

Clear test structure.

Mock External Dependencies

Isolate unit tests.

Test Edge Cases

Null, undefined, empty, large.

Test Error Paths

Not just happy paths.

Keep Tests Fast

Unit tests < 50ms each.

Clean Up After Tests

No side effects.

Review Coverage Reports

Identify gaps.

Success Metrics

  • 80%+ code coverage achieved
  • All tests passing (green)
  • No skipped or disabled tests
  • Fast test execution (< 30s for unit tests)
  • E2E tests cover critical user flows
  • Tests catch bugs before production

ECC TDD Tools

tdd-guide Agent

Specialized agent that enforces write-tests-first workflow.

/tdd Command

Invoke TDD workflow immediately.

tdd-workflow Skill

Comprehensive TDD methodology and patterns.

/test-coverage Command

Analyze test coverage and identify gaps.

Example TDD Session

1

Start TDD Workflow

Activates tdd-guide agent.
2

Write Failing Test

Run: npm test — Should fail (RED)
3

Implement Function

Run: npm test — Should pass (GREEN)
4

Refactor

Improve code quality, keep tests green.
5

Verify Coverage

Ensure 80%+ coverage.
6

Code Review

Final quality check.

Tests are not optional. They are the safety net that enables confident refactoring, rapid development, and production reliability.