@nuxt/test-utils, providing utilities and configuration for unit testing and end-to-end testing of your Nuxt application.
Installation
Install@nuxt/test-utils with your preferred test runner and environment:
Optional Dependencies
@nuxt/test-utils ships with optional peer dependencies:
- DOM environment: Choose
happy-domorjsdom - Test runners: Choose
vitest,cucumber,jest, orplaywright - Browser testing:
playwright-core(only if not using@playwright/test)
Unit Testing
Nuxt provides an environment for unit testing code that needs a Nuxt runtime environment. Currently supports Vitest only.Setup
1. Add the Nuxt Test Utils Module (Optional)
Add@nuxt/test-utils/module to your nuxt.config for Vitest integration in Nuxt DevTools:
2. Create Vitest Configuration
Create avitest.config.ts with project-based setup:
Organizing Your Tests
With the project-based setup, organize tests as follows:- Unit tests (
test/unit/): Run in Node environment for speed - Nuxt tests (
test/nuxt/): Run within Nuxt runtime environment - E2E tests (
test/e2e/): Run against a built Nuxt application
Alternative: Simple Setup
For a simpler setup where all tests run in the Nuxt environment:Running Tests
Run different test suites with the project setup:Testing Helpers
mountSuspended
Mount any Vue component within the Nuxt environment, allowing async setup and access to Nuxt plugins:mountSuspended wraps mount from @vue/test-utils. Check the Vue Test Utils documentation for available options.
renderSuspended
Render any Vue component within the Nuxt environment using@testing-library/vue:
Install
@testing-library/vue and enable testing globals in your Vitest config to use renderSuspended.mockNuxtImport
Mock Nuxt’s auto import functionality:mockNuxtImport can only be used once per mocked import per test file. It’s a macro that transforms to vi.mock.vi.hoisted:
mockComponent
Mock Nuxt components:registerEndpoint
Create Nitro endpoints that return mocked data:End-to-End Testing
Nuxt supports Vitest, Jest, Cucumber, and Playwright as end-to-end test runners.Setup
In eachdescribe block, set up the test context:
Setup Options
Nuxt Config
rootDir: Path to Nuxt app directory (default:'.')configFile: Configuration file name (default:'nuxt.config')
Timings
setupTimeout: Time for setup to complete (default:120000ms,240000ms on Windows)teardownTimeout: Time for teardown (default:30000ms)
Features
build: Run separate build step (default:true)server: Launch server for requests (default:true)port: Test server port (default:undefined)host: URL for testing against deployed app (default:undefined)browser: Launch browser for testing (default:false)browserOptions: Playwright browser optionsrunner: Test runner (default:'vitest')
Testing Against a Target Host
Test against a deployed or running application:E2E Testing APIs
$fetch(url)
Get the HTML of a server-rendered page:fetch(url)
Get the response of a server-rendered page:url(path)
Get the full URL for a given page:Browser Testing
createPage(url)
Create a configured Playwright browser instance:Playwright Test Runner
Use Nuxt with the Playwright test runner:Best Practices
- Separate test environments: Keep unit tests, Nuxt tests, and E2E tests in separate directories
- Use appropriate helpers: Choose
mountSuspendedfor components,mockNuxtImportfor composables - Mock external dependencies: Use
registerEndpointfor API mocking - Test user flows: Use E2E tests for critical user journeys
- Keep tests isolated: Avoid mutating global state; reset after each test
- Use TypeScript: Leverage type safety in your tests
- Run tests in CI: Automate testing in your deployment pipeline