Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Vitest is a next-generation testing framework powered by Vite. This is a monorep
- **Core directory test**: `CI=true pnpm test <test-file>` (for `test/core`)
- **Browser tests**: `CI=true pnpm test:browser:playwright` or `CI=true pnpm test:browser:webdriverio`

When writing tests, AVOID using `toContain` for validation. Prefer using `toMatchSnapshot` to include the test error and its stack.

If you need to typecheck tests, run `pnpm typecheck` from the root of the workspace.

### Testing Utilities
- **`runInlineTests`** from `test/test-utils/index.ts` - You must use this for complex file system setups (>1 file)
- **`runVitest`** from `test/test-utils/index.ts` - You can use this to run Vitest programmatically
Expand Down
67 changes: 38 additions & 29 deletions docs/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,47 +261,42 @@ Whether the test is expected to fail. If it does, the test will pass, otherwise

- **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](/guide/test-context.html#test-extend) for more information.
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](/guide/test-context#extend-test-context) for more information.

```ts
import { test as baseTest, expect } from 'vitest'

const todos = []
const archive = []

const test = baseTest.extend({
todos: async ({ task }, use) => {
todos.push(1, 2, 3)
await use(todos)
todos.length = 0
},
archive,
})

test('add item', ({ todos }) => {
expect(todos.length).toBe(3)
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}`
})

todos.push(4)
expect(todos.length).toBe(4)
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.scoped <Version>3.1.0</Version> {#test-scoped}

- **Alias:** `it.scoped`
## test.override <Version>4.1.0</Version> {#test-override}

Use `test.scoped` 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 [Scoping Values to Suite](/guide/test-context.html#scoping-values-to-suite) for more information.
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](/guide/test-context.html#overriding-fixture-values) for more information.

```ts
import { test as baseTest, describe, expect } from 'vitest'

const test = baseTest.extend({
dependency: 'default',
dependant: ({ dependency }, use) => use({ dependency }),
})
const test = baseTest
.extend('dependency', 'default')
.extend('dependant', ({ dependency }) => dependency)

describe('use scoped values', () => {
test.scoped({ dependency: 'new' })
test.override({ dependency: 'new' })

test('uses scoped value', ({ dependant }) => {
// `dependant` uses the new overridden value that is scoped
Expand All @@ -311,6 +306,16 @@ describe('use scoped values', () => {
})
```

## test.scoped <Version>3.1.0</Version> <Deprecated /> {#test-scoped}

- **Alias:** `it.scoped`

::: danger DEPRECATED
`test.scoped` is deprecated in favor of [`test.override`](#test-override) and will be removed in a future major version.
:::

Alias of [`test.override`](#test-override)

## test.skip

- **Alias:** `it.skip`
Expand Down Expand Up @@ -661,6 +666,10 @@ test.concurrent.for([

Scoped `describe`. See [describe](/api/describe) for more information.

## test.suite <Version>4.1.0</Version> {#test-suite}

Alias for `suite`. See [describe](/api/describe) for more information.

## test.beforeEach

Scoped `beforeEach` hook that inherits types from [`test.extend`](#test-extend). See [beforeEach](/api/hooks#beforeeach) for more information.
Expand All @@ -671,19 +680,19 @@ Scoped `afterEach` hook that inherits types from [`test.extend`](#test-extend).

## test.beforeAll

Scoped `beforeAll` hook. See [beforeAll](/api/hooks#beforeall) for more information.
Scoped `beforeAll` hook that inherits types from [`test.extend`](#test-extend). See [beforeAll](/api/hooks#beforeall) for more information.

## test.afterAll

Scoped `afterAll` hook. See [afterAll](/api/hooks#afterall) for more information.
Scoped `afterAll` hook that inherits types from [`test.extend`](#test-extend). See [afterAll](/api/hooks#afterall) for more information.

## test.aroundEach <Version>4.1.0</Version> {#test-aroundeach}

Scoped `aroundEach` hook that inherits types from [`test.extend`](#test-extend). See [aroundEach](/api/hooks#aroundeach) for more information.

## test.aroundAll <Version>4.1.0</Version> {#test-aroundall}

Scoped `aroundAll` hook. See [aroundAll](/api/hooks#aroundall) for more information.
Scoped `aroundAll` hook that inherits types from [`test.extend`](#test-extend). See [aroundAll](/api/hooks#aroundall) for more information.

## bench <Experimental /> {#bench}

Expand Down
Loading
Loading