Test-Driven Development
Follow the TDD cycle: Red -> Green -> Refactor
Core Principles
- •Write the simplest failing test first
- •Implement minimum code to make tests pass
- •Refactor only after tests are passing
- •Separate structural changes from behavioral changes
TDD Workflow
- •Write a failing test (Red)
- •Write just enough code to pass (Green)
- •Refactor while keeping tests green
- •Repeat
typescript
// 1. Write failing test
test('shouldSumTwoPositiveNumbers', () => {
expect(sum(2, 3)).toBe(5);
});
// 2. Write minimum code to pass
function sum(a: number, b: number): number {
return a + b;
}
// 3. Refactor if needed (tests still pass)
Tidy First Approach
Separate commits into two types:
Structural Changes (First)
- •Renaming, extracting methods, moving code
- •No behavior change
- •Run tests before AND after
Behavioral Changes (Second)
- •Adding or modifying functionality
- •Commit separately from structural changes
Commit Discipline
Only commit when:
- •ALL tests pass
- •ALL linter warnings resolved
- •Single logical unit of work
- •Message states if structural or behavioral
Code Quality Standards
- •Eliminate duplication ruthlessly
- •Express intent through naming
- •Make dependencies explicit
- •Keep methods small (single responsibility)
- •Minimize state and side effects
- •Use simplest solution that works
Refactoring Guidelines
- •Only refactor in Green phase
- •Use established refactoring patterns
- •One refactoring at a time
- •Run tests after each step