How to test your Nuxt application.
In Nuxt 3, we have a rewritten version of @nuxt/test-utils available as @nuxt/test-utils-edge. We support Vitest and Jest as the test runner.
yarn add --dev @nuxt/test-utils-edge vitestIn each describe block where you are taking advantage of the @nuxt/test-utils helper methods, you will need to set up the test context before beginning.
import { describe, test } from 'vitest'import { setup, $fetch } from '@nuxt/test-utils-edge'describe('My test', async () => { await setup({ // test context options }) test('my test', () => { // ... })})Behind the scenes, setup performs a number of tasks in beforeAll, beforeEach, afterEach and afterAll to set up the Nuxt test environment correctly.
rootDirPath to a directory with a Nuxt app to be put under test.
string'.'configFileName of the configuration file.
string'nuxt.config'setupTimeoutThe amount of time (in milliseconds) to allow for setupTest to complete its work (which could include building or generating files for a Nuxt application, depending on the options that are passed).
number60000serverWhether to launch a server to respond to requests in the test suite.
booleantruebuildWhether to run a separate build step.
booleantrue (false if browser or server is disabled)browserUnder the hood, Nuxt test utils uses playwright to carry out browser testing. If this option is set, a browser will be launched and can be controlled in the subsequent test suite. (More info can be found here.)
booleanfalsebrowserOptionsobject with the following propertieschromium, firefox or webkitobject of options that will be passed to playwright when launching the browser. See full API reference.runnerSpecify the runner for the test suite. Currently, Vitest is recommended.
'vitest' | 'jest''vitest'$fetch(url)Get the HTML of a server-rendered page.
import { $fetch } from '@nuxt/test-utils'const html = await $fetch('/')fetch(url)Get the response of a server-rendered page.
import { fetch } from '@nuxt/test-utils'const res = await fetch('/')const { body, headers } = resurl(path)Get the full URL for a given page (including the port the test server is running on.)
import { url } from '@nuxt/test-utils'const pageUrl = url('/page')// 'http://localhost:6840/page'