2022-10-06 09:15:30 +00:00
---
navigation.icon: uil:check-circle
---
2022-03-30 16:00:08 +00:00
# Testing
2022-10-06 09:15:30 +00:00
How to test your Nuxt application.
2022-03-30 16:00:08 +00:00
::alert{icon=👉}
2022-09-20 09:12:08 +00:00
Test utils are still in development and the API and behavior may change. Currently, it is in preview stage but not yet ready for testing production apps.
2023-03-18 06:53:54 +00:00
If you are a module author, you can find more specific information in the [Module Author's guide ](/docs/guide/going-further/modules#testing )
2022-03-30 16:00:08 +00:00
::
2022-12-06 11:51:17 +00:00
In Nuxt 3, we have a rewritten version of `@nuxt/test-utils` . We support [Vitest ](https://github.com/vitest-dev/vitest ) and [Jest ](https://jestjs.io/ ) as test runners.
2022-03-30 16:00:08 +00:00
## Installation
```bash
2022-12-06 11:51:17 +00:00
yarn add --dev @nuxt/test -utils vitest
2022-03-30 16:00:08 +00:00
```
## Setup
2022-04-16 13:53:36 +00:00
In 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.
2022-03-30 16:00:08 +00:00
```ts
import { describe, test } from 'vitest'
2022-12-06 11:51:17 +00:00
import { setup, $fetch } from '@nuxt/test-utils'
2022-03-30 16:00:08 +00:00
2022-06-20 16:53:45 +00:00
describe('My test', async () => {
2022-03-30 16:00:08 +00:00
await setup({
// test context options
})
test('my test', () => {
// ...
})
})
```
2022-04-16 13:53:36 +00:00
Behind the scenes, `setup` performs a number of tasks in `beforeAll` , `beforeEach` , `afterEach` and `afterAll` to set up the Nuxt test environment correctly.
2022-03-30 16:00:08 +00:00
## Options
2022-08-13 07:27:04 +00:00
### Nuxt Configuration
2022-03-30 16:00:08 +00:00
2022-04-06 05:56:08 +00:00
#### `rootDir`
2022-03-30 16:00:08 +00:00
Path to a directory with a Nuxt app to be put under test.
* Type: `string`
* Default: `'.'`
2022-04-06 05:56:08 +00:00
#### `configFile`
2022-03-30 16:00:08 +00:00
Name of the configuration file.
* Type: `string`
2022-07-13 17:02:33 +00:00
* Default: `'nuxt.config'`
2022-03-30 16:00:08 +00:00
2022-04-06 05:56:08 +00:00
<!--
2022-03-30 16:00:08 +00:00
#### config
Object with configuration overrides.
* Type: `NuxtConfig`
* Default: `{}` -->
2022-08-13 07:27:04 +00:00
### Setup Timings
2022-03-30 16:00:08 +00:00
2022-04-06 05:56:08 +00:00
#### `setupTimeout`
2022-03-30 16:00:08 +00:00
The 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).
* Type: `number`
* Default: `60000`
2022-08-13 07:27:04 +00:00
### Features to Enable
2022-03-30 16:00:08 +00:00
2022-04-06 05:56:08 +00:00
#### `server`
2022-03-30 16:00:08 +00:00
Whether to launch a server to respond to requests in the test suite.
* Type: `boolean`
* Default: `true`
2023-04-29 23:01:10 +00:00
#### `port`
If provided, set the launched test server port to the value.
* Type: `number | undefined`
* Default: `undefined`
2022-04-06 05:56:08 +00:00
#### `build`
2022-03-30 16:00:08 +00:00
Whether to run a separate build step.
* Type: `boolean`
* Default: `true` (`false` if `browser` or `server` is disabled)
2022-04-06 05:56:08 +00:00
#### `browser`
2022-03-30 16:00:08 +00:00
2023-03-09 11:34:41 +00:00
Under the hood, Nuxt test utils uses [`playwright` ](https://playwright.dev/ ) 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 ](/docs/getting-started/testing ).)
2022-03-30 16:00:08 +00:00
* Type: `boolean`
* Default: `false`
2022-04-06 05:56:08 +00:00
#### `browserOptions`
2022-03-30 16:00:08 +00:00
* Type: `object` with the following properties
* **type**: The type of browser to launch - either `chromium` , `firefox` or `webkit`
2022-12-03 12:25:12 +00:00
* **launch**: `object` of options that will be passed to playwright when launching the browser. See [full API reference ](https://playwright.dev/docs/api/class-browsertype#browser-type-launch ).
2022-03-30 16:00:08 +00:00
2022-04-06 05:56:08 +00:00
#### `runner`
2022-03-30 16:00:08 +00:00
2022-04-16 13:53:36 +00:00
Specify the runner for the test suite. Currently, [Vitest ](https://vitest.dev/ ) is recommended.
2022-03-30 16:00:08 +00:00
* Type: `'vitest' | 'jest'`
* Default: `'vitest'`
## APIs
2022-08-13 07:27:04 +00:00
### APIs for Rendering Testing
2022-03-30 16:00:08 +00:00
2022-04-06 05:56:08 +00:00
#### `$fetch(url)`
2022-03-30 16:00:08 +00:00
Get the HTML of a server-rendered page.
```ts
import { $fetch } from '@nuxt/test-utils'
const html = await $fetch('/')
```
2022-04-06 05:56:08 +00:00
#### `fetch(url)`
2022-03-30 16:00:08 +00:00
Get the response of a server-rendered page.
```ts
import { fetch } from '@nuxt/test-utils'
const res = await fetch('/')
const { body, headers } = res
```
2022-04-06 05:56:08 +00:00
#### `url(path)`
2022-03-30 16:00:08 +00:00
Get the full URL for a given page (including the port the test server is running on.)
```ts
import { url } from '@nuxt/test-utils'
const pageUrl = url('/page')
// 'http://localhost:6840/page'
```
2022-08-13 07:27:04 +00:00
## Testing in a Browser
2022-03-30 16:00:08 +00:00
::alert{icon=🚧}
We are working on it, stay tuned!
::