Skip to main content
ClaudeWave
Skill171 estrellas del repoactualizado 1mo ago

TDD Workflow

Enforce strict Test-Driven Development with Red-Green-Refactor discipline. Apply when implementing new features, fixing bugs, or refactoring code. Ensures tests are written before implementation.

Instalar en Claude Code
Copiar
git clone --depth 1 https://github.com/ThamJiaHe/claude-code-handbook /tmp/tdd-workflow && cp -r /tmp/tdd-workflow/skills/examples/tdd-workflow- ~/.claude/skills/tdd-workflow
Después abre una sesión nueva de Claude Code; el skill carga automáticamente.

tdd-workflow-skill.md

# TDD Workflow

Strict Red-Green-Refactor discipline for every code change. Tests come first — no exceptions.

## Overview

This skill enforces TDD as a non-negotiable workflow:

1. **RED** — Write a failing test that describes the desired behavior
2. **GREEN** — Write the minimum code to make it pass
3. **REFACTOR** — Clean up with all tests green

Never write implementation before a test exists for the happy path.

## When to Use

- Implementing any new feature or function
- Fixing a bug (write the regression test first)
- Refactoring existing code (ensure tests exist first)
- Adding edge case handling

## RED Phase: Write the Failing Test

```typescript
// Step 1: Write the test FIRST
import { describe, test, expect } from 'vitest';
import { calculateDiscount } from './pricing';

describe('calculateDiscount', () => {
  test('applies 10% discount for orders over $100', () => {
    expect(calculateDiscount(150)).toBe(135);
  });

  test('no discount for orders under $100', () => {
    expect(calculateDiscount(50)).toBe(50);
  });

  test('applies 20% discount for orders over $500', () => {
    expect(calculateDiscount(600)).toBe(480);
  });
});
```

**Critical:** Run the test. Confirm it **fails**. If it passes before implementation, the test is wrong.

```bash
# Run and verify failure
pnpm test -- --run calculateDiscount
```

## GREEN Phase: Minimum Implementation

```typescript
// Step 2: Write the MINIMUM code to pass
export function calculateDiscount(amount: number): number {
  if (amount > 500) return amount * 0.8;
  if (amount > 100) return amount * 0.9;
  return amount;
}
```

**Critical:** Run the test again. Confirm it **passes**.

```bash
pnpm test -- --run calculateDiscount
```

## REFACTOR Phase: Clean Up

With all tests green, improve the code:

```typescript
// Step 3: Refactor while tests stay green
const DISCOUNT_TIERS = [
  { threshold: 500, rate: 0.2 },
  { threshold: 100, rate: 0.1 },
] as const;

export function calculateDiscount(amount: number): number {
  const tier = DISCOUNT_TIERS.find(t => amount > t.threshold);
  return tier ? amount * (1 - tier.rate) : amount;
}
```

Run tests after every refactoring step:

```bash
pnpm test -- --run calculateDiscount
```

## Coverage Requirements

| Code Type | Minimum Coverage |
|-----------|:---:|
| Business logic | 80% |
| State transitions | 80% |
| Error paths | 80% |
| Algorithms | 80% |
| Pure data mapping | Exempt |
| Generated code | Exempt |
| Single-line wrappers | Exempt |

## Test Type Selection

Use the lowest-cost sufficient test:

| Type | When |
|------|------|
| **Unit** | Pure functions, isolated logic, no I/O |
| **Integration** | DB interactions, API calls, file system |
| **E2E** | Full user-facing flows (use sparingly) |

## Anti-Patterns to Avoid

- Writing implementation before tests
- Writing tests that pass before implementation
- Testing implementation details instead of behavior
- Mocking internal collaborators (only mock external boundaries)
- Skipping the "confirm it fails" step
- Batch-writing all tests at once instead of one at a time

## Integration with Claude Code

```bash
# Run single test during development (fast feedback)
pnpm test -- --run testName

# Run full suite before committing
pnpm test

# Check coverage
pnpm test -- --coverage
```

## Sources

- [Everything Claude Code TDD Guide](https://github.com/anthropics/everything-claude-code)
- [Superpowers TDD Skill](https://github.com/obra/superpowers)
- [Kent Beck's TDD By Example](https://www.oreilly.com/library/view/test-driven-development/0321146530/)
API DevelopmentSkill

Build REST APIs with proper error handling, status codes, request validation, response formatting, and rate limiting. Apply when creating API routes, handling errors, validating input, or designing API responses.

API Security HardeningSkill

Harden REST and GraphQL APIs against common attack vectors. Apply when building API endpoints, implementing authentication, handling file uploads, or exposing APIs to external consumers.

AWS Cloud InfrastructureSkill

Deploy Node.js applications on AWS using EC2, RDS, and managed services with security best practices. Apply when setting up AWS infrastructure, configuring databases, managing security, or optimizing costs.

Build Error ResolverSkill

Rapidly fix build failures, type errors, and lint issues with minimal diffs. Apply when builds fail, TypeScript reports errors, or CI/CD pipelines break. Focuses on getting the build green fast.

Cybersecurity Threat ModelingSkill

STRIDE-based threat modeling for application architecture. Apply when designing new systems, reviewing architecture, or assessing security posture of existing applications.

Docker ContainerizationSkill

Production-ready Docker patterns for multi-stage builds, security hardening, and orchestration. Apply when creating Dockerfiles, docker-compose configs, or deploying containerized applications.

Git WorkflowSkill

Enforces Conventional Commits, PR standards, merge conflict resolution, and branch management. Apply when committing code, opening PRs, resolving conflicts, managing branches, or handling Git operations.

Google Cloud Platform & APIsSkill

Deploy Node.js applications on Google Cloud with Cloud Run, Cloud Firestore, and Google APIs. Implement OAuth2 authentication and manage service accounts. Apply when building serverless applications, integrating Google services, or deploying to GCP.