Layer-Specific Testing, Part 1: Schema, Utils, Resources and Tools

A library system accepts a loan period of minus three days. Nobody meant it to; the schema simply never said no. The bug surfaces weeks later as a negative due date in a report three layers away, and it takes an afternoon to trace. The cheapest tests in the system are the ones that stop this, which is why LVD tests the bottom layers hardest.

Schema: say no to everything

The book sets a 100 per cent target for Schema tests, because every other layer trusts these definitions. The method is to enumerate. For each schema, write one test per missing required field, boundary tests for every minimum, maximum and pattern, a test for each member of an enumeration, a check that defaults get filled in, and a check on any coercion, such as turning date strings into dates.

Property-based testing with a library such as fast-check then covers the cases you did not think of. Instead of choosing three bad inputs, you describe a whole family of them:

import fc from "fast-check";
import { LoanSchema } from "@schema/loan";

it("rejects any loan period of zero days or fewer", () => {
  fc.assert(
    fc.property(fc.integer({ min: -500, max: 0 }), (days) =>
      !LoanSchema.safeParse({ memberId: "m-1", bookId: "b-1", days }).success
    )
  );
});

Utils: pure functions, 95 per cent or more

Utils have no side effects, so there is no excuse for gaps. Ordinary example tests handle the obvious cases: a zero amount, a zero rate, an awkward rounding. Property tests then state laws that must always hold. A late-fee function, for instance, should never return a negative amount, should never charge less for more days overdue, and should scale predictably with the daily rate.

The book adds one more tool for latency-sensitive functions: micro-benchmarks that live next to the tests, so a slowdown in a hot path shows up as a number rather than a complaint.

Resources: pay for realism where it counts

With Resources the cost of testing climbs, because these modules talk to databases, HTTP services, queues and file storage. The target drops to 80 per cent or more, and the focus narrows to the behaviours that fail in real life:

  • Retries: transient failures should trigger retries and permanent ones should surface a clear error.
  • Timeouts: a slow dependency must not hang the application.
  • Mapping: external data must be translated into the domain types defined in Schema.
  • Connection pooling: connections must be released under concurrent load.

Two techniques carry most of the weight. A containerised real database (the book uses Testcontainers) gives you an honest repository test with no mocking. A mock HTTP server, in the book's case MSW, lets you script an external API that fails twice and then succeeds, so you can assert that exactly three attempts were made, and that the code gives up with a sensible error once its retries run out.

Tools: mock the boundary, test the rules

Tools hold the business logic, so the target rises to 90 per cent or more. A tool depends on Resources through interfaces, so the test injects hand-made fakes for those interfaces and exercises the rules in full. Dependency injection is what keeps this painless.

The most valuable cases involve tools that perform several steps in sequence, such as reserve stock, charge payment, then create the order. The book's advice is to test the happy path and also every compensating action on failure. Make the fake payment service fail and then check that the stock reservation was undone. Because each fake is under your control, you can choose precisely which step breaks.

it("releases the seat when the card is declined", async () => {
  payments.charge.mockRejectedValue(new Error("declined"));
  await expect(bookSeat({ eventId: "e-4", seat: "C12" })).rejects.toThrow("declined");
  expect(seats.release).toHaveBeenCalledWith("e-4", "C12");
});

The takeaway

Each of the four layers gets the cheapest test that is honest about its nature: exhaustive for schemas, property-driven for pure code, realistic for adapters, mocked at the boundary for business rules. For more depth on the adapter layer, see testing the Resources layer and testing tools. The upper layers are in part two, and the overall targets in the testing pyramid.

What the book covers

  • A full schema test suite and how to enumerate cases per field
  • Property-based tests and micro-benchmarks for utility functions
  • Testcontainers and mock HTTP servers for the Resources layer
  • Testing multi-step tools, including every compensating action on failure