Skip to content
On this page

Configuring Vitest

Configuration

vitest will read your root vite.config.ts when it is present to match with the plugins and setup as your Vite app. If you want to have a different configuration for testing or your main app doesn't rely on Vite specifically, you could either:

  • Create vitest.config.ts, which will have the higher priority and will override the configuration from vite.config.ts
  • Pass --config option to CLI, e.g. vitest --config ./path/to/vitest.config.ts
  • Use process.env.VITEST or mode property on defineConfig (will be set to test/benchmark if not overridden) to conditionally apply different configuration in vite.config.ts

To configure vitest itself, add test property in your Vite config. You'll also need to add a reference to Vitest types using a triple slash command at the top of your config file, if you are importing defineConfig from vite itself.

using defineConfig from vite you should follow this:

ts
/// <reference types="vitest" />
import { defineConfig } from 'vite'

export default defineConfig({
  test: {
    // ...
  },
})
/// <reference types="vitest" />
import { defineConfig } from 'vite'

export default defineConfig({
  test: {
    // ...
  },
})

using defineConfig from vitest/config you should follow this:

ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    // ...
  },
})
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    // ...
  },
})

You can retrieve Vitest's default options to expand them if needed:

ts
import { configDefaults, defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    exclude: [...configDefaults.exclude, 'packages/template/*'],
  },
})
import { configDefaults, defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    exclude: [...configDefaults.exclude, 'packages/template/*'],
  },
})

When using a separate vitest.config.js, you can also extend Vite's options from another config file if needed:

ts
import { defineConfig, mergeConfig } from 'vitest/config'
import viteConfig from './vite.config'

export default mergeConfig(viteConfig, defineConfig({
  test: {
    exclude: ['packages/template/*'],
  },
}))
import { defineConfig, mergeConfig } from 'vitest/config'
import viteConfig from './vite.config'

export default mergeConfig(viteConfig, defineConfig({
  test: {
    exclude: ['packages/template/*'],
  },
}))

WARNING

mergeConfig helper is availabe in Vitest since v0.30.0. You can import it from vite directly, if you use lower version.

Options

TIP

In addition to the following options, you can also use any configuration option from Vite. For example, define to define global variables, or resolve.alias to define aliases.

TIP

All configuration options that are not supported inside a workspace project config have * sign next to them.

include

  • Type: string[]
  • Default: ['**/*.{test,spec}.?(c|m)[jt]s?(x)']

Files to include in the test run, using glob pattern.

exclude

  • Type: string[]
  • Default: ['**/node_modules/**', '**/dist/**', '**/cypress/**', '**/.{idea,git,cache,output,temp}/**', '**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*']

Files to exclude from the test run, using glob pattern.

includeSource

  • Type: string[]
  • Default: []

Include globs for in-source test files.

When defined, Vitest will run all matched files with import.meta.vitest inside.

deps

  • Type: { external?, inline?, ... }

Handling for dependencies resolution.

deps.experimentalOptimizer

Enable dependency optimization. If you have a lot of tests, this might improve their performance.

When Vitest encounters the external library listed in include, it will be bundled into a single file using esbuild and imported as a whole module. This is good for several reasons:

  • Importing packages with a lot of imports is expensive. By bundling them into one file we can save a lot of time
  • Importing UI libraries is expensive because they are not meant to run inside Node.js
  • Your alias configuration is now respected inside bundled packages
  • Code in your tests is running closer to how it's running in the browser

Be aware that only packages in deps.experimentalOptimizer?.[mode].include option are bundled (some plugins populate this automatically, like Svelte). You can read more about available options in Vite docs. By default, Vitest uses experimentalOptimizer.web for jsdom and happy-dom environments, and experimentalOptimizer.ssr for node and edge environments, but it is configurable by transformMode.

This options also inherits your optimizeDeps configuration (for web Vitest will extend optimizeDeps, for ssr - ssr.optimizeDeps). If you redefine include/exclude option in deps.experimentalOptimizer it will extend your optimizeDeps when running tests. Vitest automatically removes the same options from include, if they are listed in exclude.

TIP

You will not be able to edit your node_modules code for debugging, since the code is actually located in your cacheDir or test.cache.dir directory. If you want to debug with console.log statements, edit it directly or force rebundling with deps.experimentalOptimizer?.[mode].force option.

deps.external

  • Type: (string | RegExp)[]
  • Default: ['**/node_modules/**']

Externalize means that Vite will bypass the package to native Node. Externalized dependencies will not be applied Vite's transformers and resolvers, so they do not support HMR on reload. Typically, packages under node_modules are externalized.

deps.inline

  • Type: (string | RegExp)[] | true
  • Default: []

Vite will process inlined modules. This could be helpful to handle packages that ship .js in ESM format (that Node can't handle).

If true, every dependency will be inlined. All dependencies, specified in ssr.noExternal will be inlined by default.

deps.fallbackCJS

  • Type boolean
  • Default: false

When a dependency is a valid ESM package, try to guess the cjs version based on the path. This might be helpful, if a dependency has the wrong ESM file.

This might potentially cause some misalignment if a package has different logic in ESM and CJS mode.

deps.registerNodeLoader *

  • Type: boolean
  • Default: false

Use experimental Node loader to resolve imports inside externalized files, using Vite resolve algorithm.

If disabled, your alias and <plugin>.resolveId won't affect imports inside externalized packages (by default, node_modules).

deps.interopDefault

  • Type: boolean
  • Default: true

Interpret CJS module's default as named exports. Some dependencies only bundle CJS modules and don't use named exports that Node.js can statically analyze when a package is imported using import syntax instead of require. When importing such dependencies in Node environment using named exports, you will see this error:

import { read } from 'fs-jetpack';
         ^^^^
SyntaxError: Named export 'read' not found. The requested module 'fs-jetpack' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export.
import { read } from 'fs-jetpack';
         ^^^^
SyntaxError: Named export 'read' not found. The requested module 'fs-jetpack' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export.

Vitest doesn't do static analysis, and cannot fail before your running code, so you will most likely see this error when running tests, if this feature is disabled:

TypeError: createAsyncThunk is not a function
TypeError: default is not a function
TypeError: createAsyncThunk is not a function
TypeError: default is not a function

By default, Vitest assumes you are using a bundler to bypass this and will not fail, but you can disable this behaviour manually, if you code is not processed.

deps.moduleDirectories

  • Type: string[]
  • Default: ['node_modules']

A list of directories that should be treated as module directories. This config option affects the behavior of vi.mock: when no factory is provided and the path of what you are mocking matches one of the moduleDirectories values, Vitest will try to resolve the mock by looking for a __mocks__ folder in the root of the project.

This option will also affect if a file should be treated as a module when externalizing dependencies. By default, Vitest imports external modules with native Node.js bypassing Vite transformation step.

Setting this option will override the default, if you wish to still search node_modules for packages include it along with any other options:

ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    deps: {
      moduleDirectories: ['node_modules', path.resolve('../../packages')],
    }
  },
})
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    deps: {
      moduleDirectories: ['node_modules', path.resolve('../../packages')],
    }
  },
})

runner

  • Type: VitestRunnerConstructor
  • Default: node, when running tests, or benchmark, when running benchmarks

Path to a custom test runner. This is an advanced feature and should be used with custom library runners. You can read more about it in the documentation.

benchmark

  • Type: { include?, exclude?, ... }

Options used when running vitest bench.

benchmark.include

  • Type: string[]
  • Default: ['**/*.{bench,benchmark}.?(c|m)[jt]s?(x)']

Include globs for benchmark test files

benchmark.exclude

  • Type: string[]
  • Default: ['node_modules', 'dist', '.idea', '.git', '.cache']

Exclude globs for benchmark test files

benchmark.includeSource

  • Type: string[]
  • Default: []

Include globs for in-source benchmark test files. This option is similar to includeSource.

When defined, Vitest will run all matched files with import.meta.vitest inside.

benchmark.reporters

  • Type: Arrayable<BenchmarkBuiltinReporters | Reporter>
  • Default: 'default'

Custom reporter for output. Can contain one or more built-in report names, reporter instances, and/or paths to custom reporters.

benchmark.outputFile

  • Type: string | Record<string, string>

Write benchmark results to a file when the --reporter=json option is also specified. By providing an object instead of a string you can define individual outputs when using multiple reporters.

To provide object via CLI command, use the following syntax: --outputFile.json=./path --outputFile.junit=./other-path.

alias

  • Type: Record<string, string> | Array<{ find: string | RegExp, replacement: string, customResolver?: ResolverFunction | ResolverObject }>

Define custom aliases when running inside tests. They will be merged with aliases from resolve.alias.

globals

  • Type: boolean
  • Default: false
  • CLI: --globals, --globals=false

By default, vitest does not provide global APIs for explicitness. If you prefer to use the APIs globally like Jest, you can pass the --globals option to CLI or add globals: true in the config.

ts
// vite.config.ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    globals: true,
  },
})
// vite.config.ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    globals: true,
  },
})

To get TypeScript working with the global APIs, add vitest/globals to the types field in your tsconfig.json

json
// tsconfig.json
{
  "compilerOptions": {
    "types": ["vitest/globals"]
  }
}
// tsconfig.json
{
  "compilerOptions": {
    "types": ["vitest/globals"]
  }
}

If you are already using unplugin-auto-import in your project, you can also use it directly for auto importing those APIs.

ts
// vite.config.ts
import { defineConfig } from 'vitest/config'
import AutoImport from 'unplugin-auto-import/vite'

export default defineConfig({
  plugins: [
    AutoImport({
      imports: ['vitest'],
      dts: true, // generate TypeScript declaration
    }),
  ],
})
// vite.config.ts
import { defineConfig } from 'vitest/config'
import AutoImport from 'unplugin-auto-import/vite'

export default defineConfig({
  plugins: [
    AutoImport({
      imports: ['vitest'],
      dts: true, // generate TypeScript declaration
    }),
  ],
})

environment

  • Type: 'node' | 'jsdom' | 'happy-dom' | 'edge-runtime' | string
  • Default: 'node'
  • CLI: --environment=<env>

The environment that will be used for testing. The default environment in Vitest is a Node.js environment. If you are building a web application, you can use browser-like environment through either jsdom or happy-dom instead. If you are building edge functions, you can use edge-runtime environment

By adding a @vitest-environment docblock or comment at the top of the file, you can specify another environment to be used for all tests in that file:

Docblock style:

js
/**
 * @vitest-environment jsdom
 */

test('use jsdom in this test file', () => {
  const element = document.createElement('div')
  expect(element).not.toBeNull()
})
/**
 * @vitest-environment jsdom
 */

test('use jsdom in this test file', () => {
  const element = document.createElement('div')
  expect(element).not.toBeNull()
})

Comment style:

js
// @vitest-environment happy-dom

test('use happy-dom in this test file', () => {
  const element = document.createElement('div')
  expect(element).not.toBeNull()
})
// @vitest-environment happy-dom

test('use happy-dom in this test file', () => {
  const element = document.createElement('div')
  expect(element).not.toBeNull()
})

For compatibility with Jest, there is also a @jest-environment:

js
/**
 * @jest-environment jsdom
 */

test('use jsdom in this test file', () => {
  const element = document.createElement('div')
  expect(element).not.toBeNull()
})
/**
 * @jest-environment jsdom
 */

test('use jsdom in this test file', () => {
  const element = document.createElement('div')
  expect(element).not.toBeNull()
})

If you are running Vitest with --threads=false flag, your tests will be run in this order: node, jsdom, happy-dom, edge-runtime, custom environments. Meaning, that every test with the same environment is grouped together, but is still running sequentially.

Starting from 0.23.0, you can also define custom environment. When non-builtin environment is used, Vitest will try to load package vitest-environment-${name}. That package should export an object with the shape of Environment:

ts
import type { Environment } from 'vitest'

export default <Environment>{
  name: 'custom',
  setup() {
    // custom setup
    return {
      teardown() {
        // called after all tests with this env have been run
      }
    }
  }
}
import type { Environment } from 'vitest'

export default <Environment>{
  name: 'custom',
  setup() {
    // custom setup
    return {
      teardown() {
        // called after all tests with this env have been run
      }
    }
  }
}

Vitest also exposes builtinEnvironments through vitest/environments entry, in case you just want to extend it. You can read more about extending environments in our guide.

environmentOptions

  • Type: Record<'jsdom' | string, unknown>
  • Default: {}

These options are passed down to setup method of current environment. By default, you can configure only JSDOM options, if you are using it as your test environment.

environmentMatchGlobs

  • Type: [string, EnvironmentName][]
  • Default: []

Automatically assign environment based on globs. The first match will be used.

For example:

ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    environmentMatchGlobs: [
      // all tests in tests/dom will run in jsdom
      ['tests/dom/**', 'jsdom'],
      // all tests in tests/ with .edge.test.ts will run in edge-runtime
      ['**\/*.edge.test.ts', 'edge-runtime'],
      // ...
    ]
  }
})
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    environmentMatchGlobs: [
      // all tests in tests/dom will run in jsdom
      ['tests/dom/**', 'jsdom'],
      // all tests in tests/ with .edge.test.ts will run in edge-runtime
      ['**\/*.edge.test.ts', 'edge-runtime'],
      // ...
    ]
  }
})

poolMatchGlobs

  • Type: [string, 'browser' | 'threads' | 'child_process'][]
  • Default: []
  • Version: Since Vitest 0.29.4

Automatically assign pool in which tests will run based on globs. The first match will be used.

For example:

ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    poolMatchGlobs: [
      // all tests in "worker-specific" directory will run inside a worker as if you enabled `--threads` for them,
      ['**/tests/worker-specific/**', 'threads'],
      // run all tests in "browser" directory in an actual browser
      ['**/tests/browser/**', 'browser'],
      // all other tests will run based on "browser.enabled" and "threads" options, if you didn't specify other globs
      // ...
    ]
  }
})
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    poolMatchGlobs: [
      // all tests in "worker-specific" directory will run inside a worker as if you enabled `--threads` for them,
      ['**/tests/worker-specific/**', 'threads'],
      // run all tests in "browser" directory in an actual browser
      ['**/tests/browser/**', 'browser'],
      // all other tests will run based on "browser.enabled" and "threads" options, if you didn't specify other globs
      // ...
    ]
  }
})

update *

  • Type: boolean
  • Default: false
  • CLI: -u, --update, --update=false

Update snapshot files. This will update all changed snapshots and delete obsolete ones.

watch *

  • Type: boolean
  • Default: true
  • CLI: -w, --watch, --watch=false

Enable watch mode

root

  • Type: string
  • CLI: -r <path>, --root=<path>

Project root

reporters *

  • Type: Reporter | Reporter[]
  • Default: 'default'
  • CLI: --reporter=<name>, --reporter=<name1> --reporter=<name2>

Custom reporters for output. Reporters can be a Reporter instance or a string to select built in reporters:

  • 'default' - collapse suites when they pass
  • 'basic' - give a reporter like default reporter in ci
  • 'verbose' - keep the full task tree visible
  • 'dot' - show each task as a single dot
  • 'junit' - JUnit XML reporter (you can configure testsuites tag name with VITEST_JUNIT_SUITE_NAME environmental variable)
  • 'json' - give a simple JSON summary
  • 'html' - outputs HTML report based on @vitest/ui
  • 'hanging-process' - displays a list of hanging processes, if Vitest cannot exit process safely. This might be a heavy operation, enable it only if Vitest consistently cannot exit process
  • path of a custom reporter (e.g. './path/to/reporter.ts', '@scope/reporter')

outputFile *

  • Type: string | Record<string, string>
  • CLI: --outputFile=<path>, --outputFile.json=./path

Write test results to a file when the --reporter=json, --reporter=html or --reporter=junit option is also specified. By providing an object instead of a string you can define individual outputs when using multiple reporters.

threads

  • Type: boolean
  • Default: true
  • CLI: --threads, --threads=false

Enable multi-threading using tinypool (a lightweight fork of Piscina). Prior to Vitest 0.29.0, Vitest was still running tests inside worker thread, even if this option was disabled. Since 0.29.0, if this option is disabled, Vitest uses child_process to spawn a process to run tests inside, meaning you can use process.chdir and other API that was not available inside workers. If you want to revert to the previous behaviour, use --single-thread option instead.

Disabling this option also disables module isolation, meaning all tests with the same environment are running inside a single child process.

singleThread

  • Type: boolean
  • Default: false
  • Version: Since Vitest 0.29.0

Run all tests with the same environment inside a single worker thread. This will disable built-in module isolation (your source code or inlined code will still be reevaluated for each test), but can improve test performance. Before Vitest 0.29.0 this was equivalent to using --no-threads.

WARNING

Even though this option will force tests to run one after another, this option is different from Jest's --runInBand. Vitest uses workers not only for running tests in parallel, but also to provide isolation. By disabling this option, your tests will run sequentially, but in the same global context, so you must provide isolation yourself.

This might cause all sorts of issues, if you are relying on global state (frontend frameworks usually do) or your code relies on environment to be defined separately for each test. But can be a speed boost for your tests (up to 3 times faster), that don't necessarily rely on global state or can easily bypass that.

maxThreads *

  • Type: number
  • Default: available CPUs

Maximum number of threads. You can also use VITEST_MAX_THREADS environment variable.

minThreads *

  • Type: number
  • Default: available CPUs

Minimum number of threads. You can also use VITEST_MIN_THREADS environment variable.

useAtomics *

  • Type: boolean
  • Default: false
  • Version: Since Vitest 0.28.3

Use Atomics to synchronize threads.

This can improve performance in some cases, but might cause segfault in older Node versions.

testTimeout

  • Type: number
  • Default: 5000
  • CLI: --test-timeout=5000

Default timeout of a test in milliseconds

hookTimeout

  • Type: number
  • Default: 10000

Default timeout of a hook in milliseconds

teardownTimeout *

  • Type: number
  • Default: 10000

Default timeout to wait for close when Vitest shuts down, in milliseconds

silent *

  • Type: boolean
  • Default: false
  • CLI: --silent, --silent=false

Silent console output from tests

setupFiles

  • Type: string | string[]

Path to setup files. They will be run before each test file.

INFO

Changing setup files will trigger rerun of all tests.

You can use process.env.VITEST_POOL_ID (integer-like string) inside to distinguish between threads (will always be '1', if run with threads: false).

TIP

Note, that if you are running --threads=false, this setup file will be run in the same global scope multiple times. Meaning, that you are accessing the same global object before each test, so make sure you are not doing the same thing more than you need.

For example, you may rely on a global variable:

ts
import { config } from '@some-testing-lib'

if (!globalThis.defined) {
  config.plugins = [myCoolPlugin]
  computeHeavyThing()
  globalThis.defined = true
}

// hooks are reset before each suite
afterEach(() => {
  cleanup()
})

globalThis.resetBeforeEachTest = true
import { config } from '@some-testing-lib'

if (!globalThis.defined) {
  config.plugins = [myCoolPlugin]
  computeHeavyThing()
  globalThis.defined = true
}

// hooks are reset before each suite
afterEach(() => {
  cleanup()
})

globalThis.resetBeforeEachTest = true

globalSetup

  • Type: string | string[]

Path to global setup files, relative to project root

A global setup file can either export named functions setup and teardown or a default function that returns a teardown function (example).

INFO

Multiple globalSetup files are possible. setup and teardown are executed sequentially with teardown in reverse order.

WARNING

Beware that the global setup is run in a different global scope, so your tests don't have access to variables defined here.

watchExclude *

  • Type: string[]
  • Default: ['**/node_modules/**', '**/dist/**']

Glob pattern of file paths to be ignored from triggering watch rerun.

forceRerunTriggers *

  • Type: string[]
  • Default: ['**/package.json/**', '**/vitest.config.*/**', '**/vite.config.*/**']

Glob pattern of file paths that will trigger the whole suite rerun. When paired with the --changed argument will run the whole test suite if the trigger is found in the git diff.

Useful if you are testing calling CLI commands, because Vite cannot construct a module graph:

ts
test('execute a script', async () => {
  // Vitest cannot rerun this test, if content of `dist/index.js` changes
  await execa('node', ['dist/index.js'])
})
test('execute a script', async () => {
  // Vitest cannot rerun this test, if content of `dist/index.js` changes
  await execa('node', ['dist/index.js'])
})

TIP

Make sure that your files are not excluded by watchExclude.

isolate

  • Type: boolean
  • Default: true
  • CLI: --isolate, --isolate=false

Isolate environment for each test file. Does not work if you disable --threads.

coverage *

You can use v8, istanbul or a custom coverage solution for coverage collection.

You can provide coverage options to CLI with dot notation:

sh
npx vitest --coverage.enabled --coverage.provider=istanbul --coverage.all
npx vitest --coverage.enabled --coverage.provider=istanbul --coverage.all

WARNING

If you are using coverage options with dot notation, don't forget to specify --coverage.enabled. Do not provide a single --coverage option in that case.

coverage.provider

  • Type: 'v8' | 'istanbul' | 'custom'
  • Default: 'v8'
  • CLI: --coverage.provider=<provider>

Use provider to select the tool for coverage collection.

coverage.enabled

  • Type: boolean
  • Default: false
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.enabled, --coverage.enabled=false

Enables coverage collection. Can be overridden using --coverage CLI option.

coverage.include

  • Type: string[]
  • Default: ['**']
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.include=<path>, --coverage.include=<path1> --coverage.include=<path2>

List of files included in coverage as glob patterns

coverage.extension

  • Type: string | string[]
  • Default: ['.js', '.cjs', '.mjs', '.ts', '.mts', '.cts', '.tsx', '.jsx', '.vue', '.svelte']
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.extension=<extension>, --coverage.extension=<extension1> --coverage.extension=<extension2>

coverage.exclude

  • Type: string[]
  • Default:
js
[
  'coverage/**',
  'dist/**',
  'packages/*/test?(s)/**',
  '**/*.d.ts',
  'cypress/**',
  'test?(s)/**',
  'test?(-*).?(c|m)[jt]s?(x)',
  '**/*{.,-}{test,spec}.?(c|m)[jt]s?(x)',
  '**/__tests__/**',
  '**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*',
  '**/.{eslint,mocha,prettier}rc.{?(c|m)js,yml}',
]
[
  'coverage/**',
  'dist/**',
  'packages/*/test?(s)/**',
  '**/*.d.ts',
  'cypress/**',
  'test?(s)/**',
  'test?(-*).?(c|m)[jt]s?(x)',
  '**/*{.,-}{test,spec}.?(c|m)[jt]s?(x)',
  '**/__tests__/**',
  '**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*',
  '**/.{eslint,mocha,prettier}rc.{?(c|m)js,yml}',
]
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.exclude=<path>, --coverage.exclude=<path1> --coverage.exclude=<path2>

List of files excluded from coverage as glob patterns.

coverage.all

  • Type: boolean
  • Default: false
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.all, --coverage.all=false

Whether to include all files, including the untested ones into report.

coverage.clean

  • Type: boolean
  • Default: true
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.clean, --coverage.clean=false

Clean coverage results before running tests

coverage.cleanOnRerun

  • Type: boolean
  • Default: true
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.cleanOnRerun, --coverage.cleanOnRerun=false

Clean coverage report on watch rerun

coverage.reportsDirectory

  • Type: string
  • Default: './coverage'
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.reportsDirectory=<path>

Directory to write coverage report to.

coverage.reporter

  • Type: string | string[] | [string, {}][]
  • Default: ['text', 'html', 'clover', 'json']
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.reporter=<reporter>, --coverage.reporter=<reporter1> --coverage.reporter=<reporter2>

Coverage reporters to use. See istanbul documentation for detailed list of all reporters. See @types/istanbul-reporter for details about reporter specific options.

The reporter has three different types:

  • A single reporter: { reporter: 'html' }
  • Multiple reporters without options: { reporter: ['html', 'json'] }
  • A single or multiple reporters with reporter options:
    ts
    {
      reporter: [
        ['lcov', { 'projectRoot': './src' }],
        ['json', { 'file': 'coverage.json' }],
        ['text']
      ]
    }
    {
      reporter: [
        ['lcov', { 'projectRoot': './src' }],
        ['json', { 'file': 'coverage.json' }],
        ['text']
      ]
    }

Since Vitest 0.31.0, you can check your coverage report in Vitest UI: check Vitest UI Coverage for more details.

coverage.reportOnFailure

  • Type: boolean
  • Default: false (since Vitest 0.34.0)
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.reportOnFailure, --coverage.reportOnFailure=false
  • Version: Since Vitest 0.31.2

Generate coverage report even when tests fail.

coverage.skipFull

  • Type: boolean
  • Default: false
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.skipFull, --coverage.skipFull=false

Do not show files with 100% statement, branch, and function coverage.

coverage.perFile

  • Type: boolean
  • Default: false
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.perFile, --coverage.perFile=false

Check thresholds per file. See lines, functions, branches and statements for the actual thresholds.

coverage.thresholdAutoUpdate

  • Type: boolean
  • Default: false
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.thresholdAutoUpdate=<boolean>

Update threshold values lines, functions, branches and statements to configuration file when current coverage is above the configured thresholds. This option helps to maintain thresholds when coverage is improved.

coverage.lines

  • Type: number
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.lines=<number>

Threshold for lines. See istanbul documentation for more information.

coverage.functions

  • Type: number
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.functions=<number>

Threshold for functions. See istanbul documentation for more information.

coverage.branches

  • Type: number
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.branches=<number>

Threshold for branches. See istanbul documentation for more information.

coverage.statements

  • Type: number
  • Available for providers: 'v8' | 'istanbul'
  • CLI: --coverage.statements=<number>

Threshold for statements. See istanbul documentation for more information.

coverage.100

  • Type: boolean
  • Default: false
  • Available for providers: 'v8'
  • CLI: --coverage.100, --coverage.100=false

Shortcut for --check-coverage --lines 100 --functions 100 --branches 100 --statements 100.

coverage.ignoreClassMethods

  • Type: string[]
  • Default: []
  • Available for providers: 'istanbul'
  • CLI: --coverage.ignoreClassMethods=<method>

Set to array of class method names to ignore for coverage. See istanbul documentation for more information.

coverage.watermarks

  • Type:
ts
{
  statements?: [number, number],
  functions?: [number, number],
  branches?: [number, number],
  lines?: [number, number]
}
{
  statements?: [number, number],
  functions?: [number, number],
  branches?: [number, number],
  lines?: [number, number]
}
  • Default:
ts
{
  statements: [50, 80],
  functions: [50, 80],
  branches: [50, 80],
  lines: [50, 80]
}
{
  statements: [50, 80],
  functions: [50, 80],
  branches: [50, 80],
  lines: [50, 80]
}
  • Available for providers: 'v8' | 'istanbul'

Watermarks for statements, lines, branches and functions. See istanbul documentation for more information.

coverage.customProviderModule

  • Type: string
  • Available for providers: 'custom'
  • CLI: --coverage.customProviderModule=<path or module name>

Specifies the module name or path for the custom coverage provider module. See Guide - Custom Coverage Provider for more information.

testNamePattern *

  • Type string | RegExp
  • CLI: -t <pattern>, --testNamePattern=<pattern>, --test-name-pattern=<pattern>

Run tests with full names matching the pattern. If you add OnlyRunThis to this property, tests not containing the word OnlyRunThis in the test name will be skipped.

js
import { expect, test } from 'vitest'

// run
test('OnlyRunThis', () => {
  expect(true).toBe(true)
})

// skipped
test('doNotRun', () => {
  expect(true).toBe(true)
})
import { expect, test } from 'vitest'

// run
test('OnlyRunThis', () => {
  expect(true).toBe(true)
})

// skipped
test('doNotRun', () => {
  expect(true).toBe(true)
})

open *

  • Type: boolean
  • Default: false
  • CLI: --open, --open=false

Open Vitest UI (WIP)

api

  • Type: boolean | number
  • Default: false
  • CLI: --api, --api.port, --api.host, --api.strictPort

Listen to port and serve API. When set to true, the default port is 51204

browser

  • Type: { enabled?, name?, provider?, headless?, api?, slowHijackESM? }
  • Default: { enabled: false, headless: process.env.CI, api: 63315 }
  • Version: Since Vitest 0.29.4
  • CLI: --browser, --browser=<name>, --browser.name=chrome --browser.headless

Run Vitest tests in a browser. We use WebdriverIO for running tests by default, but it can be configured with browser.provider option.

NOTE

Read more about testing in a real browser in the guide page.

WARNING

This is an experimental feature. Breaking changes might not follow semver, please pin Vitest's version when using it.

browser.enabled

  • Type: boolean
  • Default: false
  • CLI: --browser, --browser.enabled=false

Run all tests inside a browser by default. Can be overriden with poolMatchGlobs option.

browser.name

  • Type: string
  • CLI: --browser=safari

Run all tests in a specific browser. Possible options in different providers:

  • webdriverio: firefox, chrome, edge, safari
  • playwright: firefox, webkit, chromium
  • custom: any string that will be passed to the provider

browser.headless

  • Type: boolean
  • Default: process.env.CI
  • CLI: --browser.headless, --brower.headless=false

Run the browser in a headless mode. If you are running Vitest in CI, it will be enabled by default.

browser.api

  • Type: number | { port?, strictPort?, host? }
  • Default: 63315
  • CLI: --browser.api=63315, --browser.api.port=1234, --browser.api.host=example.com

Configure options for Vite server that serves code in the browser. Does not affect test.api option.

browser.provider

  • Type: 'webdriverio' | 'playwright' | string
  • Default: 'webdriverio'
  • CLI: --browser.provider=playwright

Path to a provider that will be used when running browser tests. Vitest provides two providers which are webdriverio (default) and playwright. Custom providers should be exported using default export and have this shape:

ts
export interface BrowserProvider {
  name: string
  getSupportedBrowsers(): readonly string[]
  initialize(ctx: Vitest, options: { browser: string }): Awaitable<void>
  openPage(url: string): Awaitable<void>
  close(): Awaitable<void>
}
export interface BrowserProvider {
  name: string
  getSupportedBrowsers(): readonly string[]
  initialize(ctx: Vitest, options: { browser: string }): Awaitable<void>
  openPage(url: string): Awaitable<void>
  close(): Awaitable<void>
}

WARNING

This is an advanced API for library authors. If you just need to run tests in a browser, use the browser option.

browser.slowHijackESM

  • Type: boolean
  • Default: true
  • Version: Since Vitest 0.31.0

When running tests in Node.js Vitest can use its own module resolution to easily mock modules with vi.mock syntax. However it's not so easy to replicate ES module resolution in browser, so we need to transform your source files before browser can consume it.

This option has no effect on tests running inside Node.js.

This options is enabled by default when running in the browser. If you don't rely on spying on ES modules with vi.spyOn and don't use vi.mock, you can disable this to get a slight boost to performance.

clearMocks

  • Type: boolean
  • Default: false

Will call .mockClear() on all spies before each test. This will clear mock history, but not reset its implementation to the default one.

mockReset

  • Type: boolean
  • Default: false

Will call .mockReset() on all spies before each test. This will clear mock history and reset its implementation to an empty function (will return undefined).

restoreMocks

  • Type: boolean
  • Default: false

Will call .mockRestore() on all spies before each test. This will clear mock history and reset its implementation to the original one.

unstubEnvs

  • Type: boolean
  • Default: false
  • Version: Since Vitest 0.26.0

Will call vi.unstubAllEnvs before each test.

unstubGlobals

  • Type: boolean
  • Default: false
  • Version: Since Vitest 0.26.0

Will call vi.unstubAllGlobals before each test.

testTransformMode

  • Type: { web?, ssr? }
  • Version: Since Vitest 0.34.0

Determine the transform method for all modules inported inside a test that matches the glob pattern. By default, relies on the environment. For example, tests with JSDOM environment will process all files with ssr: false flag and tests with Node environment process all modules with ssr: true.

testTransformMode.ssr

  • Type: string[]
  • Default: []

Use SSR transform pipeline for all modules inside specified tests.
Vite plugins will receive ssr: true flag when processing those files.

testTransformMode.web

  • Type: string[]
  • Default: []

First do a normal transform pipeline (targeting browser), then do a SSR rewrite to run the code in Node.
Vite plugins will receive ssr: false flag when processing those files.

snapshotFormat *

  • Type: PrettyFormatOptions

Format options for snapshot testing. These options are passed down to pretty-format.

resolveSnapshotPath *

  • Type: (testPath: string, snapExtension: string) => string
  • Default: stores snapshot files in __snapshots__ directory

Overrides default snapshot path. For example, to store snapshots next to test files:

ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    resolveSnapshotPath: (testPath, snapExtension) => testPath + snapExtension,
  },
})
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    resolveSnapshotPath: (testPath, snapExtension) => testPath + snapExtension,
  },
})

allowOnly

  • Type: boolean
  • Default: false
  • CLI: --allowOnly, --allowOnly=false

Allow tests and suites that are marked as only.

dangerouslyIgnoreUnhandledErrors *

  • Type: boolean
  • Default: false
  • CLI: --dangerouslyIgnoreUnhandledErrors --dangerouslyIgnoreUnhandledErrors=false

Ignore any unhandled errors that occur.

passWithNoTests *

  • Type: boolean
  • Default: false
  • CLI: --passWithNoTests, --passWithNoTests=false

Vitest will not fail, if no tests will be found.

logHeapUsage

  • Type: boolean
  • Default: false
  • CLI: --logHeapUsage, --logHeapUsage=false

Show heap usage after each test. Useful for debugging memory leaks.

css

  • Type: boolean | { include?, exclude?, modules? }

Configure if CSS should be processed. When excluded, CSS files will be replaced with empty strings to bypass the subsequent processing. CSS Modules will return a proxy to not affect runtime.

css.include

  • Type: RegExp | RegExp[]
  • Default: []

RegExp pattern for files that should return actual CSS and will be processed by Vite pipeline.

TIP

To process all CSS files, use /.+/.

css.exclude

  • Type: RegExp | RegExp[]
  • Default: []

RegExp pattern for files that will return an empty CSS file.

css.modules

  • Type: { classNameStrategy? }
  • Default: {}

css.modules.classNameStrategy

  • Type: 'stable' | 'scoped' | 'non-scoped'
  • Default: 'stable'

If you decide to process CSS files, you can configure if class names inside CSS modules should be scoped. You can choose one of the options:

  • stable: class names will be generated as _${name}_${hashedFilename}, which means that generated class will stay the same, if CSS content is changed, but will change, if the name of the file is modified, or file is moved to another folder. This setting is useful, if you use snapshot feature.
  • scoped: class names will be generated as usual, respecting css.modules.generateScopeName method, if you have one and CSS processing is enabled. By default, filename will be generated as _${name}_${hash}, where hash includes filename and content of the file.
  • non-scoped: class names will not be hashed.

WARNING

By default, Vitest exports a proxy, bypassing CSS Modules processing. If you rely on CSS properties on your classes, you have to enable CSS processing using include option.

maxConcurrency

  • Type: number
  • Default: 5

A number of tests that are allowed to run at the same time marked with test.concurrent.

Test above this limit will be queued to run when available slot appears.

cache *

  • Type: false | { dir? }

Options to configure Vitest cache policy. At the moment Vitest stores cache for test results to run the longer and failed tests first.

cache.dir

  • Type: string
  • Default: node_modules/.vitest

Path to cache directory.

sequence

  • Type: { sequencer?, shuffle?, seed?, hooks?, setupFiles? }

Options for how tests should be sorted.

You can provide sequence options to CLI with dot notation:

sh
npx vitest --sequence.shuffle --sequence.seed=1000
npx vitest --sequence.shuffle --sequence.seed=1000

sequence.sequencer *

  • Type: TestSequencerConstructor
  • Default: BaseSequencer

A custom class that defines methods for sharding and sorting. You can extend BaseSequencer from vitest/node, if you only need to redefine one of the sort and shard methods, but both should exist.

Sharding is happening before sorting, and only if --shard option is provided.

sequence.shuffle

  • Type: boolean
  • Default: false
  • CLI: --sequence.shuffle, --sequence.shuffle=false

If you want tests to run randomly, you can enable it with this option, or CLI argument --sequence.shuffle.

Vitest usually uses cache to sort tests, so long running tests start earlier - this makes tests run faster. If your tests will run in random order you will lose this performance improvement, but it may be useful to track tests that accidentally depend on another run previously.

sequence.concurrent

  • Type: boolean
  • Default: false
  • CLI: --sequence.concurrent, --sequence.concurrent=false
  • Version: Since Vitest 0.32.2

If you want tests to run in parallel, you can enable it with this option, or CLI argument --sequence.concurrent.

sequence.seed *

  • Type: number
  • Default: Date.now()
  • CLI: --sequence.seed=1000

Sets the randomization seed, if tests are running in random order.

sequence.hooks

  • Type: 'stack' | 'list' | 'parallel'
  • Default: 'parallel'
  • CLI: --sequence.hooks=<value>

Changes the order in which hooks are executed.

  • stack will order "after" hooks in reverse order, "before" hooks will run in the order they were defined
  • list will order all hooks in the order they are defined
  • parallel will run hooks in a single group in parallel (hooks in parent suites will still run before the current suite's hooks)

sequence.setupFiles

  • Type: 'list' | 'parallel'
  • Default: 'parallel'
  • CLI: --sequence.setupFiles=<value>
  • Version: Since Vitest 0.29.3

Changes the order in which setup files are executed.

  • list will run setup files in the order they are defined
  • parallel will run setup files in parallel

typecheck

Options for configuring typechecking test environment.

typecheck.checker

  • Type: 'tsc' | 'vue-tsc' | string
  • Default: tsc

What tools to use for type checking. Vitest will spawn a process with certain parameters for easier parsing, depending on the type. Checker should implement the same output format as tsc.

You need to have a package installed to use typechecker:

  • tsc requires typescript package
  • vue-tsc requires vue-tsc package

You can also pass down a path to custom binary or command name that produces the same output as tsc --noEmit --pretty false.

typecheck.include

  • Type: string[]
  • Default: ['**/*.{test,spec}-d.?(c|m)[jt]s?(x)']

Glob pattern for files that should be treated as test files

typecheck.exclude

  • Type: string[]
  • Default: ['**/node_modules/**', '**/dist/**', '**/cypress/**', '**/.{idea,git,cache,output,temp}/**']

Glob pattern for files that should not be treated as test files

typecheck.allowJs

  • Type: boolean
  • Default: false

Check JS files that have @ts-check comment. If you have it enabled in tsconfig, this will not overwrite it.

typecheck.ignoreSourceErrors

  • Type: boolean
  • Default: false

Do not fail, if Vitest found errors outside the test files. This will not show you non-test errors at all.

By default, if Vitest finds source error, it will fail test suite.

typecheck.tsconfig

  • Type: string
  • Default: tries to find closest tsconfig.json

Path to custom tsconfig, relative to the project root.

slowTestThreshold *

  • Type: number
  • Default: 300

The number of milliseconds after which a test is considered slow and reported as such in the results.

chaiConfig

  • Type: { includeStack?, showDiff?, truncateThreshold? }
  • Default: { includeStack: false, showDiff: true, truncateThreshold: 40 }
  • Version: Since Vitest 0.30.0

Equivalent to Chai config.

chaiConfig.includeStack

  • Type: boolean
  • Default: false

Influences whether stack trace is included in Assertion error message. Default of false suppresses stack trace in the error message.

chaiConfig.showDiff

  • Type: boolean
  • Default: true

Influences whether or not the showDiff flag should be included in the thrown AssertionErrors. false will always be false; true will be true when the assertion has requested a diff to be shown.

chaiConfig.truncateThreshold

  • Type: number
  • Default: 40

Sets length threshold for actual and expected values in assertion errors. If this threshold is exceeded, for example for large data structures, the value is replaced with something like [ Array(3) ] or { Object (prop1, prop2) }. Set it to 0 if you want to disable truncating altogether.

This config option affects truncating values in test.each titles and inside the assertion error message.

bail

  • Type: number
  • Default: 0
  • CLI: --bail=<value>
  • Version: Since Vitest 0.31.0

Stop test execution when given number of tests have failed.

By default Vitest will run all of your test cases even if some of them fail. This may not be desired for CI builds where you are only interested in 100% successful builds and would like to stop test execution as early as possible when test failures occur. The bail option can be used to speed up CI runs by preventing it from running more tests when failures have occured.

retry

  • Type: number
  • Default: 0
  • CLI: --retry=<value>
  • Version: Since Vitest 0.32.3

Retry the test specific number of times if it fails.

Released under the MIT License.