Testing
Groundwork's tests run on Vitest and Testing Library for unit and component tests, and Playwright for end-to-end flows. CI enforces coverage thresholds of 75% branches, 85% functions, 70% lines and 75% statements across app/lib/, and only there. This doc explains why the threshold is scoped that way, where tests live, what the four Playwright specs actually check (accessibility, search, account-login, browse-to-cart), and a real gotcha you'll hit if you write a similar test against Hydrogen's cart hooks.
It's for a developer adding tests to a feature they're building on Groundwork, or an agency lead checking the coverage claims before signing off on the template for a client project. Afterwards you'll know how to run the suite locally, what "coverage enforced on app/lib/" means in practice, and the two fixes already in place for testing components that use useOptimisticCart (server.deps.inline and an IntersectionObserver stub).
Related: Architecture for why app/lib/ stays framework-light in the first place, Contributing for the pre-PR check suite that runs these same commands, and Theme Contract for the contrast pairings the accessibility suite verifies.
Source: docs/TESTING.md
This template uses Vitest with Testing Library for unit and component tests, and Playwright for end-to-end tests.
Running tests
npm run test # unit/component tests, run once
npm run test:watch # unit/component tests, watch mode
npm run test:coverage # unit/component tests, with a coverage report
npm run test:e2e # end-to-end tests (starts its own dev server)
npm run test:e2e:ui # end-to-end tests, interactive UI mode
npm run build:vercel && npm run test:e2e:vercel # end-to-end tests against the Vercel build
Why a separate Vitest config
vite.config.ts wires up the Oxygen and React Router Vite plugins, which run the server build in workerd (the Oxygen runtime) during npm run dev. Unit tests run in Node with a jsdom environment instead, so vitest.config.ts is a separate, minimal config (React + vite-tsconfig-paths, no Hydrogen/Oxygen plugins). Both configs share the same ~/* path alias as the app.
Where tests live
Tests are colocated with the code they test, using a .test.ts/.test.tsx suffix:
app/lib/variants.ts
app/lib/variants.test.ts
app/components/ProductPrice.tsx
app/components/ProductPrice.test.tsx
This keeps a test next to the thing it covers and makes it obvious when new code has no test yet.
What to test
This template ships two examples that cover the two kinds of things you'll test most in a Hydrogen app:
app/lib/variants.test.tstests a pure function (getVariantUrl). Plain input/output assertions, no rendering needed. Most ofapp/lib/falls into this category and is the cheapest, highest-value code to test.app/components/ProductPrice.test.tsxtests a presentational component, rendered with Testing Library. Its assertions check what a user would actually see (screen.getByText(...)) and leave implementation details alone.
The easiest way to test a route loader/action is usually to extract its logic into a plain function in app/lib/ and test that directly, so you don't have to mock the full request/context lifecycle. Full request/response cycles, with a user actually clicking through the app in a browser, are covered by the Playwright e2e suite described below.
End-to-end tests
e2e/ holds Playwright specs that drive a real browser against the app:
browse-to-cart.spec.ts: browse a product, add it to the cart, confirm the cart drawer and cart page both reflect it, and that a checkout link is present.search.spec.ts: full search results, the empty-results state, and predictive search.account-login.spec.ts: unauthenticated visitors aren't shown the account dashboard, and the "Sign in" link points at/account.accessibility.spec.tsruns axe-core (via@axe-core/playwright) against the home page, a product detail page, a collection page, and the open cart drawer, asserting zero WCAG 2.1 A/AA violations. This is automated-scan coverage only: contrast, missing labels/alt text, invalid ARIA, landmark uniqueness, and similar. It doesn't replace a manual keyboard-only pass or a screen reader check, which are still the only way to verify things like focus order and announcement quality.
These run against Mock.shop, the same backend the app uses by default, so you don't need a live Shopify store or credentials to run them. playwright.config.ts starts vite dev directly as the e2e web server. It skips shopify hydrogen dev because the e2e run doesn't need the Shopify CLI's extra tooling.
One deliberate gap: account-login.spec.ts only checks the logged-out state. Mock.shop doesn't implement the Customer Account API's OAuth flow, so there's no test backend to log in against without pointing at a real store. If you connect a real store, consider adding a full login-flow test.
The same suite also runs against the Vercel build: npm run build:vercel, then npm run test:e2e:vercel. playwright.vercel.config.ts serves the Vercel server bundle under Node.js with scripts/serve-vercel-build.mjs instead of starting the dev server. CI runs both. See DEPLOYMENT.md for what that stand-in can't reproduce.
Run them locally with npm run test:e2e (or npm run test:e2e:ui for Playwright's interactive UI). The first run will prompt you to install browser binaries via npx playwright install.
Coverage
npm run test:coverage uses @vitest/coverage-v8 and reports on everything in app/, excluding generated files (*.generated.ts, GraphQL codegen output) and the framework entry points (entry.client.tsx, entry.server.tsx), which are mostly framework wiring with little app logic in them.
That said, the coverage threshold enforced in CI, and the badge in the README, are scoped to app/lib/ specifically (vitest.config.ts's coverage.thresholds) and leave the rest of app/ out. Most of app/routes and app/components is JSX that the e2e suite exercises better than unit tests asserting on rendered markup would. A repo-wide coverage number would either sit artificially low (by the template's own design) or get inflated with low-value tests just to hit a target. app/lib/ is where this template's actual business logic lives, and it's realistic to hold that code to a real bar: currently enforced at 75%/85%/70%/75% (statements/branches/functions/lines), a few points below the measured baseline as headroom.
Regenerate the badge locally with npm run coverage:badge. It runs the coverage suite and writes badges/coverage.json in shields.io's endpoint-badge format. In CI, this happens automatically on every push to main and is committed back to the repo (see below).
Testing components that use the cart store
Components that read or change the cart (CartMain, CartSummary, anything rendering AddToCartButton) use Hydrogen's cart store, so their tests render them inside <CartProvider initialData={{cart}}> from app/lib/cart.ts, seeded with a fixture cart ({cart: null} for an empty one). Two other things are already wired up and worth knowing about if you write a similar test:
vitest.config.ts'stest.server.deps.inline: ['@shopify/hydrogen']. By default Vitest externalizesnode_modulespackages, letting Node load them directly instead of processing them through Vite's resolver. That can leave@shopify/hydrogenwith its own copy of React (or React Router), distinct from the one the test imports. Two module instances means two separate React contexts, which is how classic Hydrogen's cart hooks once threw "must be used within a data router" inside a realRouterProvider. Inlining the package forces both imports through the same resolver.- An
IntersectionObserverstub intest/setup.ts. jsdom doesn't implement it, and React Router's<Link>uses it internally for viewport prefetching, so anything rendering a<Link>needs at least a no-op stub.
If you hit a similar "works in the browser, throws in tests" error with another Hydrogen hook, server.deps.inline is the first thing to check.
CI
Every push and pull request runs formatting, linting, typechecking, unit tests with coverage, a production build, and the Playwright e2e suite (.github/workflows/ci.yml). On pushes to main, a second job regenerates badges/coverage.json and commits it back if the number changed.