---
title: "Testing a Nuxt App with Vitest and Playwright"
description: "Unit tests with Vitest plus API and browser tests with Playwright against a real production server and PostgreSQL. Only Polar and email are mocked."
canonical_url: "https://nuxtstarterkit.com/docs/testing"
---

> Index of every page on this site: [llms.txt](https://nuxtstarterkit.com/llms.txt)

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.

<table>
<thead>
  <tr>
    <th>
      Kind
    </th>
    
    <th>
      File name
    </th>
    
    <th>
      Runner
    </th>
    
    <th>
      What it covers
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Unit
    </td>
    
    <td>
      <code>
        *.test.ts
      </code>
    </td>
    
    <td>
      <a href="https://vitest.dev/" rel="nofollow">
        Vitest
      </a>
    </td>
    
    <td>
      Pure functions, e.g. admin access rules, upload validation and i18n key parity
    </td>
  </tr>
  
  <tr>
    <td>
      API
    </td>
    
    <td>
      <code>
        *.api.ts
      </code>
    </td>
    
    <td>
      <a href="https://playwright.dev/" rel="nofollow">
        Playwright
      </a>
      
      , no browser
    </td>
    
    <td>
      HTTP endpoints: auth flows, permissions, CRUD, emails
    </td>
  </tr>
  
  <tr>
    <td>
      e2e
    </td>
    
    <td>
      <code>
        *.e2e.ts
      </code>
    </td>
    
    <td>
      Playwright, Chromium
    </td>
    
    <td>
      Critical user flows in the browser, including a passkey sign-in and a cross-tab sign-out
    </td>
  </tr>
</tbody>
</table>

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

```bash
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:

```bash
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:

```bash
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.

## Sitemap

See the full [sitemap](https://nuxtstarterkit.com/sitemap.md) for all pages.
