---
title: "Run end-to-end tests"
description: "Drive the real app in a browser with Playwright: sign in with a code read from the dev inbox as JSON, pay with the test card in Polar's sandbox, and assert what people see."
canonical_url: "https://nuxt-backend.dev/docs/develop/testing/e2e"
---
# Run end-to-end tests

> Drive the real app in a browser with Playwright: sign in with a code read from the dev inbox as JSON, pay with the test card in Polar's sandbox, and assert what people see.

Version: nuxt-backend 0.6.x · Tab: Develop · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

Run the whole app in a real browser, on a dev deployment, with every provider in its test mode. A test signs in with a sign-in code, buys credits with a test card and checks what the page shows.

- **What powers it natively:** [Playwright](https://playwright.dev) drives the browser. Resend's test addresses accept mail without delivering it, and Polar's sandbox takes Stripe's test cards.
- **What the package adds:** `npx nuxt-backend inbox <address> --json`, which reads the sign-in code a test address received. On a dev deployment, `BACKEND_ENVIRONMENT` is `development`, which keeps both providers in test mode whatever keys it holds.

## Before you start

- **A dev deployment.** Run `npx nuxt-backend dev` once, so `.env.local` names one ([Run the dev loop](https://nuxt-backend.dev/docs/develop/dev-loop)). A local one works: the return from checkout refreshes the balance once. Only a test of what a webhook alone changes, such as a renewal, needs a cloud dev deployment Polar can reach.
- **The test-mode keys** in `.env.local`, pushed to the deployment. `EMAIL_API_KEY` is a Resend key ([Resend](https://nuxt-backend.dev/docs/services/resend#sandbox-and-test-mode)): without it nothing is sent, and the code prints only in the terminal, where a test can't read it. `BILLING_ACCESS_TOKEN` is a Polar sandbox token, for the purchase test ([Polar](https://nuxt-backend.dev/docs/services/polar#sandbox-and-test-mode)).

```bash
npx nuxt-backend env push      # forwards the keys in .env.local to the deployment
npx nuxt-backend billing sync  # once: the catalog in Polar's sandbox
```

## Set up Playwright

Install Playwright, its browser, and axe for the [accessibility check](#check-accessibility-in-a-browser):

```bash
npm install -D @playwright/test @axe-core/playwright
npx playwright install chromium
```

The config starts the app, or reuses the one you already run. It has three projects: `setup` signs in once and saves the session, `visitor` runs the tests that start signed out, and `signed-in` starts every other test from the saved session:

```ts [playwright.config.ts]
import { defineConfig, devices } from '@playwright/test'

export default defineConfig({
  testDir: 'test/e2e',
  timeout: 90_000,
  retries: process.env.CI ? 1 : 0,
  use: {
    baseURL: 'http://localhost:3000',
    trace: 'on-first-retry',
  },
  webServer: {
    // In CI, an account-free local deployment that needs no login.
    command: process.env.CI ? 'npx nuxt-backend dev --anonymous' : 'npm run dev',
    url: 'http://localhost:3000/login',
    reuseExistingServer: !process.env.CI,
    timeout: 180_000,
  },
  projects: [
    { name: 'setup', testMatch: /.*\.setup\.ts/ },
    {
      name: 'visitor',
      testMatch: /(sign-in|a11y)\.test\.ts/,
      use: { ...devices['Desktop Chrome'] },
    },
    {
      name: 'signed-in',
      testIgnore: /(sign-in|a11y)\.test\.ts/,
      use: { ...devices['Desktop Chrome'], storageState: 'test/e2e/.auth/person.json' },
      dependencies: ['setup'],
    },
  ],
})
```

`npm run dev` runs `nuxt-backend dev`, which pushes the backend before Nuxt starts, so the first page already talks to your current functions.

## Sign in from a test

Give each test a test address of its own, `delivered+<label>@resend.dev`. The inbox then holds only that test's mail, and the per-address rate limit on codes never trips across tests.

Read the code with the CLI. It reads once and exits, so poll it until a code arrives:

```ts [test/e2e/support.ts]
import { execFile } from 'node:child_process'
import { randomUUID } from 'node:crypto'
import { promisify } from 'node:util'
import { expect, type Page } from '@playwright/test'

const run = promisify(execFile)

/** A test address only this run uses. */
export function testAddress(label = 'e2e') {
  return `delivered+${label}-${randomUUID().slice(0, 8)}@resend.dev`
}

/** Polls the dev inbox until the address has a sign-in code, and returns it. */
export async function readCode(address: string): Promise<string> {
  let code: string | null = null
  await expect.poll(async () => {
    const { stdout } = await run('npx', ['nuxt-backend', 'inbox', address, '--json'])
    code = JSON.parse(stdout).code
    return code
  }, { timeout: 30_000, intervals: [1_000, 2_000, 3_000] }).not.toBeNull()
  return code!
}

/** Signs in on /login with a sign-in code, as a person would. */
export async function signIn(page: Page, address = testAddress()) {
  await page.goto('/login')
  await page.getByRole('button', { name: 'Continue with email code' }).click()
  await page.getByLabel('Email').fill(address)
  await page.getByRole('button', { name: 'Email me a code' }).click()
  await page.getByLabel('Verification code').fill(await readCode(address))
  await page.getByRole('button', { name: 'Verify' }).click()
  // After a code, the form offers to save a passkey where the browser supports
  // passkeys, as Chromium does. Elsewhere it skips the offer itself.
  await page.getByRole('button', { name: 'Skip for now' }).click({ timeout: 10_000 }).catch(() => {})
  await expect(page).not.toHaveURL(/\/login/)
  return address
}
```

```ts [test/e2e/sign-in.test.ts]
import { expect, test } from '@playwright/test'
import { signIn } from './support'

test('a new person signs in and lands on the app', async ({ page }) => {
  await signIn(page)
  await expect(page.getByRole('main')).toBeVisible()
})
```

The code only works in the browser that asked for it, so the test types it into the same page. The labels are the package's English defaults: if you changed them in [Labels](https://nuxt-backend.dev/docs/reference/configuration/labels), use yours.

## Pay with the test card

Open the pricing page, start checkout, and pay on Polar's sandbox checkout with `4242 4242 4242 4242`, any future expiry and any CVC ([Polar's sandbox](https://polar.sh/docs/integrate/sandbox), checked on 2026-10-25):

The test runs in the `signed-in` project, so it starts from the saved session. It reads the balance on `/settings` before and after, and asserts it changed:

```ts [test/e2e/credits.test.ts]
import { expect, test } from '@playwright/test'

test('a person buys a credit pack', async ({ page }) => {
  const balance = page.locator('[data-settings="balance"]')
  await page.goto('/settings')
  const before = await balance.innerText()

  await page.goto('/pricing')
  await page.getByRole('button', { name: 'Top up' }).first().click()

  // Polar's hosted checkout. Its card fields sit in the payment processor's frame.
  await page.waitForURL(/sandbox\.polar\.sh/)
  const card = page.frameLocator('iframe[title*="payment"]').first()
  await card.getByLabel('Card number').fill('4242 4242 4242 4242')
  await card.getByLabel(/Expiration/).fill('12 / 34')
  await card.getByLabel('Security code').fill('123')
  await page.getByRole('button', { name: /^(Pay|Subscribe)/ }).click()

  // Back in the app, the balance includes the pack.
  await page.waitForURL(/localhost:3000/, { timeout: 60_000 })
  await page.goto('/settings')
  await expect(balance).not.toHaveText(before, { timeout: 30_000 })
})
```

Polar owns the checkout page, and its fields and button labels can change between its releases. Keep the checkout steps in one helper, so one edit follows a change. Checkout may also ask for a name or a billing country: fill those the same way.

## Sign in once per run

Signing in for every test is slow. The `setup` project in the config signs in once and saves the session, and the `signed-in` project starts from it:

```ts [test/e2e/auth.setup.ts]
import { test as setup } from '@playwright/test'
import { signIn } from './support'

setup('sign in', async ({ page }) => {
  await signIn(page)
  await page.context().storageState({ path: 'test/e2e/.auth/person.json' })
})
```

Add `test/e2e/.auth/` to `.gitignore`: the file holds a live session.

## Check accessibility in a browser

Contrast needs a real layout, which a page test doesn't have. Run axe in the browser with `@axe-core/playwright`:

```ts [test/e2e/a11y.test.ts]
import AxeBuilder from '@axe-core/playwright'
import { expect, test } from '@playwright/test'

test('the sign-in page has no WCAG 2.2 AA violations', async ({ page }) => {
  await page.goto('/login')
  const { violations } = await new AxeBuilder({ page })
    .withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa', 'wcag22aa'])
    .analyze()
  expect(violations).toEqual([])
})
```

## Run it

```bash
npx playwright test
```

Playwright starts the app, signs in once, and runs each project. A failed test leaves a trace of its retry: open the report with `npx playwright show-report`.

## What happens when…

- **No code arrives:** the deployment has no `EMAIL_API_KEY`, or the address isn't a Resend test address. Outside production, a send to any other address is refused.
- **inbox exits with 1:** it read a deployment that isn't a dev deployment. It refuses production, preview deployments and a deploy key for anything but a dev one ([`inbox`](https://nuxt-backend.dev/docs/reference/cli#inbox)).
- **The page says to wait before asking again:** the test reused an address, and the per-address limit on codes refused it ([`rate_limited`](https://nuxt-backend.dev/docs/reference/backend/errors#rate_limited)). Use a fresh address per test.
- **The balance doesn't change after checkout:** the browser didn't come back to the app, or a later change waits on Polar's webhook, which can't reach a local deployment. Use a cloud dev deployment, or a tunnel ([Reach your machine from outside](https://nuxt-backend.dev/docs/develop/dev-loop#reach-your-machine-from-outside)).

## Limits

- The dev inbox lists what one address received in the last hour.
- Test sends count against your Resend quota.
- The dev inbox reads dev deployments only. A demo or staging app on `BACKEND_ENVIRONMENT=sandbox` publishes it to the page instead, once it opts in with `EMAIL_DEV_INBOX=1` ([Read test mail in the dev inbox](https://nuxt-backend.dev/docs/develop/dev-inbox#read-it-in-a-page)).
- Resend sends every event of a team to each of the team's webhooks. Give the app's dev deployment a Resend team apart from production's.

## Related

- [Read test mail in the dev inbox](https://nuxt-backend.dev/docs/develop/dev-inbox): what `inbox --json` prints.
- [Develop on real sandboxes](https://nuxt-backend.dev/docs/develop/sandboxes): the test addresses and the test card.
- [Check every change in CI](https://nuxt-backend.dev/docs/develop/testing/ci): this suite on every pull request.
- [Polar](https://nuxt-backend.dev/docs/services/polar) and [Resend](https://nuxt-backend.dev/docs/services/resend): the sandbox accounts.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
