Caching and Error Translation in the Resources Layer
Caching and error translation both hide something from callers: where data came from and how a vendor fails. See how the Resources layer handles each.
The tests for your Resources code all pass, the deploy goes out, and checkout fails because the carrier quietly renamed a field. Everything you tested was your own understanding of the carrier. The Resources layer is awkward to test because it is the code that meets slow, stateful, unpredictable systems, and one kind of test cannot cover that. The book uses three.
Each technique answers a different question about a repository or adapter.
The first catches your mistakes, the second catches the database's opinions, and the third catches the other side changing. Skip one and that category of failure waits for production.
The mock-based tests are the ones you will write most often, so give them the layer's characteristic scenarios: a cache hit that should skip the database, a duplicate-key error that should become a domain conflict, and a vendor timeout that should be marked transient.
A realistic suite uses all three, in that order of quantity. Mock-based tests are numerous because they are cheap. Container tests are fewer because they start real infrastructure. Contract tests are fewer still, but they guard the seam where you have the least control.
Retry behaviour is a good candidate for a unit test, provided time is injected so the test does not wait:
it("retries a 503 and then succeeds", async () => {
const carrier = fakeCarrier([http(503), http(503), ok({ code: "IN_TRANSIT" })]);
const tracking = new TrackingResource(carrier, { sleep: async () => {} });
const result = await tracking.statusFor("TRK-1");
expect(result.status).toBe("in_transit");
expect(carrier.calls).toBe(3);
});
Three details make this useful. The fake carrier is scripted, so the failure is repeatable. The sleep function is replaced, so the test takes milliseconds. And the assertion is about the domain value, in_transit, rather than the vendor's code, which checks translation and retry together.
The book sets the coverage target for Resources at 80% or higher, lower than for the bottom layers, and gives the reason. Some failure paths, such as network partitions and TLS handshake failures, are hard to reproduce deterministically, so chasing every line would waste effort. It then names what must not be skimped on: mapping logic, error translation, caching behaviour, and the state transitions of retry and circuit-breaker code.
There is a reason the number is lower here than in the foundation layers. Utils and Schema contain no I/O, so every line can be exercised with ease. Resources code is dominated by interactions with the outside world, and some of those interactions simply cannot be staged in a test run.
That list is a handy checklist. Whatever the percentage says, if a mapping function or a breaker transition has no test, the number is hiding a real gap.
Match the test to the question, and spend your coverage where the layer does its real work: translation, errors, caching and resilience. For the code under test, see resilience patterns and caching and error translation. The wider picture of how tests are shaped across layers is in the LVD testing pyramid.