Two Testing Pitfalls in Layered Systems: Over-Mocking and Snapshot Sprawl

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.

Mocking inside a layer

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.

Snapshots in the most volatile layer

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:

  • the control exists and has an accessible role and name;
  • the message a person needs to read is visible;
  • the one value that matters on this screen is shown.

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.

One question that catches both

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.

The takeaway

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.

What the book covers

  • Brittle tests that assert on call counts and argument shapes, and how to repair them
  • Tests that mirror the implementation line by line, and why behavioural test names help
  • A rule of thumb for when mock setup signals the wrong level of abstraction
  • Exercises on designing a schema test suite and comparing mock strategies