Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vitest 3.x reference skill covering configuration, test/describe APIs, mocking, coverage, snapshots, and concurrency.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/core-test-api.md
1---2name: test-api3description: test/it function for defining tests with modifiers4---56# Test API78## Basic Test910```ts11import { expect, test } from 'vitest'1213test('adds numbers', () => {14expect(1 + 1).toBe(2)15})1617// Alias: it18import { it } from 'vitest'1920it('works the same', () => {21expect(true).toBe(true)22})23```2425## Async Tests2627```ts28test('async test', async () => {29const result = await fetchData()30expect(result).toBeDefined()31})3233// Promises are automatically awaited34test('returns promise', () => {35return fetchData().then(result => {36expect(result).toBeDefined()37})38})39```4041## Test Options4243```ts44// Timeout (default: 5000ms)45test('slow test', async () => {46// ...47}, 10_000)4849// Or with options object50test('with options', { timeout: 10_000, retry: 2 }, async () => {51// ...52})53```5455## Test Modifiers5657### Skip Tests5859```ts60test.skip('skipped test', () => {61// Won't run62})6364// Conditional skip65test.skipIf(process.env.CI)('not in CI', () => {})66test.runIf(process.env.CI)('only in CI', () => {})6768// Dynamic skip via context69test('dynamic skip', ({ skip }) => {70skip(someCondition, 'reason')71// ...72})73```7475### Focus Tests7677```ts78test.only('only this runs', () => {79// Other tests in file are skipped80})81```8283### Todo Tests8485```ts86test.todo('implement later')8788test.todo('with body', () => {89// Not run, shows in report90})91```9293### Failing Tests9495```ts96test.fails('expected to fail', () => {97expect(1).toBe(2) // Test passes because assertion fails98})99```100101### Concurrent Tests102103```ts104// Run tests in parallel105test.concurrent('test 1', async ({ expect }) => {106// Use context.expect for concurrent tests107expect(await fetch1()).toBe('result')108})109110test.concurrent('test 2', async ({ expect }) => {111expect(await fetch2()).toBe('result')112})113```114115### Sequential Tests116117```ts118// Force sequential in concurrent context119test.sequential('must run alone', async () => {})120```121122## Parameterized Tests123124### test.each125126```ts127test.each([128[1, 1, 2],129[1, 2, 3],130[2, 1, 3],131])('add(%i, %i) = %i', (a, b, expected) => {132expect(a + b).toBe(expected)133})134135// With objects136test.each([137{ a: 1, b: 1, expected: 2 },138{ a: 1, b: 2, expected: 3 },139])('add($a, $b) = $expected', ({ a, b, expected }) => {140expect(a + b).toBe(expected)141})142143// Template literal144test.each`145a | b | expected146${1} | ${1} | ${2}147${1} | ${2} | ${3}148`('add($a, $b) = $expected', ({ a, b, expected }) => {149expect(a + b).toBe(expected)150})151```152153### test.for154155Preferred over `.each` - doesn't spread arrays:156157```ts158test.for([159[1, 1, 2],160[1, 2, 3],161])('add(%i, %i) = %i', ([a, b, expected], { expect }) => {162// Second arg is TestContext163expect(a + b).toBe(expected)164})165```166167## Test Context168169First argument provides context utilities:170171```ts172test('with context', ({ expect, skip, task }) => {173console.log(task.name) // Test name174skip(someCondition) // Skip dynamically175expect(1).toBe(1) // Context-bound expect176})177```178179## Custom Test with Fixtures180181```ts182import { test as base } from 'vitest'183184const test = base.extend({185db: async ({}, use) => {186const db = await createDb()187await use(db)188await db.close()189},190})191192test('query', async ({ db }) => {193const users = await db.query('SELECT * FROM users')194expect(users).toBeDefined()195})196```197198## Retry Configuration199200```ts201test('flaky test', { retry: 3 }, async () => {202// Retries up to 3 times on failure203})204205// Advanced retry options206test('with delay', {207retry: {208count: 3,209delay: 1000,210condition: /timeout/i, // Only retry on timeout errors211},212}, async () => {})213```214215## Tags216217```ts218test('database test', { tags: ['db', 'slow'] }, async () => {})219220// Run with: vitest --tags db221```222223## Key Points224225- Tests with no body are marked as `todo`226- `test.only` throws in CI unless `allowOnly: true`227- Use context's `expect` for concurrent tests and snapshots228- Function name is used as test name if passed as first arg229230<!--231Source references:232- https://vitest.dev/api/test.html233-->234