new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Vitest · Config reference · all subjects

config

342 notes in this subject, read out of this brain and free to use. This is page 5 of 6.

silent CLI flags

The silent option can be set via CLI using --silent or --silent=false flags.

silent config option type and default

The silent configuration option has type boolean | 'passed-only' with a default value of false.

slowTestThreshold config option

The slowTestThreshold option defines the number of milliseconds after which a test or suite is considered slow and reported as such in the results. It is of type number with a default value of 300 milliseconds. It can be configured via CLI using --slow-test-threshold=<number> or --slowTestThreshold=<number>.

setupFiles automatic rerun on file edit

Editing a setup file will automatically trigger a rerun of all tests.

setupFiles vs globalSetup execution differences

Setup files are executed in the same process as tests, whereas globalSetup runs once in the main thread before any test worker is created.

setupFiles isolation disabled example pattern

Example pattern for setupFiles when isolation is disabled: Use a global variable to track whether initialization has occurred, and only initialize once. Wrap heavy setup logic behind this check, and use hooks like afterEach to reset state before each test file. ```ts import { config } from '@some-testing-lib' if (!globalThis.setupInitialized) { config.plugins = [myCoolPlugin] computeHeavyThing() globalThis.setupInitialized = true } // hooks reset before each test file afterEach(() => { cleanup() }) globalThis.resetBeforeEachTest = true ```

setupFiles configuration option type and purpose

The setupFiles option accepts type `string | string[]` and specifies paths to setup files resolved relative to the root. These files run before each test file in the same process. By default, all test files run in parallel, but execution order can be configured with the `sequence.setupFiles` option.

setupFiles exports are ignored

Vitest will ignore any exports from setupFiles.

setupFiles behavior when isolation is disabled

When isolation is disabled, imported modules are cached, but the setup file itself is executed again before each test file, meaning the same global object is accessed before each test file. Care must be taken not to execute the same initialization more than necessary.

Using VITEST_POOL_ID to distinguish between workers

You can access `process.env.VITEST_POOL_ID` (an integer-like string) inside setup files to distinguish between workers and spread the workload when a heavy process is running in the background.

SnapshotEnvironment interface specification

A custom snapshot environment implementation must have the shape of the SnapshotEnvironment interface with the following methods: getVersion() returns a string, getHeader() returns a string, resolvePath(filepath: string) returns Promise<string>, resolveRawPath(testPath: string, rawPath: string) returns Promise<string>, saveSnapshotFile(filepath: string, snapshot: string) returns Promise<void>, readSnapshotFile(filepath: string) returns Promise<string | null>, and removeSnapshotFile(filepath: string) returns Promise<void>.

Prefer snapshotFormat and resolveSnapshotPath for standard snapshot configuration

For basic snapshot configuration needs, use the snapshotFormat or resolveSnapshotPath configuration options instead of snapshotEnvironment.

snapshotEnvironment is a low-level option

snapshotEnvironment is a low-level configuration option that should only be used for advanced cases where you do not have access to default Node.js APIs.

Extending default VitestSnapshotEnvironment

The default VitestSnapshotEnvironment can be extended from the 'vitest/snapshot' entry point if only part of the API needs to be overwritten, rather than implementing the entire interface from scratch.

snapshotEnvironment config option type and purpose

The snapshotEnvironment configuration option accepts a string type representing the path to a custom snapshot environment implementation. It is useful when running tests in an environment that doesn't support Node.js APIs. This option has no effect on a browser runner.

snapshotFormat maxOutputLength option

The maxOutputLength option in snapshotFormat is an approximate per-depth output budget, not a hard cap on the final rendered string.

snapshotFormat config option type

The snapshotFormat configuration option has the type Omit<PrettyFormatOptions, 'plugins' | 'compareKeys'> & { compareKeys?: null | undefined }. This means it accepts PrettyFormatOptions with the plugins and compareKeys properties removed, then optionally adds back compareKeys as null or undefined.

snapshotFormat default formatting options

Vitest snapshots apply these default formatting options before snapshotFormat overrides: printBasicPrototype set to false, escapeString set to false, escapeRegex set to true, and printFunctionName set to false.

snapshotFormat printShadowRoot option

The printShadowRoot formatter option controls whether shadow-root contents are included in DOM snapshots.

snapshotFormat compareKeys default behavior

By default, snapshot keys are sorted using the formatter's default behavior. Set compareKeys to null to disable key sorting. Custom compare functions are not supported in snapshotFormat.

snapshotFormat plugins are ignored

The plugins property on the snapshotFormat object will be ignored. To extend snapshot serialization via pretty-format plugins, use expect.addSnapshotSerializer or the snapshotSerializers config option instead.

snapshotSerializers config option

The snapshotSerializers option is a string array that specifies paths to snapshot serializer modules for snapshot testing. It allows you to add custom snapshot serializers. The type is string[], and the default value is an empty array [].

strictTags behavior

When strictTags is enabled, Vitest will throw an error if a test has a tag that is not defined in the config. This prevents silently applying wrong configuration or skipping tests due to mistyped tag names. Vitest will always throw an error if the --tags-filter flag defines a tag not present in the config, regardless of strictTags setting.

strictTags example with typo

Example showing strictTags error handling: A test with tags: ['fortnend'] will throw an error because 'fortnend' is not defined in the config (the correct tag name is 'frontend'). The config defines tags using { name: 'frontend' }. This demonstrates how strictTags catches typos in tag names.

strictTags CLI flags

The strictTags option can be controlled via CLI using the flags --strict-tags to enable it and --no-strict-tags to disable it.

strictTags option type and default

The strictTags configuration option is of type boolean with a default value of true.

taskTitleValueFormatTruncate affects test.each and test.for

The taskTitleValueFormatTruncate option affects values inserted by APIs like test.each and test.for, including both $value and % placeholder formatting.

taskTitleValueFormatTruncate can be disabled with 0

Setting taskTitleValueFormatTruncate to 0 disables truncation of formatted values in task titles.

taskTitleValueFormatTruncate option name, type and default

The taskTitleValueFormatTruncate configuration option has type number and a default value of 40. It sets the length limit for formatted values interpolated into generated task titles.

teardownTimeout configuration option

The teardownTimeout option specifies the default timeout to wait for close when Vitest shuts down, measured in milliseconds. Its type is number with a default value of 10000 milliseconds. It can be set via CLI using --teardown-timeout=5000 or --teardownTimeout=5000.

testNamePattern config type and CLI options

The testNamePattern configuration option has type string or RegExp. It can be set via CLI with -t <pattern>, --testNamePattern=<pattern>, or --test-name-pattern=<pattern>.

testNamePattern matches full test names with suite path

The testNamePattern pattern is matched against the test's full name, which is the enclosing suite names and the test name joined with ' > '. For example, a test named 'adds' inside a suite named 'math' has full name 'math > adds' and will match both -t 'math > adds' and -t adds.

testNamePattern Vitest 5 breaking change from Jest compatibility

Before Vitest 5, test full name segments were joined with a single space (e.g. 'math adds') to mirror Jest. From Vitest 5 onwards, segments are joined with ' > '.

testNamePattern matching example with suite and test

Example showing testNamePattern usage: import { describe, expect, test } from 'vitest'; describe('math', () => { test('adds', () => { expect(1 + 1).toBe(2) }) }). This test has full name 'math > adds' and runs with -t 'math > adds' or -t adds.

testNamePattern behavior with matching patterns

When testNamePattern is set, only tests with full names matching the pattern will run. Tests not containing the matching pattern in their full name will be skipped.

Tags can define test options that apply to marked tests

Tags can define test options that will be applied to every test marked with that tag. These options are merged with the test's own options, with the test's options taking precedence over tag options.

retry.condition in tags must be regexp

The retry.condition option can only be a regexp when defined in a tag, because config values need to be serialized.

Tags cannot apply other tags via test options

Tags cannot apply other tags via the test options they define.

Complete tags configuration example

import { defineConfig } from 'vitest/config' export default defineConfig({ test: { tags: [ { name: 'unit', description: 'Unit tests.', }, { name: 'e2e', description: 'End-to-end tests.', timeout: 60_000, }, { name: 'flaky', description: 'Flaky tests that need retries.', retry: process.env.CI ? 3 : 0, priority: 1, }, { name: 'slow', description: 'Slow tests.', timeout: 120_000, }, { name: 'skip-ci', description: 'Tests to skip in CI.', skip: !!process.env.CI, }, ], }, })

tags option defines available test tags

The tags configuration option defines all available tags in your test project. By default, if a test defines a tag name not listed in this configuration, Vitest will throw an error, though this behavior can be configured via the strictTags option.

tags configuration option type and default

The tags configuration option has type TestTagDefinition[] and a default value of an empty array [].

tags configuration in projects inheritance

When using the projects configuration, projects will automatically inherit all global tag definitions.

TestTagDefinition.name field

The name field is a required string property of TestTagDefinition that specifies the name of the tag. This is the value used in the tags option when marking tests.

TestTagDefinition.description field

The description field is an optional string property of TestTagDefinition that provides a human-readable description for the tag. This description is shown in the UI and in error messages when a tag is not found.

TestTagDefinition.priority field

The priority field is a number property of TestTagDefinition with a default value of Infinity. It determines the priority for merging options when multiple tags with the same options are applied to a test. Lower numbers mean higher priority (for example, priority 1 takes precedence over priority 3).

testTimeout CLI flags

The testTimeout option can be set via CLI using either --test-timeout=5000 or --testTimeout=5000 flag syntax.

testTimeout option type and defaults

The testTimeout configuration option is a number type. Its default value is 5000 milliseconds in Node.js and 15000 milliseconds when browser.enabled is true. A value of 0 disables timeout completely.

ui configuration option

The ui option is a boolean configuration parameter with a default value of false. It enables Vitest UI and can be set via CLI with --ui or --ui=false flags.

ui requires @vitest/ui package

The ui feature requires the @vitest/ui package to be installed. If not already installed, Vitest will install it automatically when you run the test command for the first time.

ui server security with api.host

When api.host is set to anything other than localhost, the UI server becomes read-only and disables buttons for saving code or running tests. This is a security measure to prevent exposure of the UI server to the network.

unstubEnvs option type and default

The unstubEnvs configuration option has type boolean and a default value of false.

unstubEnvs configuration example

Example configuration showing unstubEnvs enabled in vitest.config.js: import { defineConfig } from 'vitest/config' export default defineConfig({ test: { unstubEnvs: true, }, })

unstubEnvs automatically calls vi.unstubAllEnvs before each test

When unstubEnvs is enabled, Vitest automatically calls vi.unstubAllEnvs() before each test.

unstubEnvs pitfall with concurrent tests

The unstubEnvs option may cause problems with async concurrent tests. When enabled, the completion of one test will restore all values changed with vi.stubEnv, including those currently being used by other tests in progress.

typecheck.allowJs configuration

The typecheck.allowJs option is a boolean that enables checking of JS files that have @ts-check comment. Its type is boolean and the default value is false. If this option is enabled in tsconfig, this configuration will not overwrite it.

typecheck.only configuration

The typecheck.only option is a boolean that runs only typecheck tests when typechecking is enabled. Its type is boolean, the default value is false, and it can be set via CLI with --typecheck.only flag. When using CLI, this option automatically enables typechecking.

typecheck.include configuration

The typecheck.include option is a glob pattern array that specifies which files should be treated as typecheck test files. Its type is string[], and the default value is ['**/*.{test,spec}-d.?(c|m)[jt]s?(x)'].

typecheck.enabled configuration

The typecheck.enabled option is a boolean that enables typechecking alongside regular tests. Its type is boolean, the default value is false, and it can be set via CLI with --typecheck or --typecheck.enabled flags.

typecheck.tsconfig configuration

The typecheck.tsconfig option specifies a path to a custom tsconfig file, relative to the project root. Its type is string, and the default value attempts to find the closest tsconfig.json file.

typecheck.exclude configuration

The typecheck.exclude option is a glob pattern array that specifies which files should not be treated as typecheck test files. Its type is string[], and the default value is ['**/node_modules/**', '**/dist/**', '**/cypress/**', '**/.{idea,git,cache,output,temp}/**'].

Give your agent this brain