Skip to content

Testing a Nuxt App with Vitest and Playwright

Unit tests with Vitest plus API and browser tests with Playwright against a real production server and PostgreSQL. Only Polar and email are mocked.

The starter kit has three kinds of tests. The core flows run against a real production server and a real PostgreSQL database. Only external services (Polar and the email API) are mocked.

KindFile nameRunnerWhat it covers
Unit*.test.tsVitestPure functions, e.g. admin access rules, upload validation and i18n key parity
API*.api.tsPlaywright, no browserHTTP endpoints: auth flows, permissions, CRUD, emails
e2e*.e2e.tsPlaywright, ChromiumCritical user flows in the browser, including a passkey sign-in and a cross-tab sign-out

Every layer keeps its tests in its own test/ folder, so deleting a feature also deletes its tests. Shared fixtures and helpers are in apps/web/test/.

Running the Tests

pnpm test:unit    # Vitest, no server or database needed
pnpm test:api     # Playwright API tests only
pnpm test:e2e     # Playwright API and browser tests
pnpm test:e2e:ui  # Playwright UI mode
pnpm test         # Unit tests, then API and e2e tests

Run a single file or test:

pnpm --filter web exec playwright test layers/waitlist/test/waitlist.api.ts
pnpm --filter web exec playwright test --grep "password reset"

PostgreSQL

API and e2e tests need PostgreSQL. Start one locally with Docker:

docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres:17

The global setup creates the test database from apps/web/.env.test if needed and applies the migrations.

Production Build

Playwright tests a production build on port 9009 with the values from apps/web/.env.test. Locally it runs pnpm build first. With CI=true it reuses the existing .output.

Writing Tests

  • Mocked services: apps/web/test/mock-services.mjs replaces Polar and the Plunk email API. Read sent emails with waitForEmail() from apps/web/test/support/mail.ts.
  • Fixtures from apps/web/test/fixtures.ts:
    • api: an anonymous API client
    • createUser({ role }): a verified, signed-in user with its own api client
    • loginAs(user): puts the session into the browser
  • Parallel runs: tests run in parallel against one database. Use uniqueEmail() for new data.
  • Rate limits: better-auth rate-limits per client IP. The fixtures give every client its own x-forwarded-for IP, so send requests through them.
  • Unit tests: keep logic that needs a unit test free of Nuxt auto-imports, e.g. in server/lib/.

Continuous Integration (CI)

GitHub Actions runs on pushes to main and on pull requests against main (.github/workflows/ci.yml):

  • lint, typecheck, unit tests and build
  • API and e2e tests against a PostgreSQL service
  • a feature-removal matrix that deletes each optional layer once and runs typecheck, unit tests and the build without it

docker-image.yml builds and smoke-tests the Docker image.