TDD (Test-Driven Development)

Methodology

// Definition

Write a failing test first, write the minimum code to make it pass, refactor. Drives design through tests rather than testing afterward — and produces a thorough unit test suite as a byproduct.

// Code Example

VitestRed → Green → Refactor
TypeScript
// 1. RED — write a failing test first
test('add returns the sum of two numbers', () => {
  expect(add(2, 3)).toBe(5);
});

// 2. GREEN — minimum code to pass
function add(a: number, b: number) {
  return a + b;
}

// 3. REFACTOR — improve while keeping tests green
const add = (a: number, b: number): number => a + b;

// Related terms