Skip to content

Test-Driven Development

The Red-Green-Refactor cycle: write a failing test, make it pass, then clean up.

What you'll learn

  • The three phases of the TDD cycle: Red, Green, Refactor
  • Why writing the test first shapes better designs
  • Keeping tests and code in tiny, reversible steps
  • Common obstacles and how to overcome them

Concept

Red, Green, Refactor

Test-Driven Development (TDD) is a discipline where you write the test before the production code. The cycle has three phases. Red: write a small test for the next bit of behavior and watch it fail (the code does not exist yet). Green: write the simplest code that makes the test pass — no more. Refactor: improve the code's structure without changing behavior, with the test as a safety net. Repeat.

Why Test First

Writing the test first forces you to think about the interface and the desired behavior before getting lost in implementation. It keeps every piece of code born with a test, so coverage is organic rather than retrofitted. It also gives instant feedback: you know the test can fail (you saw it red), so it is not a tautology that always passes. And it tends to produce code that is testable by construction, with dependencies injected and side effects isolated.

Tiny Steps

The power of TDD comes from tiny, reversible steps. A cycle might take two minutes: one test, one function, one refactor. Small steps mean you almost never debug, because the last change is small and obvious when something breaks. If you write a hundred lines before testing, a failure could be anywhere; with TDD, it is almost always the line you just wrote.

Obstacles

TDD is hard to apply to UI exploration, throwaway prototypes, or when you do not yet know the design. In those cases, write a spike (throwaway code to learn the problem), then delete it and TDD the real version. TDD also feels slow at first; with practice the cycle becomes second-nature and overall speed increases because you spend far less time debugging.

In Practice

Start TDD on a pure function with clear inputs and outputs — a calculator, a validator, a formatter. Write one test for the simplest case, watch it fail, write the minimum code to pass, and refactor. Then add the next case (an edge, an error) and repeat. The cycle should take one to three minutes; if it takes longer, your step is too big and you should split it. When a test fails unexpectedly, you almost never need to debug — the failure is in the last line you wrote. Commit after each green cycle so your history is a sequence of small, working steps. Over a few weeks the discipline becomes habit, and you will find yourself writing better-designed code because testability forces you to inject dependencies, isolate side effects, and keep functions small.

Example

              // RED — write the test first (it fails: fizzBuzz does not exist)
import { describe, it, expect } from "vitest";
import { fizzBuzz } from "./fizzBuzz";

describe("fizzBuzz", () => {
  it("returns Fizz for multiples of 3", () => {
    expect(fizzBuzz(3)).toBe("Fizz");
  });
});

// GREEN — write the minimum code to pass
export function fizzBuzz(n: number): string {
  if (n % 3 === 0) return "Fizz";
  return String(n);
}

// REFACTOR — now add the next test for 5, then 15, generalizing
// the code step by step. Each cycle stays green.
            

Start with one failing test for the simplest case. Make it pass with the least code possible. Only then add the next case and generalize. The cycle keeps design clean and bugs local.

Try it

  • Code Diff

    Review each Red-Green-Refactor diff before committing.

  • JSON Formatter

    Inspect the structured outputs your tests assert against.

Common mistakes

The mistake

Skipping the Red phase and writing test + code together

The fix

Always run the test and see it fail first. If it passes immediately, the test is wrong or testing the wrong thing.

The mistake

Writing more than the minimum code in Green

The fix

Green is for making the test pass, nothing more. Save generalization and cleanup for the Refactor phase so each step is verifiable.

Practice Test-Driven Development

4 exercises

Practice Test-Driven Development

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors