Skip to content

Testing Best Practices

Principles for trustworthy tests: FAST, single-assertion, behavior-focused, and DRY.

What you'll learn

  • The FIRST principles: Fast, Isolated, Repeatable, Self-verifying, Timely
  • Testing behavior over implementation to keep tests resilient
  • Balancing DRY with DAMP to avoid brittle test setup
  • Keeping the suite trustworthy, fast, and maintainable

Concept

FIRST Principles

A trustworthy test suite follows FIRST. Fast: thousands of unit tests run in seconds so developers run them constantly. Isolated: no test depends on another or on order; each can run alone. Repeatable: the same result every time, no random or network. Self-verifying: pass or fail automatically with a clear signal, no manual inspection. Timely: written close to the code, ideally before it (TDD). Violating any of these erodes trust.

Behavior over Implementation

Tests should assert what the code does, not how it does it. A test that checks result.internalCache.size breaks when you rename the cache; a test that checks result.total() survives refactors. Query by visible output, assert on public return values, and avoid asserting on private state, call counts to your own methods, or CSS classes. Behavior-focused tests let you refactor freely; implementation-coupled tests fight every change.

DRY vs DAMP

In production code, DRY (Don't Repeat Yourself) is king. In tests, a little repetition is healthy: each test should read top-to-bottom without jumping to shared helpers. Use DAMP (Descriptive And Meaningful Phrases) — extract setup into helpers when it is truly identical, but inline the parts that vary so each test tells its own story. Over-abstracted test setup is the leading cause of tests no one understands.

Trustworthy Suites

A suite is only valuable if the team trusts it. Keep it green: fix or remove failing tests immediately. Keep it fast: a suite that takes ten minutes is one nobody runs. Keep it meaningful: prune tests that assert nothing or duplicate others. Review tests in code review with the same rigor as production code. A trustworthy suite is a safety net; an untrustworthy one is dead weight that everyone ignores.

In Practice

Adopt these habits incrementally so they stick. Start by making the suite green and keeping it green — a red suite trains the team to ignore failures. Add a CI gate that blocks merges on red, and a coverage threshold that prevents silent drops. In code review, read the test changes as carefully as the production changes: ask whether the test would fail if the code broke, whether the name describes the behavior, and whether the setup is readable. Periodically run a "test review" session: delete tests that duplicate or assert nothing, consolidate overlapping setup, and fix or quarantine flaky tests. Treat the test suite as a product with users (your teammates) — its speed, clarity, and trustworthiness determine whether it protects you or slows you down.

Example

              import { describe, it, expect } from "vitest";
import { formatPrice } from "./format";

describe("formatPrice", () => {
  // Behavior-focused, isolated, self-verifying, repeatable
  it("formats a whole dollar amount with two decimals", () => {
    expect(formatPrice(10)).toBe("$10.00");
  });
  it("rounds to two decimal places", () => {
    expect(formatPrice(10.005)).toBe("$10.01");
  });
  it("handles zero", () => {
    expect(formatPrice(0)).toBe("$0.00");
  });
  it("formats negative amounts with a minus sign", () => {
    expect(formatPrice(-5)).toBe("-$5.00");
  });
});
            

Each test is independent, asserts one behavior, and would fail if formatPrice broke. No implementation details leak in, so refactors are safe.

Try it

  • Code Diff

    Review test changes in pull requests with the rigor of prod code.

  • JSON Formatter

    Inspect the structured outputs your behavior assertions check.

Common mistakes

The mistake

Asserting on implementation details like private fields

The fix

Assert on public behavior and visible output. Implementation-coupled tests break on every refactor and fight change instead of enabling it.

The mistake

Letting the suite stay red 'just for now'

The fix

A red suite is ignored. Fix failures immediately, or quarantine with a ticket. Trust is hard to rebuild once the team learns to ignore the tests.

Practice Testing Best Practices

10 exercises

Practice Testing Best Practices

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