Test Infrastructure for a Layered System

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.

Three ways to make test data

The book separates three complementary tools, each for a different job.

  • Fixtures are static files, JSON or SQL seed scripts, loaded before a suite. They give integration tests a known database state.
  • Factories produce valid domain objects with sensible defaults, so a test overrides only the field that matters to its scenario. The book builds its factory around a Schema definition, which keeps generated objects honest about the real shape of the data.
  • Builders use a fluent interface to assemble a connected graph of objects, such as a customer, a product and an order that must all agree with one another.

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.

A mock for every purpose

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:

  1. Manual mocks, plain objects with stubbed methods, for simple interfaces.
  2. Mock Service Worker for HTTP-based resources, intercepting requests at the network level.
  3. Testcontainers for databases, which start a real instance in Docker.
  4. In-memory implementations for stateful resources, offering the same interface but keeping data in memory.

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.

Isolation, then parallelism

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.

The takeaway

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.

What the book covers

  • Fixtures, factories and fluent builders for test data
  • Manual mocks, MSW, Testcontainers and in-memory implementations compared
  • Three database isolation strategies for parallel runs
  • The execution topology that falls out of the dependency graph