Skip to content
Testing & QualityChapter 49 / 57Intermediate6 min read

Testing Pyramid

The testing pyramid balances unit, integration and end to end tests. Learn what each layer proves, how to use a real user for auth, and how to fake vendors.

On this page

1. What is this practice?

LayerWhat it provesExample
UnitPure logic: parsers, validators, calculations, token helperscalculateTax(1000) returns the right number
Endpoint / integrationAPI contract, every validation branch, auth failures, limitsPOST /api/orders returns 422 on a wrong body
Browser (end to end)The whole user flow in a real browserFrom checkout to payment

The lower layers are cheap, fast and many. The upper ones are expensive, slow and few.

2. Why does it matter?

If you check everything with browser tests, the suite runs for hours and one small UI change breaks ten tests. If you have only unit tests, every function is fine, but an endpoint goes to production without an auth check.

Upside-down pyramid (ice cream cone)The right pyramid
Mostly browser testsMostly unit and endpoint tests
Suite takes 40 minutesSuite takes a few minutes
Flaky, passes on retryDeterministic
When it fails, you do not know whereWhen it fails, you see the exact function or endpoint
Shared databaseEvery run gets its own isolated database

3. When to follow it

LevelHow importantWhy
Learning projectUnit tests for pure functionsBuilds the testing habit and teaches writing testable code
Production codeUnit + tests for every endpoint, browser tests for critical flowsContract and security proven on every deploy
Senior levelThe balance of layers and the test infrastructureThe suite stays fast and trustworthy

4. Bad vs good code

Bad: mocking everything

ts
vi.mock("@/lib/db");
vi.mock("@/lib/auth");

test("creates order", async () => {
  vi.mocked(getUser).mockResolvedValue({ id: "u1", role: "admin" });
  vi.mocked(db.order.create).mockResolvedValue({ id: "o1" });

  const res = await POST(new Request("http://x", { method: "POST", body: "{}" }));
  expect(res.status).toBe(201);
});

Auth is mocked, the database is mocked, and the user is always admin. This test only proves that the mocks work. Real authorization was never tested.

Good: a unit test of pure logic

ts
import { describe, expect, test } from "vitest";
import { calculateTax } from "./tax";

describe("calculateTax", () => {
  test("applies standard rate", () => {
    expect(calculateTax(1000)).toBe(170);
  });

  test("returns zero below threshold", () => {
    expect(calculateTax(50)).toBe(0);
  });
});

Good: an endpoint test with a real user

ts
import { expect, test } from "vitest";
import { signInAsSeededUser } from "../helpers/auth";

test("rejects order with empty items", async () => {
  const token = await signInAsSeededUser();

  const res = await fetch(`${process.env.TEST_BASE_URL}/api/orders`, {
    method: "POST",
    headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
    body: JSON.stringify({ items: [] }),
  });

  expect(res.status).toBe(422);
});

A seed script creates a test user whose email is already confirmed. The test signs in as that user, so authorization and tenant scoping run exactly as in production.

What is the difference?

ThingBadGood
AuthMocked, always adminToken of a seeded real user
DatabaseMockedIsolated test database
What is provenThe wiring of mocksThe real contract and rules

5. How to apply it

Tests for a new feature
Pull the pure logic into its own function
Unit tests for it (every branch)
Endpoint tests for success, validation and auth
One browser test if it is a critical user flow
Run everything in CI on an isolated database

Rules that apply on every layer:

  • Tests never touch the production or shared staging database. CI uses a service container (like Postgres or MySQL) or a separate project.
  • External vendors (payment, SMS, email) are faked at the boundary. A test that needs a real vendor key is not a test, it is a manual script in scripts/.
  • Paid AI model calls do not run in the CI suite. Use a free contract test for the prompt, and run paid evaluation only manually, after a cost estimate.
  • Tests never wait for a mailbox or an SMS.

6. Exceptions and common mistakes

When the rule does not apply: a small marketing site with no logic gets little value from unit tests. A build and link check is enough there. Teams also differ on coverage percentage: some enforce a threshold (like 80%), others only report it so the number is visible but people do not write useless tests just to hit it. Both sides have a point.

Mistake: Checking everything with browser tests

Why? The suite becomes slow and flaky, and finding the problem on failure is hard.

Better: Logic in unit tests, contract in endpoint tests, browser only for critical flows.

Mistake: Mocking auth

Why? Then authorization and tenant scoping are never tested, and those are the most dangerous bugs.

Better: Sign in for real with a seeded test user and send the token.

Mistake: Tests that depend on order

Why? If one test needs another test's data, it fails when run alone or in parallel.

Better: Each test creates its own data and does not rely on any other test.

7. What to learn before and after

Final quick revision

CheckYes?
Unit tests for pure logic
Contract tests for every endpoint
Browser tests only for critical flows
Tests sign in as a seeded real user
Isolated database, never production
Vendors faked, paid AI calls kept out of CI