---
description: "Testing requirements and TDD practices"
globs: ["**/*.test.ts", "**/*.test.tsx", "**/*.test.js", "**/*.test.jsx"]
alwaysApply: false
---

# Testing Standards

## Test-Driven Development (TDD)

**TDD is MANDATORY for all implementation.**

### Required Process

1. **Write test first** — Define expected behavior
2. **Validate RED** — Confirm test fails (proves test works)
3. **Minimal code for GREEN** — Write simplest code to pass
4. **Refactor** — Improve code quality while keeping tests green

**Never skip tests because "this is trivial."**
```typescript
// Step 1: Write test first
describe('parseConfig', () => {
  it('should return null for invalid data', () => {
    expect(parseConfig(null)).toBeNull();
    expect(parseConfig(undefined)).toBeNull();
  });
});

// Step 2: Validate RED (test fails - no implementation yet)

// Step 3: Minimal code for GREEN
export function parseConfig(data: unknown): Config | null {
  if (!data) return null;
  return data as Config;
}

// Step 4: Refactor (improve while keeping green)
```

## Test Requirements

**All public functions must have tests:**
- Unit tests for logic
- Integration tests for cross-component behavior
- Coverage target: 80% minimum

**Test file structure:**
- Mirror source: `src/foo.ts` → `tests/foo.test.ts`
- Descriptive names: `should validate github url format`
- Arrange-Act-Assert pattern
```typescript
// ✅ GOOD - clear structure
describe('validateIssue', () => {
  it('should return true for valid issue', () => {
    // Arrange
    const issue = createValidIssue();
    const spec = createSpec();
    
    // Act
    const result = validateIssue(issue, spec);
    
    // Assert
    expect(result).toBe(true);
  });
});
```

## What to Test

**Always test:**
- Happy path (expected behavior)
- Error cases (invalid input, edge cases)
- Boundary conditions (empty, null, max values)

**Don't test:**
- Third-party library internals
- Simple getters/setters
- Framework behavior

## Related Rules

- **Code standards:** See `04-coding-standards.mdc`
- **Error handling:** Custom error classes must be testable

---

## Summary

- TDD is mandatory: Test → RED → GREEN → Refactor
- All public functions need tests
- 80% coverage minimum
- Test files mirror source structure