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-describe.md
1---2name: describe-api3description: describe/suite for grouping tests into logical blocks4---56# Describe API78Group related tests into suites for organization and shared setup.910## Basic Usage1112```ts13import { describe, expect, test } from 'vitest'1415describe('Math', () => {16test('adds numbers', () => {17expect(1 + 1).toBe(2)18})1920test('subtracts numbers', () => {21expect(3 - 1).toBe(2)22})23})2425// Alias: suite26import { suite } from 'vitest'27suite('equivalent to describe', () => {})28```2930## Nested Suites3132```ts33describe('User', () => {34describe('when logged in', () => {35test('shows dashboard', () => {})36test('can update profile', () => {})37})3839describe('when logged out', () => {40test('shows login page', () => {})41})42})43```4445## Suite Options4647```ts48// All tests inherit options49describe('slow tests', { timeout: 30_000 }, () => {50test('test 1', () => {}) // 30s timeout51test('test 2', () => {}) // 30s timeout52})53```5455## Suite Modifiers5657### Skip Suites5859```ts60describe.skip('skipped suite', () => {61test('wont run', () => {})62})6364// Conditional65describe.skipIf(process.env.CI)('not in CI', () => {})66describe.runIf(!process.env.CI)('only local', () => {})67```6869### Focus Suites7071```ts72describe.only('only this suite runs', () => {73test('runs', () => {})74})75```7677### Todo Suites7879```ts80describe.todo('implement later')81```8283### Concurrent Suites8485```ts86// All tests run in parallel87describe.concurrent('parallel tests', () => {88test('test 1', async ({ expect }) => {})89test('test 2', async ({ expect }) => {})90})91```9293### Sequential in Concurrent9495```ts96describe.concurrent('parallel', () => {97test('concurrent 1', async () => {})9899describe.sequential('must be sequential', () => {100test('step 1', async () => {})101test('step 2', async () => {})102})103})104```105106### Shuffle Tests107108```ts109describe.shuffle('random order', () => {110test('test 1', () => {})111test('test 2', () => {})112test('test 3', () => {})113})114115// Or with option116describe('random', { shuffle: true }, () => {})117```118119## Parameterized Suites120121### describe.each122123```ts124describe.each([125{ name: 'Chrome', version: 100 },126{ name: 'Firefox', version: 90 },127])('$name browser', ({ name, version }) => {128test('has version', () => {129expect(version).toBeGreaterThan(0)130})131})132```133134### describe.for135136```ts137describe.for([138['Chrome', 100],139['Firefox', 90],140])('%s browser', ([name, version]) => {141test('has version', () => {142expect(version).toBeGreaterThan(0)143})144})145```146147## Hooks in Suites148149```ts150describe('Database', () => {151let db152153beforeAll(async () => {154db = await createDb()155})156157afterAll(async () => {158await db.close()159})160161beforeEach(async () => {162await db.clear()163})164165test('insert works', async () => {166await db.insert({ name: 'test' })167expect(await db.count()).toBe(1)168})169})170```171172## Modifier Combinations173174All modifiers can be chained:175176```ts177describe.skip.concurrent('skipped concurrent', () => {})178describe.only.shuffle('only and shuffled', () => {})179describe.concurrent.skip('equivalent', () => {})180```181182## Key Points183184- Top-level tests belong to an implicit file suite185- Nested suites inherit parent's options (timeout, retry, etc.)186- Hooks are scoped to their suite and nested suites187- Use `describe.concurrent` with context's `expect` for snapshots188- Shuffle order depends on `sequence.seed` config189190<!--191Source references:192- https://vitest.dev/api/describe.html193-->194