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 { mergeConfig } from 'vite'
import { defineConfig } from 'vitest/config'
import viteConfig from './vite.config'

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

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

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.

include

  • Type: string[]
  • Default: ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']

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}.config.*']

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

deps

  • Type: { external?, inline? }

Handling for dependencies inlining or externalizing

deps.external

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

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 node_modules, using Vite resolve algorithm.

If disabled, your alias and <plugin>.resolveId won't affect imports inside node_modules or deps.external.

deps.interopDefault

  • Type: boolean
  • Default: true

Interpret CJS module's default as named exports.

benchmark

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

Options used when running vitest bench.

benchmark.include

  • Type: string[]
  • Default: ['**/*.{bench,benchmark}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']

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

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'

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 --no-threads 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.

update

  • Type: boolean
  • Default: false

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

watch

  • Type: boolean
  • Default: true

Enable watch mode

root

  • Type: string

Project root

reporters

  • Type: Reporter | Reporter[]
  • Default: 'default'

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

  • 'default' - collapse suites when they pass
  • 'verbose' - keep the full task tree visible
  • 'dot' - show each task as a single dot
  • 'junit' - JUnit XML reporter
  • 'json' - give a simple JSON summary
  • path of a custom reporter (e.g. './path/to/reporter.ts', '@scope/reporter')

outputTruncateLength

  • Type: number
  • Default: 80

Truncate output diff lines up to 80 number of characters. You may wish to tune this, depending on your terminal window width.

outputDiffLines

  • Type: number
  • Default: 15

Limit number of output diff lines up to 15.

outputFile

  • Type: string | Record<string, string>

Write test results to a file when the --reporter=json or --reporter=junit 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.

threads

  • Type: boolean
  • Default: true

Enable multi-threading using tinypool (a lightweight fork of Piscina)

WARNING

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.

testTimeout

  • Type: number
  • Default: 5000

Default timeout of a test in milliseconds

hookTimeout

  • Type: number
  • Default: 10000

Default timeout of a hook in milliseconds

teardownTimeout

  • Type: number
  • Default: 1000

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

silent

  • Type: boolean
  • Default: false

Silent console output from tests

setupFiles

  • Type: string | string[]

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

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 --no-threads, 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

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

coverage

  • Type: CoverageC8Options | CoverageIstanbulOptions
  • Default: undefined

You can use c8 or istanbul for coverage collection.

provider

  • Type: 'c8' | 'istanbul'
  • Default: 'c8'

Use provider to select the tool for coverage collection.

CoverageC8Options

Used when provider: 'c8' is set. Coverage options are passed to c8.

CoverageIstanbulOptions

Used when provider: 'istanbul' is set.

include
  • Type: string[]
  • Default: ['**']

List of files included in coverage as glob patterns

exclude
  • Type: string[]
  • Default: ['coverage/**', 'dist/**', 'packages/*/test{,s}/**', '**/*.d.ts', 'cypress/**', 'test{,s}/**', 'test{,-*}.{js,cjs,mjs,ts,tsx,jsx}', '**/*{.,-}test.{js,cjs,mjs,ts,tsx,jsx}', '**/*{.,-}spec.{js,cjs,mjs,ts,tsx,jsx}', '**/__tests__/**', '**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress}.config.{js,cjs,mjs,ts}', '**/.{eslint,mocha,prettier}rc.{js,cjs,yml}']

List of files excluded from coverage as glob patterns.

skipFull
  • Type: boolean
  • Default: false

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

perFile
  • Type: boolean
  • Default: false

Check thresholds per file.

lines
  • Type: number

Threshold for lines.

functions
  • Type: number

Threshold for functions.

branches
  • Type: number

Threshold for branches.

statements
  • Type: number

Threshold for statements.

ignoreClassMethods
  • Type: string[]
  • Default: []

Set to array of class method names to ignore for 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]
}

Watermarks for statements, lines, branches and functions.

all
  • Type: boolean
  • Default: false

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

testNamePattern

  • Type string | RegExp

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

Open Vitest UI (WIP)

api

  • Type: boolean | number
  • Default: false

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

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.

transformMode

  • Type: { web?, ssr? }

Determine the transform method of modules

transformMode.ssr

  • Type: RegExp[]
  • Default: [/\.([cm]?[jt]sx?|json)$/]

Use SSR transform pipeline for the specified files.
Vite plugins will receive ssr: true flag when processing those files.

transformMode.web

  • Type: RegExp[]
  • Default: modules other than those specified in transformMode.ssr

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.

When you use JSX as component models other than React (e.g. Vue JSX or SolidJS), you might want to config as following to make .tsx / .jsx transformed as client-side components:

ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    transformMode: {
      web: [/\.[jt]sx$/],
    },
  },
})
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    transformMode: {
      web: [/\.[jt]sx$/],
    },
  },
})

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

Allow tests and suites that are marked as only.

dangerouslyIgnoreUnhandledErrors

  • Type: boolean
  • Default: false

Ignore any unhandled errors that occur.

passWithNoTests

  • Type: boolean
  • Default: false

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

logHeapUsage

  • Type: boolean
  • Default: 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? }

Options for how tests should be sorted.

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

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.seed

  • Type: number
  • Default: Date.now()

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

sequence.hooks

  • Type: 'stack' | 'list' | 'parallel'
  • Default: 'parallel'

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)

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 typecheker:

  • 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.{ts,js}']

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.

Released under the MIT License.