When to Use Approval Testing vs Unit Tests

When to Use Approval Testing vs Unit Tests

Both unit tests and approval tests verify code behavior. Both catch regressions. Both live in your test suite. But they make different trade-offs, and choosing the wrong one leads to tests that are either too fragile or too vague.

This guide walks through when to reach for approval testing, when unit tests are the better fit, and how to combine both in a coherent strategy.


The Core Trade-off

Unit tests make explicit assertions:

expect(calculateDiscount(100, "PREMIUM")).toBe(25);
expect(calculateDiscount(0, "PREMIUM")).toBe(0);
expect(calculateDiscount(100, "BASIC")).toBe(5);

You write out exactly what you expect. The test is precise, readable, and directly tied to specification.

Approval tests compare to a stored baseline:

verify(calculateDiscount(100, "PREMIUM")); // compares to approved file

You capture what the function actually returns and approve it as correct. The test is less prescriptive but covers complex or voluminous output efficiently.


Use Unit Tests When

You know the expected value

If you can write expect(result).toBe(expectedValue) clearly and readably, a unit test is simpler and more direct.

// Clear expected value → unit test
it("calculates 20% discount for premium users", () => {
  expect(discountService.calculate("premium", 100)).toBe(20);
});

The output is a single, simple value

A boolean, a number, a short string—these don't benefit from approval files.

// Simple output → unit test
expect(isValidEmail("test@example.com")).toBe(true);
expect(isValidEmail("notanemail")).toBe(false);

You're specifying behavior before implementation

TDD (test-driven development) requires writing tests first. You can't approve output that doesn't exist yet.

// TDD: write this before writing the function
it("returns zero for empty cart", () => {
  expect(calculateTotal([])).toBe(0);
});

Approval testing is inherently retrospective—you run code first, then approve. Unit testing is prescriptive—you specify first, then implement.

You're testing edge cases explicitly

Boundary values and edge cases are clearest when the expected result is explicit.

// Edge cases: explicit expected values are clearer
it("handles negative quantities", () => {
  expect(calculateTotal([{ price: 10, qty: -1 }])).toBe(0);
});

it("handles fractional prices with rounding", () => {
  expect(calculateTotal([{ price: 0.1, qty: 3 }])).toBe(0.30); // not 0.30000000000000004
});

Use Approval Testing When

The output is too complex to write manually

A formatted report, a rendered template, a deep object graph—manually writing the expected value is impractical.

// 50-field invoice object → approval test
verify(JSON.stringify(generateInvoice(order), null, 2));

You need to cover many input combinations

Approval testing's combinatorial support captures all combinations in a single approved file.

Approvals.verifyAllCombinations(
  (discount, currency, qty) => formatPrice(discount, currency, qty),
  ["basic", "pro", "enterprise"],
  ["USD", "EUR", "GBP"],
  [1, 10, 100]
);

27 combinations, one approved file. A unit test for each would require 27 separate assertions—or a complex data-driven test.

You're working with legacy code you don't fully understand

You can't write unit tests for behavior you don't know. But you can capture existing behavior:

// Characterize legacy function
verify(legacyPricingFunction(sampleInput));
// Now you have a baseline—change it only when you understand what you're changing

The format of the output matters, not just the value

For email templates, generated HTML, or formatted text, the shape of the output matters alongside the values.

Approval test catches:
  - "Dear Alice" changed to "Dear alice" (formatting regression)
  - Line breaks added/removed (layout regression)
  - Column widths in tables shifted (visual regression)

A unit test checking individual values wouldn't catch these.

Refactoring complex code

When you have a complex function and want to ensure refactoring preserves behavior, approval testing captures the current output as a safety net before you start.


Side-by-Side Decision Guide

Situation Unit Test Approval Test
Simple return value (bool, number, short string) ✓
Complex multi-field object ✓
TDD / spec-first ✓
Legacy code characterization ✓
Many input combinations ✓
Output format matters (layout, whitespace) ✓
Clear, writable expected value ✓
Refactoring safety net ✓
Edge cases with specific expected values ✓
Generated text (reports, emails) ✓

The Hybrid Pattern

Many functions benefit from both:

// UserSummary service: generates a complex formatted summary

// Unit tests: verify specific business logic
it("includes user tier in summary", () => {
  const summary = userSummaryService.generate(premiumUser);
  expect(summary.tier).toBe("Premium");
});

it("calculates loyalty years correctly", () => {
  const user = { createdAt: "2020-01-01" };
  const summary = userSummaryService.generate(user, { now: new Date("2025-01-01") });
  expect(summary.loyaltyYears).toBe(5);
});

// Approval test: verifies the formatted output as a whole
it("formats summary for display", () => {
  verify(userSummaryService.generateFormatted(testUser));
});

Unit tests pin the critical business rules. The approval test pins the output shape. If someone adds a new field or changes the layout, the approval test catches it.


Warning: When Approval Tests Go Wrong

Approving without reviewing

The biggest failure mode: a developer runs the test, it fails, they look at it briefly, hit "accept all", and move on. The approved file now contains a regression.

Approval testing only works when someone actually reviews the diff.

Mitigation: require approval file changes to be reviewed in pull requests, just like code changes.

Volatile data in approved files

Timestamps and random IDs that change on every run make tests permanently red.

// Problem: createdAt changes on every run
const user = { id: 1, createdAt: new Date().toISOString() };
verify(JSON.stringify(user));

// Fix: normalize before approving
const normalized = { ...user, createdAt: "[timestamp]" };
verify(JSON.stringify(normalized, null, 2));

Approved files getting stale

An approved file that no longer has a corresponding test is silently useless. Configure errorOnStaleApprovedFiles: true to catch this.

Using approval tests for simple values

// Overkill: approval test for a single number
verify(String(calculateTax(100, "CA")));

// Better: plain unit test
expect(calculateTax(100, "CA")).toBeCloseTo(9.25);

In Your Test Strategy

A balanced test suite uses both:

Unit Tests
  - Business rules and calculations
  - Edge cases with known expected values  
  - TDD / spec-first tests
  - Behavior that changes on purpose (tracked through test changes)

Approval Tests
  - Complex output structures
  - Generated text/documents
  - Legacy characterization before refactoring
  - Combined coverage of many input variations

Neither replaces the other. They answer different questions.


What Neither Does

Unit tests and approval tests both verify code in isolation. They don't test:

  • Real browser rendering
  • HTTP request/response flows
  • Database queries in production environments
  • Multi-service interactions

HelpMeTest handles that layer with plain-English end-to-end tests. Your unit and approval tests verify the logic; HelpMeTest verifies the complete user experience against your real application.


Summary

  • Use unit tests when you know the expected value and can write it clearly
  • Use approval tests when output is complex, voluminous, or impractical to specify manually
  • Combine both: unit tests for business rules, approval tests for output shape
  • Approval testing requires real review—approving without looking defeats the purpose
  • Neither replaces integration or end-to-end testing

Read more

Start now free