Testing the Upper Layers: Contracts, the UI and a Few End-to-End Checks
API and UI code translate between worlds, so test the translation, not the business rules: contract tests, UI outcome tests and where end-to-end fits.
Somebody renames a private helper, and fifteen tests across three folders turn red. The behaviour has not moved an inch. By the next sprint the team has quietly stopped tidying anything, because each cleanup costs an afternoon of repairing tests. Tests that punish improvement do more harm than having none, and layered code makes two kinds of them especially easy to write.
Picture a Tool that quotes a ride price. To test it, you stub the database driver two layers down, script the exact query it will receive and assert that the query ran once. The test passes. It also now knows how persistence works, down to the SQL. Swap the storage library and every Tool test fails, although nothing about pricing changed.
The book's name for this is over-mocking, and its remedy suits an LVD codebase well: mock at the boundary between layers, never within one. Each layer already exposes a defined interface to the layer beneath it, so that interface is the natural seam. A Tool test should meet the Resource it depends on at its interface and stay out of the machinery behind it.
I would add one refinement from general practice: where you can, use a small in-memory fake instead of a scripted mock. A fake behaves like a real implementation, so the test asserts on outcomes instead of on the conversation.
const drivers = (...pool: Driver[]): DriverRepo => ({
nearest: async (zone) => pool.find((d) => d.zone === zone) ?? null,
});
it("applies surge pricing when no driver is nearby", async () => {
const quote = await quoteRide({ drivers: drivers(), zone: "north" });
expect(quote.surge).toBe(true); // what the caller gets, not which calls were made
});
The fake also survives refactoring. Change how the real repository stores drivers and nothing in this test needs to move.
The UI layer has the highest volatility in the model (ν = 1.0), which means its output changes more often than anything else you own. That is the worst place for a test whose only rule is "the rendered output must not differ from last time".
The decay is predictable. A pull request arrives with four hundred changed snapshot lines. Reviewers cannot see the one meaningful change among the noise, so they approve. Developers learn to regenerate the files and commit them alongside whatever broke them, without reading the diff. At that point the test cannot fail for a good reason, and it protects nothing.
Replace the blanket snapshot with a few assertions about what a user depends on:
Snapshots still have a place on small outputs whose changes mean something: a serialised error payload, a formatted receipt, a short configuration object. A diff you can read in one glance is a useful test. A diff nobody scrolls through is noise. Visual-regression tooling is likewise better spent on stable components, where a pixel change is news.
Both traps tie a test to what the code looks like instead of what the layer promises. Before you commit a test, ask: if I rewrote the inside of this layer and kept its behaviour, would this test still pass? If the answer is no, the test is measuring the implementation.
Layers make that question easy to answer, because every layer has a stated contract to hold to. For where each kind of test sits, see the testing pyramid and the guide to testing the lower layers. The book's chapter lists four pitfalls, and this article covers two.
Mock at the seam between layers, prefer fakes that behave over mocks that narrate, and keep snapshots for output small enough to read. A test should break when behaviour changes and at no other time.