Nuxt/docs/content/1.docs/1.getting-started/9.testing.md
Sébastien Chopin 90784f79d7
docs: new website design (#9007)
* docs: implement new website theme

* chore: rename dirs

* chore: update build

* lint fix

* chore: update deps

* fix: include node_modules in esbuild step

* chore: update deps

* Update .gitignore

* chore: update theme version

* up

* up

* fix: use svg for illustration

* chore: update to 0.0.12

* chore: force parse5 resolution

* stay with build

* feat: always display first home section

* Update yarn.lock

* chore: update theme

* fix lint

* docs: update home title

* chore: update website theme version

* Update docs/content/0.index.md

Co-authored-by: pooya parsa <pyapar@gmail.com>

* Update docs/content/0.index.md

Co-authored-by: pooya parsa <pyapar@gmail.com>

* up

* chore: bump theme version

* up

* chore: up

* up up and up

* chore: generate

* fix: boolean value

* feat: new images

* update again

* chore: up

* ouep

* chore: up

Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Clément Ollivier <clement.o2p@gmail.com>
Co-authored-by: pooya parsa <pyapar@gmail.com>
2022-11-16 11:04:28 +01:00

3.6 KiB

navigation.icon
uil:check-circle

Testing

How to test your Nuxt application.

::alert{icon=👉} 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. If you are a module author, you can find more specific informations in the Module Author's guide ::

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.

Installation

yarn add --dev @nuxt/test-utils-edge vitest

Setup

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.

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.

Options

Nuxt Configuration

rootDir

Path to a directory with a Nuxt app to be put under test.

  • Type: string
  • Default: '.'

configFile

Name of the configuration file.

  • Type: string
  • Default: 'nuxt.config'

Setup Timings

setupTimeout

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

Features to Enable

server

Whether to launch a server to respond to requests in the test suite.

  • Type: boolean
  • Default: true

build

Whether to run a separate build step.

  • Type: boolean
  • Default: true (false if browser or server is disabled)

browser

Under 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.)

  • Type: boolean
  • Default: false

browserOptions

  • Type: object with the following properties
    • type: The type of browser to launch - either chromium, firefox or webkit
    • launch: object of options that will be passed to playwright when launching the browser. See full API reference.

runner

Specify the runner for the test suite. Currently, Vitest is recommended.

  • Type: 'vitest' | 'jest'
  • Default: 'vitest'

APIs

APIs for Rendering Testing

$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 } = res

url(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'

Testing in a Browser

::alert{icon=🚧} We are working on it, stay tuned! ::