Skip to main content

Testing

A frontend has no database or broker to integration-test; what protects it is a layered set of gates, from fast unit tests to browser journeys and performance budgets, all runnable without a backend because the stub BFF stands in for storefront-bff.

LayerToolWhereCount todayBackend needed
Lint and typeseslint, react-router typegen + tsc --noEmitwhole repon/ano
Unit and componentvitest 5, jsdom, Testing Libraryapp/**/*.test.{ts,tsx}143 it/test declarations over 21 files (a static count; each tables expand at run time)no
Loader and actionvitest + React Router createRoutesStubapp/routes/*.test.ts(x)loaders.test.ts 6, actions.test.ts 15, pages.test.tsx 18, pages2.test.tsx 18no
Server and stubvitest (node APIs)server/proxy.test.ts (5), server/stub.test.ts (5)10no
Sensor logicvitestscripts/bundle-scan.test.ts1no
Bundle sensorscripts/check-bundle.mjsover build/clientCI job no-network-url-in-bundleno
End-to-endPlaywright (chromium)e2e/*.spec.ts5 journeys + 3 security checks run in CI; 1 live purchase skipped unless E2E_LIVE=1stub by default, real BFF with E2E_BASE_URL
PerformanceLighthouse CIlighthouserc.cjs4 URLs x mobile and desktop x 3 runsstub
Charthelm, bash and Pythoncharts/storefront-web/tests/render_test.sh, test_service_selectors.pyno
Agent harnessPythonscripts/harness/guide_lint.py, repo_lint.py, test_hook.py, test_red_issue.py, test_repo_lint.pyno

There are no contract tests against the BFF's OpenAPI document, no mutation testing and no visual regression. Contract drift is instead caught by the stub, which enforces the upstream rules (see below).

Running​

TargetCommandNotes
Lintmake lint / npm run linteslint . with typescript-eslint, eslint-plugin-react-hooks, eqeqeq, no any, consistent type imports, and a no-restricted-syntax rule that bans absolute http(s):// string literals in app code (ADR 0002; relaxed for *.server.ts, tests, e2e/, server/, scripts/, config files) (eslint.config.js)
Typesmake typecheck / npm run typecheckreact-router typegen && tsc --noEmit
Unit testsmake test / npm testvitest run, jsdom
Coveragemake coverage / npm run test:coveragev8 provider over app/lib, app/components, app/routes; thresholds lines 90, functions 85, statements 88, branches 70 (vitest.config.ts)
Bundle sensormake bundle-checkbuild then node scripts/check-bundle.mjs
E2Emake e2e / npm run e2ebuilds, starts the stub on port 3100, runs chromium with one worker; first use needs npx playwright install chromium
E2E against a real BFFE2E_BASE_URL=http://localhost:3000 npm run e2eno web server is started; the target must already run
Live purchasenpm run e2e:livee2e/live-purchase.spec.ts; places a real order in the cluster with synthetic data
Lighthousemake lighthousebuild, then mobile and desktop budgets on port 3200
Everything fastmake checklint, typecheck, test, build
Everythingmake check-alllint, typecheck, coverage, bundle-check, e2e
Harnessmake guide-lint, make harness-testPython linters and hook tests
Chartbash charts/storefront-web/tests/render_test.sh, python3 charts/storefront-web/tests/test_service_selectors.pyneed helm; the Python test needs PyYAML

Unit, component and route tests​

vitest.config.ts includes app/**/*.test.{ts,tsx}, server/**/*.test.ts and scripts/**/*.test.ts, runs under jsdom, aliases ~ to app/, and loads app/test/setup.ts (jest-dom matchers, cleanup after each test, localStorage cleared, a 4 s async timeout for Testing Library because route stubs resolve loaders asynchronously).

  • app/lib/*.test.ts: the pure logic. api.test.ts (headers, Idempotency-Key, cookie forwarding, problem parsing), queries.test.ts (request bodies, notably {destinationPostalPrefix}), destination.test.ts (3-character prefix), dates.test.ts (promise windows in the destination's time zone, switching TZ), cart-session.test.ts, checkout.test.ts, payment.test.ts, promise.test.ts, departments.test.ts, order-status.test.ts, money.test.ts, search-params.test.ts, action-id.test.tsx.
  • app/components/*.test.tsx: rendering and interaction through Testing Library (components.test.tsx, CartLink.test.tsx).
  • app/routes/loaders.test.ts: server loaders receive a Request carrying a cookie and must forward it to the BFF (opts?.cookie).
  • app/routes/actions.test.ts, pages.test.tsx, pages2.test.tsx: client actions and whole route components mounted with createRoutesStub, with ~/lib/queries.ts mocked.
  • server/proxy.test.ts: the proxy forwards method, path and query to the BFF origin, pins the upstream Host, pipes bodies, rejects absolute-form and protocol-relative request targets with 400, and reports an unreachable upstream as 503 problem+json.
  • server/stub.test.ts: the stub enforces the upstream contracts: strict decoders (an unknown field is a 400), DESTINATION_NOT_SERVED for any prefix other than 786/787, and 422 session-required on GET /carts without a shopper.
  • scripts/bundle-scan.test.ts: each rule of the bundle sensor fires on a sample (for example process.env.BFF_URL yields backend-env).

Rules from the repo's own guide (.claude/skills/how-to-test/SKILL.md): a bundle-sensor hit names the rule and the string, so fix the code and never the allow-list unless the string is a pure namespace; fix a Lighthouse failure in the page (image priority, JS weight, layout shift), never the budget; and do not run two builds at once, because a rebuild under a running server mismatches asset hashes. When an incident escapes, add the guard test and a row to INCIDENTS.md.

The stub BFF​

server/stub*.ts is an in-memory, stateful stand-in for storefront-bff, mounted under /api/storefront when STOREFRONT_STUB=1 (ADR 0005). It serves synthetic departments and products (server/fixtures.ts, for example item-kettle, item-mug), carts, delivery quotes, checkout sessions with the three payment scenarios, orders and shoppers, sets the sf_state cookie the way the BFF does, and returns problem+json for errors. It exists so CI needs no running backend and so the web cannot drift unnoticed from contracts the BFF relays unchanged. The incident log shows why: a body field sent as postalPrefix (instead of destinationPostalPrefix) and a full ZIP sent as the destination both passed a lenient fake and failed on the live cluster; the stub now rejects both.

End-to-end journeys (Playwright)​

playwright.config.ts: testDir: e2e, one worker, chromium (Desktop Chrome), trace: retain-on-failure, one retry and an HTML report when CI is set. Without E2E_BASE_URL it starts npm run build && STOREFRONT_STUB=1 PORT=3100 node server/index.ts and waits on /healthz (120 s timeout).

FileTestWhat it proves
journeys.spec.tsbrowse, product promise, cart, checkout, confirmationthe happy path; the destination body is exactly {destinationPostalPrefix: "787"}; no /api/storefront response with status 400 or above on the way
journeys.spec.tsa ZIP outside the service areas"This item cannot be delivered to ZIP 10001 yet."
journeys.spec.tsa declined paymentthe shopper stays on checkout with the declined alert
journeys.spec.tsorders, detail with promise and tracker, cancelthe order goes from Placed to Cancelled
journeys.spec.tsdepartment navigationfilters by category id, facets show names not ids, every /api/* call starts with /api/storefront/
security.spec.tsstrict CSPthe header has default-src 'none', a nonce script-src, no unsafe-inline/unsafe-eval, frame-ancestors 'none'; seven pages load with no CSP console violation
security.spec.tsdepartment illustrationsserved by this origin as image/svg+xml
security.spec.tsin-doubt paymentthe "Time out" scenario shows "placing your order" and then settles on the confirmation
live-purchase.spec.tsbuy on the live networkskipped unless E2E_LIVE=1; product, promise, registration, delivery choice, Approve checkout, same promise window on confirmation and order detail

Shared steps (addToCart, register, signIn, chooseDelivery, checkout, seePromise) live in e2e/steps.ts.

The no-network-url-in-bundle sensor​

scripts/check-bundle.mjs walks build/client (.js .mjs .css .html .json .map) and applies scripts/bundle-scan.mjs. It fails (exit 1) on any hit of: other-api-route (an /api/ or /graphql/ path other than /api/storefront), in-cluster-host (*.svc, cluster.local, loopback with a port), service-endpoint (one of fifteen network service names used as an endpoint), backend-env (the string BFF_URL) and absolute-url (any host outside localhost, www.w3.org, react.dev, reactrouter.com, github.com, reactjs.org, developer.mozilla.org). It exits 2 when there is nothing to scan. The Dockerfile runs it too, so an image cannot be built from a leaking bundle.

Lighthouse budgets​

lighthouserc.cjs starts STOREFRONT_STUB=1 PORT=3200 node server/index.ts, audits only the performance category, three runs per URL, and asserts on the median run:

MetricBudget (error above)
largest-contentful-paint2500 ms
total-blocking-time (lab proxy for INP)200 ms
cumulative-layout-shift0.1

URLs: /, /search?q=mug, /products/item-kettle, /cart. LHCI_FORM_FACTOR=desktop switches to the desktop preset; CI runs mobile first and desktop second, and uploads .lighthouseci as the lighthouse-reports artifact. Reports are written to .lighthouseci/mobile or .lighthouseci/desktop.

CI jobs (.github/workflows/ci.yml)​

Runs on pushes and pull requests to develop and main.

JobRequiredWhat it guards
guide-lintyesguide_lint.py, repo_lint.py, test_hook.py, test_red_issue.py, test_repo_lint.py (agent guides, repo lint, hook tests)
webyesnpm ci, lint, typecheck, test:coverage (with the thresholds), build
file-lengthnoon pull requests into develop: at most 300 lines of code in every .ts/.tsx file the PR touches (oxlint max-lines, .oxlintrc.file-length.json); informational elsewhere
no-network-url-in-bundleyesbuild then check:bundle
lighthouse-budgetsyesbuild, lhci mobile, lhci desktop with CHROME_PATH=/usr/bin/google-chrome
e2eyesnpx playwright install --with-deps chromium, npm run e2e with CI=true; uploads playwright-report and test-results on failure
docker-buildyesdocker build -t storefront-web:ci . (which re-runs the bundle sensor inside the build)
helm-lintnopull requests only: ct lint and helm lint with each charts/storefront-web/ci/*.yaml
chart-testsnoevery charts/storefront-web/tests/*.sh and test_*.py; a test printing SKIP fails the job

Other workflows: codeql.yml (code scanning), ai-review.yml and docs.yml (builds this site on pull requests, deploys on push to main). The documentation build is separate from the required checks.

Adding a test​

  1. Logic in app/lib: add <name>.test.ts beside it and run npm test; route tests mount the component with createRoutesStub and mock ~/lib/queries.ts.
  2. A new BFF call: add the function to app/lib/queries.ts, assert its path, method and body in queries.test.ts, give the stub the route in server/stub*.ts with the same strictness as the real contract, and extend a journey if the shopper flow changes.
  3. A new page: add the route to app/routes.ts, a loader (server) or clientLoader (needs the cookie) and a route test; check Lighthouse if it is on the critical path.
  4. Anything in the browser that touches a new origin must fail no-network-url-in-bundle or the CSP journey: fix the code, not the gate.