Custom Fixtures with test.extend in Vitest

From the Vitest cheat sheet ยท Test Context & Fixtures ยท verified Nov 2026

Custom Fixtures with test.extend

Create reusable setup with automatic cleanup and inferred types

typescript
// my-test.ts
import { test as base } from 'vitest'

export const test = base
  .extend('baseUrl', 'http://localhost:3000')
  .extend('api', ({ baseUrl }) => createClient(baseUrl))

// users.test.ts
test('lists users', async ({ api }) => {
  expect(await api.get('/users')).toHaveLength(2)
})
๐Ÿ“Œ The builder API (4.1+) infers fixture types from return values
๐Ÿ’ก Fixtures are lazy: only the ones a test destructures are created
โš ๏ธ A file or worker fixture can't depend on a test-scoped one
๐Ÿ” onCleanup works once per fixture; split fixtures for more cleanup
fixturestest.extendonCleanup

Continue with Vitest

Save the full cheat sheet or work through every related task.

More Vitest tasks

Back to the full Vitest cheat sheet