Guides
Testing
Write unit tests for the rules, Nuxt tests for components and stores, and end-to-end tests for the screens.
The template ships with three kinds of tests and passes all of them.
Kind | Tool | Folder | What it covers |
|---|---|---|---|
| Unit | Vitest (unit project) | tests/unit/ | Pure functions in utils/, mocks, navigation coverage, the showcase index. |
| Nuxt | Vitest (nuxt project) | tests/nuxt/ | Components, composables and stores inside a real Nuxt app: useProviderState(), the auth store, BaseSegmentBar. |
| End-to-end | Playwright | tests/e2e/ | Real flows in the production build, on desktop and on a phone: sign in, filters, forms, drag and drop, quick views. |
Unit tests
Rules live in utils/ as pure functions, so their tests need no Vue and no browser:
import { describe, expect, it } from 'vitest'
import { reportStatus } from '~/utils/reports'
describe('reportStatus', () => {
it('is late after the due date', () => {
expect(reportStatus({ dueAt: '2026-01-01', sentAt: null }, new Date('2026-01-02'))).toBe('late')
})
})
Pass the current date as an argument instead of reading new Date() inside the rule, so the test is deterministic.
$npm run test$npx vitest run tests/unit/reports.test.ts$npx vitest --project unit$npm run test:coverage
npm run test runs both Vitest projects; --project unit runs only the fast one in watch mode. npm run test:coverage adds a V8 coverage report of components/, composables/, stores/ and utils/ (an HTML version lands in coverage/).
Tests that guard the structure
Test | Fails when |
|---|---|
navigation-coverage.test.ts | A page in pages/ is not reachable from the menu, or the menu links to a missing page. |
ui-showcase.test.ts | The component list in config/ui-showcase.config.ts and the showcase pages differ. |
mocks.test.ts | A mock returns data that breaks its model or the expected ids. |
Component, composable and store tests
Files in tests/nuxt/ run in the nuxt environment of @nuxt/test-utils: the app boots once with its plugins, Vuetify, i18n and Pinia, so auto-imports, useState and the stores work as they do in the browser (happy-dom stands in for the DOM).
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { describe, expect, it } from 'vitest'
import BaseSegmentBar from '~/components/ui/BaseSegmentBar.vue'
describe('BaseSegmentBar', () => {
it('names the segments for screen readers', async () => {
const wrapper = await mountSuspended(BaseSegmentBar, {
props: { segments: [{ id: 'a', label: 'Desktop', value: 60, color: 'chart-1' }] },
})
expect(wrapper.get('[role="img"]').attributes('aria-label')).toContain('Desktop')
})
})
- Mount with
mountSuspended, which waits for async setup and provides the Nuxt app. - Call a composable inside a component when it needs a component instance, such as
useI18n(): mount a smalldefineComponentwhosesetup()calls it (seetests/nuxt/provider-state.test.ts). - Give each test its own
useStatekey, because the app is shared by every test in the project.
Cannot read properties of undefined (reading 'config') or "Vitest failed to find the current suite", the terminal is in d:\… while Node resolves D:\…, and Vitest loads two copies of its runner. Open the folder as D:\… (in VS Code, File > Open Folder) and run the tests again.End-to-end tests
npm run test:e2e builds the app and runs Playwright against node .output/server/index.mjs, so you test what you deploy. Specs import test, expect and two helpers from tests/e2e/fixtures.ts instead of @playwright/test:
import { expect, gotoReady, test } from './fixtures'
test.use({ cookies: { 'reports-view': 'list' } })
test('exports the reports', async ({ page }) => {
await gotoReady(page, '/reports')
await page.getByRole('button', { name: 'Export all' }).click()
await expect(page.getByRole('status')).toContainText('Exported')
})
Option or helper | What it does |
|---|---|
signedIn (default true) | Sets the demo session cookie. test.use({ signedIn: false }) tests as a guest. |
appLocale (default en) | Sets the language cookie; null leaves the browser language. |
role | Signs in with a demo role, such as viewer. |
cookies | Extra cookies, such as a saved view. |
gotoReady(page, path) | Opens the page and waits until Nuxt has hydrated it (<html data-hydrated>, set by plugins/hydrated.client.ts), so clicks reach live components. |
waitForHydration(page) | The same wait after page.reload(). |
- Set the language and the session with the fixture options. It is faster than signing in through the form and keeps tests independent.
- Find elements by role and name, the way a user or a screen reader would. It also checks accessibility for free.
- Wait for what the user sees, with
expect(...).toBeVisible()ortoHaveURL(), not for timers ornetworkidle.
Tests run in two Playwright projects: desktop (1440 × 900) and mobile (Pixel 7). Tag a test with { tag: '@mobile' } to run it on the phone instead of the desktop:
test('lists orders as compact rows', { tag: '@mobile' }, async ({ page }) => {
await gotoReady(page, '/e-commerce/orders')
await expect(page.locator('.base-data-table--mobile-rows')).toBeVisible()
})
To debug a test, run it headed: npx playwright test tests/e2e/reports.spec.ts --headed. In CI a failed test is retried once with a trace, takes a screenshot and leaves an HTML report: open the playwright-report artifact with npx playwright show-report.
In continuous integration
name: CI
on: [push, pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
cache: npm
- run: npm ci
- run: npm run lint
- run: npm run typecheck
- run: npm run test
- run: npx playwright install --with-deps chromium
- run: npm run test:e2e
env:
E2E_BROWSER_CHANNEL: chromium