Setup

Setting up your Nuxt test environment

Setting up the test context

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 { setupTest } from '@nuxt/test-utils'

describe('My test', () => {
  setupTest({
    // test context options
  })

  test('my test', () => {
    // ...
  })
})

Behind the scenes, setupTest performs a number of tasks in beforeEach, afterEach and afterAll to setup the Nuxt test environment correctly. It also adds a single Nuxt setup task as an additional test. This means it must be run within the describe block of a test, before any other calls to test or it.

Using test.only or it.only later in the describe block of your test will cause the tests to fail.

Options for setupTest

Paths and Nuxt configuration

configFile

The name of the Nuxt configuration file that will be read to get the configuration for the tests.

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

config

You can override Nuxt options when running tests using this option.

  • Type: NuxtConfig
  • Default: {}

testDir

The parent directory for the test fixture.

  • Type: string
  • Default: '~~/test'

fixture

(For module or library testing) specifies the name of a fixture directory (under testDir) containing a Nuxt app.

  • Type: string
  • Default: 'fixture'

rootDir

The path to the Nuxt application that will be used in the tests.

  • Type: string
  • Default: <testDir>/<fixture>

buildDir

By default, build directories are randomised to allow jest to run tests in parallel without conflict.

  • Type: string
  • Default: <rootDir>/.nuxt/<randomID>

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

waitFor

An additional aount of time (in milliseconds) to wait after setting up the test context before commencing the rest of the test suite.

  • Type: number
  • Default: 0

Features to enable

server

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

  • Type: boolean
  • Default: false

build

Whether to run a separate build step.

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

generate

Whether to run generate pre-rendered HTML files for the application.

  • Type: boolean
  • Default: false

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.