Test doubles and limits
What you need to know
A test double replaces a real dependency with a controlled implementation. It helps verify application decisions without a network or database, but its fidelity must be stated.
By the end, you can: distinguish mock from real framework behavior; choose integration tests.
Inject a small read dependency
type Book={title:string};async function label(read:()=>Promise<Book|null>){ const book=await read();return book?book.title:'Not found';}async function demo(){ console.log(await label(async()=>({title:'River Walk'}))); console.log(await label(async()=>null));}void demo();Expected behavior: River Walk and Not found on separate lines. The injected read function makes no external requests.
How it works
A stub supplies a known value or failure. A fake implements a limited working contract, such as an in-memory repository. A spy records how a dependency was used. Mock terminology varies by library; explain the actual behavior instead of relying on labels. Inject dependencies at explicit boundaries so tests can create isolated instances and simulate success, missing records and failures. Avoid mocking the very behavior being tested. A fake session is not an authenticated user, and an in-memory repository cannot prove real database constraints. Similarly, a mock of revalidateTag only demonstrates that a call was attempted, not that any cached route was refreshed. Async Server Components and actual framework request APIs should be covered with the reviewed integration fixture. Keep each double limited, deterministic and free of private data.
Failure and integration boundaries
Stub read rejects -> application error handlingFake store returns null -> missing-record branchReal disposable DB -> constraints and transaction testsReal production server -> route, session and cache behaviorExpected behavior: Each layer proves a different contract; none automatically proves the next.
Common mistake
Do not assert a mock called itself or configure a mock to always return the expected final result. Test your application decisions, not the fake implementation.
Practice and verification
Add a test for a rejected repository read and verify that the application does not silently convert a service failure to a missing record. List integration assertions for the real adapter.
Scope: Proposed Next.js 16.x / React 19, Cache Components disabled unless noted. Framework, browser and component examples are staged; local standalone tests are reported separately. Exact dependency patches and platform contracts pending.