Two Testing Pitfalls in Layered Systems: Over-Mocking and Snapshot Sprawl
Tests that punish refactoring teach teams to stop refactoring. Two traps layered code invites: mocking inside a layer and snapshotting a volatile one.
Teams rarely abandon testing because writing assertions is hard. They abandon it because the surrounding machinery is painful: data that has to be conjured by hand, doubles that behave nothing like the real thing, suites that only pass when run in the right order. LVD's chapter on test infrastructure shows how the layer rules take most of the pain away.
The book separates three complementary tools, each for a different job.
The factory is the workhorse. A few lines of your own are enough to start with, here for a ticketing service:
const buildTicket = (over: Partial<Ticket> = {}): Ticket => ({
ticketId: `t-${crypto.randomUUID()}`,
eventId: "evt-1",
seat: "A1",
status: "held",
...over,
});
const sold = buildTicket({ status: "sold" });
A test that cares about sold tickets says so and nothing else. When the schema gains a field, you change the factory once rather than forty tests.
The layer structure simplifies mocking. A test for a given layer only needs fakes for the layers below it, never above, so the list of things to fake is short and known. The book then offers four options and says the choice follows the purpose of the test:
The rule of thumb is that unit tests of Tools favour manual mocks, for speed, while integration tests of Resources favour containers, for fidelity. The same dependency can be faked differently at different levels of the pyramid, and that is a feature, not an inconsistency.
Every test must stand alone: no shared mutable state, no ordering assumptions, no leaked side effects. Where a database is involved, the book lists three strategies. You can wrap each test in a transaction and roll it back, give each test file its own database schema so files run side by side, or truncate every table and reload fixtures between tests. They trade speed against simplicity, and the right one depends on how your suite is organised.
The payoff of isolation comes from the dependency graph. Since LVD layers contain no cycles, the Schema, Utils, Resources and Tools suites have nothing to wait for from each other and can run at the same time. The API suite follows once the Tools tests pass, and the end-to-end suite follows once the API tests pass. In other words, the pipeline stages are not an arbitrary choice; they are the layer order. A failing Utils test stops the build early and cheaply, long before anything expensive starts.
Good test infrastructure is mostly a matter of knowing what to fake and where to stop. LVD helps because the layer a test belongs to tells you what it may depend on, what it should replace, and when it can run. See the testing pyramid for the targets that drive these choices, testing the Resources layer for containers and mock servers in depth, and common testing pitfalls for what goes wrong when the mocking goes too far.