Tdd workflow

From travisjneuman/.claude · View interactive version

Tdd workflow is an AI agent skill from the GitHub repository travisjneuman/.claude. Test-Driven Development workflow enforcement with RED-GREEN-REFACTOR cycle. Use only when the user explicitly asks for test-first work or improved test coverage.

How to use this skill

Skill file

Test-Driven Development (TDD) Workflow

Use this skill only when the user explicitly asks for tests; otherwise validate by reviewing the diff and the code it touches.

A disciplined approach to development where tests drive design and implementation.

The TDD Mantra

"Never write a line of code without a failing test."

The RED-GREEN-REFACTOR Cycle

RED Phase: Write a Failing Test

Goal: Define the expected behavior BEFORE implementation.

Rules:

  1. Write the smallest test that fails
  2. Test must fail for the RIGHT reason
  3. Test should clearly express intent
  4. Don't write implementation yet

Example:

// RED: Test the behavior we want
describe("Calculator", () => {
  it("should add two numbers", () => {
    const calc = new Calculator();
    expect(calc.add(2, 3)).toBe(5);
  });
});

// Run: npm test
// Result: FAIL - Calculator is not defined
// This is RED ✓

Checklist:


GREEN Phase: Make the Test Pass

Goal: Write the MINIMUM code to pass the test.

Rules:

  1. Do the simplest thing that works
  2. Don't add extra features
  3. Don't optimize
  4. Just make it green

Example:

// GREEN: Minimum implementation to pass
class Calculator {
  add(a: number, b: number): number {
    return a + b; // Simplest thing that works
  }
}

// Run: npm test
// Result: PASS
// This is GREEN ✓

Checklist:


REFACTOR Phase: Improve the Code

Goal: Clean up while keeping tests green.

Rules:

  1. Only refactor with passing tests
  2. Run tests after each change
  3. Improve design, not behavior
  4. Small, incremental changes

Examples of refactoring:

Checklist:


TDD in Practice

Starting a New Feature

1. Write high-level acceptance test (may not run yet)
2. Write first unit test (RED)
3. Implement minimum code (GREEN)
4. Refactor if needed (REFACTOR)
5. Repeat 2-4 until feature complete
6. Verify acceptance test passes

Test Structure (AAA Pattern)

it("should [behavior] when [condition]", () => {
  // Arrange - Set up test data and dependencies
  const user = createTestUser({ role: "admin" });
  const service = new UserService();

  // Act - Execute the code under test
  const result = service.getPermissions(user);

  // Assert - Verify expected outcomes
  expect(result).toContain("delete");
  expect(result).toContain("edit");
});

Test Naming Convention

[Unit]_[Scenario]_[ExpectedResult]

Examples:


Test Categories

Unit Tests

Integration Tests

End-to-End Tests


TDD Best Practices

DO:

DON'T:


Edge Cases to Test

Always test:


Test Coverage Guidelines

| Metric | Minimum | Target |

| ---------- | ------- | ------ |

| Statements | 70% | 85% |

| Branches | 70% | 80% |

| Functions | 80% | 90% |

| Lines | 70% | 85% |

Coverage is a metric, not a goal. 100% coverage doesn't mean bug-free.


Quick Reference

RED    → Write failing test (define behavior)
GREEN  → Minimum code to pass (make it work)
REFACTOR → Clean up (make it right)
COMMIT → Save progress (make it permanent)

Common TDD Mistakes

| Mistake | Problem | Solution |

| ---------------------- | ------------- | ---------------------- |

| Testing implementation | Brittle tests | Test behavior/outcomes |

| Tests too large | Hard to debug | Smaller, focused tests |

| Shared state | Flaky tests | Isolate each test |

| Slow tests | Skipped tests | Mock external deps |

| Testing obvious code | Wasted time | Focus on logic |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.