Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 17 / Topic 4 of 8

Testing a data-access function: app/quickcart/lib/products.ts

With both gotchas fixed, examples/02-products-dal.test.ts (copied verbatim from harness/app/quickcart/lib/products.test.ts) calls listProducts, getProduct, and updateProduct directly — no Next.js runtime involved at all, because none of Part 16's DAL functions touch cookies()/headers(). It runs against the real quickcart.sqlite file this harness's own capstone uses (not a mock, not a scratch copy); the one test that mutates a row (updateProduct) reads the row's previous values first and restores them at the end, so the suite is safe to run repeatedly and never leaves the capstone's seed data altered:

$ npm run test -- app/quickcart/lib/products.test.ts
 Test Files  1 passed (1)
      Tests  3 passed (3)

This is the cleanest case this part covers, and it's deliberately placed first: a synchronous, dependency-light function with no Next.js-specific API in it at all is the easiest thing in this whole stack to unit test, and most of a Next.js app's actual business logic (price math, validation, formatting) can live in functions shaped exactly like this one specifically so they stay this easy to test.

Done here? The practice set for Part 17 — including the solutions — is on the part page.

Search the guide