# Tdd Workflow

> Enforces test-driven development with 80%+ coverage across unit, integration, and E2E tests. Use when the user says: 'write tests first', 'TDD', 'add test coverage', 'test this feature', 'write unit tests', 'create integration test', 'add E2E test', 'fix with TDD', 'red green refactor', 'increase coverage'. Guides the RED-GREEN-REFACTOR cycle for new features, bug fixes, and refactoring. Do NOT u…

- **Type:** Skill
- **Install:** `agentstack add skill-cor-incorporated-claude-code-skills-tdd-workflow`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [Cor-Incorporated](https://agentstack.voostack.com/s/cor-incorporated)
- **Installs:** 0
- **Category:** [Developer Tools](https://agentstack.voostack.com/c/developer-tools)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [Cor-Incorporated](https://github.com/Cor-Incorporated)
- **Source:** https://github.com/Cor-Incorporated/claude-code-skills/tree/develop/skills/tdd-workflow

## Install

```sh
agentstack add skill-cor-incorporated-claude-code-skills-tdd-workflow
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# TDD Workflow

Enforce test-driven development: write tests first, implement to pass, then refactor.

## The Cycle: RED -> GREEN -> REFACTOR

### RED: Write Failing Tests

Define expected behavior before writing any implementation:

```typescript
describe('searchMarkets', () => {
  it('returns relevant markets for query', async () => {
    const results = await searchMarkets('election')
    expect(results).toHaveLength(5)
    expect(results[0].relevanceScore).toBeGreaterThan(0.8)
  })

  it('returns empty array for no matches', async () => {
    const results = await searchMarkets('zzz_nonexistent')
    expect(results).toEqual([])
  })

  it('handles empty query gracefully', async () => {
    const results = await searchMarkets('')
    expect(results).toEqual([])
  })
})
```

Run tests -- they MUST fail:
```bash
npm test -- --run [test-file]
```

### GREEN: Write Minimal Implementation

Write just enough code to make all tests pass. No more.

```bash
npm test -- --run [test-file]
# All tests should now pass
```

### REFACTOR: Improve While Green

Improve code quality with tests as safety net:
- Remove duplication
- Improve naming
- Optimize performance
- Extract helpers

Run tests after each change to confirm nothing broke.

## Test Types and When to Use

| Type | What to Test | Tool | Speed Target |
|------|-------------|------|-------------|
| Unit | Single function/class, pure logic | Vitest/Jest | 

## Coverage Requirement

Target 80%+ across all metrics:

```bash
npm run test:coverage
```

Check branches, functions, lines, and statements individually.

## Testing Rules

1. **Test behavior, not implementation**
   - WRONG: `expect(component.state.count).toBe(5)`
   - RIGHT: `expect(screen.getByText('Count: 5')).toBeInTheDocument()`

2. **Each test is independent** -- set up own data, no shared mutable state

3. **Use semantic selectors**
   - WRONG: `page.click('.css-xyz')`
   - RIGHT: `page.click('[data-testid="submit"]')` or `page.click('button:has-text("Submit")')`

4. **Mock external dependencies** -- isolate the unit under test

5. **Test edge cases** -- null, undefined, empty, boundary values, error paths

6. **Arrange-Act-Assert** structure in every test

## Mocking Patterns

### Database/ORM
```typescript
vi.mock('@/lib/prisma', () => ({
  prisma: { user: { findUnique: vi.fn() } }
}))
```

### External APIs
```typescript
vi.mock('@/lib/external-api', () => ({
  fetchData: vi.fn(() => Promise.resolve({ data: 'mocked' }))
}))
```

## Error Handling

- If tests pass immediately (no RED phase): the test is not testing anything meaningful. Add assertions that verify specific behavior.
- If coverage is below 80%: run `npm run test:coverage` and check the uncovered lines report. Add tests for missed branches.
- If E2E tests are flaky: replace `waitForTimeout` with `waitForSelector` or `expect().toBeVisible()`. Never use fixed timeouts.
- If mocks leak between tests: add `vi.restoreAllMocks()` in `afterEach` or use `vi.mock` at module level.
- If tests are slow (unit > 50ms): check for unmocked network calls or missing test isolation.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [Cor-Incorporated](https://github.com/Cor-Incorporated)
- **Source:** [Cor-Incorporated/claude-code-skills](https://github.com/Cor-Incorporated/claude-code-skills)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** yes
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-cor-incorporated-claude-code-skills-tdd-workflow
- Seller: https://agentstack.voostack.com/s/cor-incorporated
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
