Test
- Alias:
it
function test(
name: string | Function,
body?: () => unknown,
timeout?: number
): void
function test(
name: string | Function,
options: TestOptions,
body?: () => unknown,
): voidtest or it defines a set of related expectations. It receives the test name and a function that holds the expectations to test.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before terminating, or a set of additional options. The default timeout is 5 seconds, and can be configured globally with testTimeout.
import { expect, test } from 'vitest'
test('should work as expected', () => {
expect(Math.sqrt(4)).toBe(2)
})WARNING
If the first argument is a function, its name property will be used as the name of the test. The function itself will not be called.
If test body is not provided, the test is marked as todo.
When a test function returns a promise, the runner will wait until it is resolved to collect async expectations. If the promise is rejected, the test will fail.
TIP
In Jest, TestFunction can also be of type (done: DoneCallback) => void. If this form is used, the test will not be concluded until done is called. You can achieve the same using an async function, see the Migration guide Done Callback section.
Test Options
You can define boolean options by chaining properties on a function:
import { test } from 'vitest'
test.skip('skipped test', () => {
// some logic that fails right now
})
test.concurrent.skip('skipped concurrent test', () => {
// some logic that fails right now
})But you can also provide an object as a second argument instead:
import { test } from 'vitest'
test('skipped test', { skip: true }, () => {
// some logic that fails right now
})
test('skipped concurrent test', { skip: true, concurrent: true }, () => {
// some logic that fails right now
})They both work in exactly the same way. To use either one is purely a stylistic choice.
Options set to undefined are ignored. They don't override values from the chain, the parent suite, or tags.
timeout
- Type:
number - Default:
5_000(configured bytestTimeout)
Test timeout in milliseconds.
WARNING
Note that if you are providing timeout as the last argument, you cannot use options anymore:
import { test } from 'vitest'
// ✅ this works
test.skip('heavy test', () => {
// ...
}, 10_000)
// ❌ this doesn't work
test('heavy test', { skip: true }, () => {
// ...
}, 10_000)However, you can provide a timeout inside the object:
import { test } from 'vitest'
// ✅ this works
test('heavy test', { skip: true, timeout: 10_000 }, () => {
// ...
})retry
- Default:
0(configured byretry) - Type:
type Retry = number | {
/**
* The number of times to retry the test if it fails.
* @default 0
*/
count?: number
/**
* Delay in milliseconds between retry attempts.
* @default 0
*/
delay?: number
/**
* Condition to determine if a test should be retried based on the error.
* - If a RegExp, it is tested against the error message
* - If a function, called with the TestError object; return true to retry
*
* NOTE: Functions can only be used in test files, not in vitest.config.ts,
* because the configuration is serialized when passed to worker threads.
*
* @default undefined (retry on all errors)
*/
condition?: RegExp | ((error: TestError) => boolean)
}Retry configuration for the test. If a number, specifies how many times to retry. If an object, allows fine-grained retry control.
Note that the object configuration is available only since Vitest 4.1.
repeats
- Type:
number - Default:
0
How many times the test will run again. If set to 0 (the default), the test will run only one time.
This can be useful for debugging flaky tests.
Each repeat gets its own seed for context.random and vi.useFakeRandom, so a test that uses them runs with other values each time. If a repeat that used its seed fails, Vitest prints the seed and a command that runs the test with this seed. See Seeded Tests.
If a repeat calls skip(), Vitest skips only this repeat, and the next repeats run. The test is skipped only if all of its repeats are skipped.
import { expect, test } from 'vitest'
test('the queue keeps the order of messages', { repeats: 99 }, async ({ random }) => {
const queue = new Queue()
const messages = random.sample(['create', 'update', 'delete', 'archive'], 3)
for (const message of messages) {
await queue.push(message, { delay: random.int(0, 50) })
}
expect(await queue.drain()).toEqual(messages)
})seeds experimental 5.2.0+
- Type:
string[] - Default:
[]
Seeds that run before the repeats, for example seeds that failed before. Each seed has 16 hexadecimal digits, as Vitest prints it, and adds 1 run. A seed can be in the list only 1 time. See Keep the Seeds That Failed.
import { test } from 'vitest'
test('one leader per term', { repeats: 199, seeds: ['3fa2c9e1b07d5a44'] }, async ({ random }) => {
// ...
})The test runs 201 times: first the seed 3fa2c9e1b07d5a44, then the 200 runs of repeats. A test with seeds and without repeats runs its seeds and then 1 time with its own seed. The repeats of a tag or of the config also apply to a test with seeds. A tag cannot set seeds.
fakeTimers experimental 5.2.0+
- Type:
boolean | FakeTimersConfig
Runs the test with fake timers. Vitest calls vi.useFakeTimers() before the aroundEach, beforeEach and afterEach hooks and the fixtures of the test, and vi.useRealTimers() after them. Each retry and each repeat gets a new clock, and the timers of the test do not stay for the next test:
import { expect, test, vi } from 'vitest'
test('a debounced search runs 1 time', { fakeTimers: true }, () => {
const search = vi.fn()
const debounced = debounce(search, 300)
debounced()
debounced()
vi.advanceTimersByTime(300)
expect(search).toHaveBeenCalledTimes(1)
})An object sets the fakeTimers options of the test. They are merged with the options of the config:
test('shows the date of the order', {
fakeTimers: { toFake: ['Date'], now: new Date('2024-02-29') },
}, () => {
// ...
})In the test option, now can also be a range. Then the clock starts at a time from the seed of the test. Each repeat tests another date, and the replay command of a failure starts at the same date. The range includes min and max:
test('a subscription renews in the next month', {
repeats: 100,
fakeTimers: {
now: { min: new Date('2024-01-01'), max: new Date('2027-12-31') },
},
}, () => {
// ...
})The first and the last day of a month, midnight, and the last millisecond of a day are more likely than other times, because they are edge cases. The time is the same on every machine, but local dates also depend on the time zone.
beforeAllandafterAllhooks run with real timers. Fixtures of file and worker scope that the test uses also start with real timers.- The test fails if the timers are already fake when it starts, or if a fixture of file or worker scope throws before the fake clock starts. Then the test, its hooks, its retries and its repeats do not run.
- A concurrent test cannot use the option, because concurrent tests share the timers.
vi.useFakeTimers()does not accept anowrange.
false turns off the option that the test gets from its suite or from a tag.
fakeRandom experimental 5.2.0+
- Type:
boolean
true runs the test with the fake functions of vi.useFakeRandom(), so Math.random and the random functions of crypto return values from the seed of the test. The aroundEach, beforeEach and afterEach hooks and the fixtures of the test also get the fake functions. After them, Vitest restores the functions that the test started with:
import { expect, test } from 'vitest'
import { dealHands } from './cards.js'
test('deals every card one time', { repeats: 20, fakeRandom: true }, () => {
const hands = dealHands(4)
expect(new Set(hands.flat()).size).toBe(52)
})false runs the test with the real functions. Use it for a test in a project with fakeRandom.enableGlobally. A concurrent test cannot set true.
A library that kept a reference to a random function when it loaded still calls that function. To replace the functions before your modules load, set fakeRandom.enableGlobally.
tags 4.1.0+
- Type:
string[] - Default:
[]
Custom user tags. If the tag is not specified in the configuration, the test will fail before it starts, unless strictTags is disabled manually.
import { it } from 'vitest'
it('user returns data from db', { tags: ['db', 'flaky'] }, () => {
// ...
})meta 4.1.0+
- Type:
TaskMeta
Attaches custom metadata available in reporters.
WARNING
Vitest merges top-level properties inherited from suites or tags. However, it does not perform a deep merge of nested objects.
import { describe, test } from 'vitest'
describe(
'nested meta',
{
meta: {
nested: { object: true, array: false },
},
},
() => {
test(
'overrides part of meta',
{
meta: {
nested: { object: false }
},
},
({ task }) => {
// task.meta === { nested: { object: false } }
// notice array got lost because "nested" object was overridden
}
)
}
)Prefer using non-nested meta, if possible.
concurrent
- Type:
boolean - Default:
false(configured bysequence.concurrent) - Alias:
test.concurrent
Whether this test run concurrently with other concurrent tests in the suite.
Set concurrent to false to opt out of concurrency inherited from describe.concurrent or sequence.concurrent:
test('runs sequentially', { concurrent: false }, async () => {
// ...
})skip
- Type:
boolean - Default:
false - Alias:
test.skip
Whether the test should be skipped.
only
- Type:
boolean - Default:
false - Alias:
test.only
Should this test be the only one running in a suite.
todo
- Type:
boolean - Default:
false - Alias:
test.todo
Whether the test should be skipped and marked as a todo.
fails
- Type:
boolean - Default:
false - Alias:
test.fails
Whether the test is expected to fail. If it does, the test will pass, otherwise it will fail.
test.extend
- Alias:
it.extend
Use test.extend to extend the test context with custom fixtures. This will return a new test and it's also extendable, so you can compose more fixtures or override existing ones by extending it as you need. See Extend Test Context for more information.
import { test as baseTest, expect } from 'vitest'
export const test = baseTest
// Simple value - type is inferred as { port: number; host: string }
.extend('config', { port: 3000, host: 'localhost' })
// Function fixture - type is inferred from return value
.extend('server', async ({ config }) => {
// TypeScript knows config is { port: number; host: string }
return `http://${config.host}:${config.port}`
})
test('server uses correct port', ({ config, server }) => {
// TypeScript knows the types:
// - config is { port: number; host: string }
// - server is string
expect(server).toBe('http://localhost:3000')
expect(config.port).toBe(3000)
})test.override 4.1.0+
Use test.override to override fixture values for all tests within the current suite and its nested suites. This must be called at the top level of a describe block. See Overriding Fixture Values for more information.
import { test as baseTest, describe, expect } from 'vitest'
const test = baseTest
.extend('dependency', 'default')
.extend('dependant', ({ dependency }) => dependency)
describe('use scoped values', () => {
test.override({ dependency: 'new' })
test('uses scoped value', ({ dependant }) => {
// `dependant` uses the new overridden value that is scoped
// to all tests in this suite
expect(dependant).toEqual({ dependency: 'new' })
})
})test.scoped 3.1.0+ deprecated
- Alias:
it.scoped
DEPRECATED
test.scoped is deprecated in favor of test.override and will be removed in a future major version.
Alias of test.override
test.skip
- Alias:
it.skip
If you want to skip running certain tests, but you don't want to delete the code due to any reason, you can use test.skip to avoid running them.
import { assert, test } from 'vitest'
test.skip('skipped test', () => {
// Test skipped, no error
assert.equal(Math.sqrt(4), 3)
})You can also skip test by calling skip on its context dynamically:
import { assert, test } from 'vitest'
test('skipped test', (context) => {
context.skip()
// Test skipped, no error
assert.equal(Math.sqrt(4), 3)
})If the condition is unknown, you can provide it to the skip method as the first arguments:
import { assert, test } from 'vitest'
test('skipped test', (context) => {
context.skip(Math.random() < 0.5, 'optional message')
// Test skipped, no error
assert.equal(Math.sqrt(4), 3)
})test.skipIf
- Alias:
it.skipIf
In some cases you might run tests multiple times with different environments, and some of the tests might be environment-specific. Instead of wrapping the test code with if, you can use test.skipIf to skip the test whenever the condition is truthy.
import { assert, test } from 'vitest'
const isDev = process.env.NODE_ENV === 'development'
test.skipIf(isDev)('prod only test', () => {
// this test only runs in production
})Unlike test.skip, test.skipIf always sets the skip option: true if the condition is truthy, false otherwise. A falsy condition overrides skip inherited from tags, the same as test(name, { skip: false }).
test.runIf
- Alias:
it.runIf
Opposite of test.skipIf. It always sets the skip option: false if the condition is truthy, true otherwise.
import { assert, test } from 'vitest'
const isDev = process.env.NODE_ENV === 'development'
test.runIf(isDev)('dev only test', () => {
// this test only runs in development
})test.only
- Alias:
it.only
Use test.only to only run certain tests in a given suite. This is useful when debugging.
import { assert, test } from 'vitest'
test.only('test', () => {
// Only this test (and others marked with only) are run
assert.equal(Math.sqrt(4), 2)
})Sometimes it is very useful to run only tests in a certain file, ignoring all other tests from the whole test suite, which pollute the output.
In order to do that, run vitest with specific file containing the tests in question:
vitest interesting.test.tsWARNING
Vitest detects when tests are running in CI and will throw an error if any test has only flag. You can configure this behaviour via allowOnly option.
test.concurrent
- Alias:
it.concurrent
test.concurrent marks consecutive tests to be run in parallel. It receives the test name, an async function with the tests to collect, and an optional timeout (in milliseconds).
import { describe, test } from 'vitest'
// The two tests marked with concurrent will be run in parallel
describe('suite', () => {
test('serial test', async () => { /* ... */ })
test.concurrent('concurrent test 1', async () => { /* ... */ })
test.concurrent('concurrent test 2', async () => { /* ... */ })
})test.skip, test.only, and test.todo works with concurrent tests. All the following combinations are valid:
test.concurrent(/* ... */)
test.skip.concurrent(/* ... */) // or test.concurrent.skip(/* ... */)
test.only.concurrent(/* ... */) // or test.concurrent.only(/* ... */)
test.todo.concurrent(/* ... */) // or test.concurrent.todo(/* ... */)When running concurrent tests, Snapshots and Assertions must use expect from the local Test Context to ensure the right test is detected.
test.concurrent('test 1', async ({ expect }) => {
expect(foo).toMatchSnapshot()
})
test.concurrent('test 2', async ({ expect }) => {
expect(foo).toMatchSnapshot()
})Note that if tests are synchronous, Vitest will still run them sequentially.
test.todo
- Alias:
it.todo
Use test.todo to stub tests to be implemented later. An entry will be shown in the report for the tests so you know how many tests you still need to implement.
// An entry will be shown in the report for this test
test.todo('unimplemented test', () => {
// failing implementation...
})TIP
Vitest will automatically mark test as todo if test has no body.
test.fails
- Alias:
it.fails
Use test.fails to indicate that an assertion will fail explicitly.
import { expect, test } from 'vitest'
test.fails('repro #1234', () => {
expect(add(1, 2)).toBe(4)
})This flag is useful to track difference in behaviour of your library over time. For example, you can define a failing test without fixing the issue yet due to time constraints. Tests marked with fails are tracked in the test summary since Vitest 4.1.
test.each
- Alias:
it.each
TIP
While test.each is provided for Jest compatibility, Vitest also has test.for with an additional feature to integrate TestContext.
Use test.each when you need to run the same test with different variables. You can inject parameters with printf formatting in the test name in the order of the test function parameters.
%s: string%d: number%i: integer%f: floating point value%j: json%o: object%#: 0-based index of the test case%$: 1-based index of the test case%%: single percent sign ('%')
import { expect, test } from 'vitest'
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('add(%i, %i) -> %i', (a, b, expected) => {
expect(a + b).toBe(expected)
})
// this will return
// ✓ add(1, 1) -> 2
// ✓ add(1, 2) -> 3
// ✓ add(2, 1) -> 3You can also access object properties and array elements with $ prefix:
test.each([
{ a: 1, b: 1, expected: 2 },
{ a: 1, b: 2, expected: 3 },
{ a: 2, b: 1, expected: 3 },
])('add($a, $b) -> $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected)
})
// this will return
// ✓ add(1, 1) -> 2
// ✓ add(1, 2) -> 3
// ✓ add(2, 1) -> 3
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('add($0, $1) -> $2', (a, b, expected) => {
expect(a + b).toBe(expected)
})
// this will return
// ✓ add(1, 1) -> 2
// ✓ add(1, 2) -> 3
// ✓ add(2, 1) -> 3You can also access Object attributes with ., if you are using objects as arguments:
test.each`
a | b | expected
${{ val: 1 }} | ${'b'} | ${'1b'}
${{ val: 2 }} | ${'b'} | ${'2b'}
${{ val: 3 }} | ${'b'} | ${'3b'}
`('add($a.val, $b) -> $expected', ({ a, b, expected }) => {
expect(a.val + b).toBe(expected)
})
// this will return
// ✓ add(1, b) -> 1b
// ✓ add(2, b) -> 2b
// ✓ add(3, b) -> 3b- First row should be column names, separated by
|; - One or more subsequent rows of data supplied as template literal expressions using
${value}syntax.
import { expect, test } from 'vitest'
test.each`
a | b | expected
${1} | ${1} | ${2}
${'a'} | ${'b'} | ${'ab'}
${[]} | ${'b'} | ${'b'}
${{}} | ${'b'} | ${'[object Object]b'}
${{ asd: 1 }} | ${'b'} | ${'[object Object]b'}
`('returns $expected when $a is added $b', ({ a, b, expected }) => {
expect(a + b).toBe(expected)
})TIP
Vitest formats interpolated title values with its display formatter. If the value is too truncated, you can increase taskTitleValueFormatTruncate in your config file.
test.for
- Alias:
it.for
Alternative to test.each to provide TestContext.
The difference from test.each lies in how arrays are provided in the arguments. Non-array arguments to test.for (including template string usage) work exactly the same as for test.each.
// `each` spreads arrays
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('add(%i, %i) -> %i', (a, b, expected) => {
expect(a + b).toBe(expected)
})
// `for` doesn't spread arrays (notice the square brackets around the arguments)
test.for([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('add(%i, %i) -> %i', ([a, b, expected]) => {
expect(a + b).toBe(expected)
})The 2nd argument is TestContext and can be used for concurrent snapshots, for example:
test.concurrent.for([
[1, 1],
[1, 2],
[2, 1],
])('add(%i, %i)', ([a, b], { expect }) => {
expect(a + b).toMatchSnapshot()
})test.describe 4.1.0+
Scoped describe. See describe for more information.
test.suite 4.1.0+
Alias for suite. See describe for more information.
test.beforeEach
Scoped beforeEach hook that inherits types from test.extend. See beforeEach for more information.
test.afterEach
Scoped afterEach hook that inherits types from test.extend. See afterEach for more information.
test.beforeAll
Scoped beforeAll hook that inherits types from test.extend. See beforeAll for more information.
test.afterAll
Scoped afterAll hook that inherits types from test.extend. See afterAll for more information.
test.aroundEach 4.1.0+
Scoped aroundEach hook that inherits types from test.extend. See aroundEach for more information.
test.aroundAll 4.1.0+
Scoped aroundAll hook that inherits types from test.extend. See aroundAll for more information.
bench experimental
Updated in Vitest 5
The benchmarking API has been rewritten. bench is no longer a top-level import from vitest, and the bench.skip / bench.only / bench.todo helpers have been removed. bench is now a test-context fixture accessed from inside a test().
See the Benchmarking guide for the new API.