Testing the Upper Layers: Contracts, the UI and a Few End-to-End Checks

The API tests are green. The UI tests are green. A rider still opens the app and sees "Fare: NaN", because the server renamed a field and the screen kept the old name. Bugs in the upper layers rarely hide in logic. They hide in the seams between layers, which is exactly where these layers do their work.

Upper layers translate, so test the translation

The API layer turns an outside request into a Tool call and the result back into a response. The UI turns a response into something a person can read. Neither is meant to hold business rules in LVD, and for the UI the book is explicit: correctness is verified lower down, in Utils and Tools.

That shapes what a good test here looks like. It asserts the form of the exchange: which fields are present, which status comes back, what a failure looks like, what a user sees when the data is missing. If you catch yourself computing a fare inside a route test, one of two things is true. Either the rule is living in the wrong layer, or the test is in the wrong place.

Contract tests compare against the document, not your memory

A hand-written expectation drifts with the code it was copied from. If the response shape changes and the test is updated to match, the test has recorded the change instead of questioning it. A stronger design checks the live response against the published contract, so that a mismatch between implementation and document fails the build whichever of the two is wrong.

it("matches the published contract", async () => {
  const res = await api.post("/rides").send({ pickup: "stn-12", dropoff: "air-03" });
  expect(res.status).toBe(201);
  expectConformsToSpec(res, { path: "/rides", method: "post" }); // shape and types only
});

Notice what is missing: any assertion about the fare. The helper checks that the fields exist and have the right types, nothing more. Write the same kind of test for the error paths, since client code branches on error shapes just as heavily as on success. The book's checklist for API contract tests goes well beyond status codes.

UI tests should visit every outcome

A screen that fetches data has several faces: the moment before anything arrives, a full result, an empty one, and a failure. Developers tend to build and test only the full one, and that is the face that users see least often in production. Mocking at the HTTP boundary lets the real hook, cache and component run together while you choose what the "server" says:

it.each([
  ["a list of rides", ok([ride("Airport run")]), /airport run/i],
  ["an empty state", ok([]), /no rides yet/i],
  ["an error message", fail(500), /could not load/i],
])("shows %s", async (_name, reply, expected) => {
  server.use(rides(reply));
  renderWithProviders(<RideHistory />);
  expect(await screen.findByText(expected)).toBeVisible();
});

Two habits keep these tests honest. Query by role and accessible name where you can, so that a control that loses its label fails a test rather than an audit. And assert that a total is displayed, never that it is correct. The book draws that line the same way: correctness is checked in Utils and Tools, and the UI test only proves the number reaches the screen.

The few end-to-end checks

At the top of the pyramid sit tests that drive a whole workflow through every layer. They are slow to write, slow to run and sensitive to the environment they run in, so the book reserves them for the most critical paths. A rule of thumb (mine, not the book's) is one journey per promise the business makes to its users: book a ride, pay for it, see the receipt. Everything else should be caught lower, where failures are cheaper to diagnose.

The wider picture is in the testing pyramid article, and the foundations below these layers are in Part 1 on Schema, Utils, Resources and Tools. The protocol layers get a closer look in testing the protocol layers.

The takeaway

Upper-layer tests earn their keep by checking seams: responses against a contract, screens against every outcome a server can give, and a handful of journeys against reality. Leave the arithmetic to the layers that own it.

What the book covers

  • Per-layer coverage targets, including those for API contracts and UI components
  • Checks beyond status codes: response headers, pagination links, rate-limit headers and compatibility with earlier API versions
  • An end-to-end order workflow driven through tools, and a browser journey test with Playwright