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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://vitest.dev/vitest-light.svg">
<source media="(prefers-color-scheme: light)" srcset="https://vitest.dev/vitest-dark.svg">
<img alt="rolldown logo" src="https://vitest.dev/vitest-dark.svg" height="60">
<img alt="Vitest logo" src="https://vitest.dev/vitest-dark.svg" height="60">
</picture>
</a>
<br>
Expand Down
9 changes: 9 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export default ({ mode }: { mode: string }) => {
['link', { rel: 'me', href: 'https://m.webtoo.ls/@vitest' }],
['link', { rel: 'mask-icon', href: '/logo.svg', color: '#ffffff' }],
['link', { rel: 'apple-touch-icon', href: '/apple-touch-icon.png', sizes: '180x180' }],
[
'script',
{
'src': 'https://cdn.usefathom.com/script.js',
'data-site': 'BEAFAKYG',
'data-spa': 'auto',
'defer': '',
},
],
],
lastUpdated: true,
vite: {
Expand Down
7 changes: 0 additions & 7 deletions docs/.vitepress/scripts/transformHead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ export async function transformHead({ pageData }: TransformContext): Promise<Hea
})

head.push(['link', { rel: 'prefetch', href: '/logo.svg', as: 'image' }])
if (home) {
head.push(['link', { rel: 'prefetch', href: '/logo-shadow.svg', as: 'image' }])
head.push(['link', { rel: 'prefetch', href: '/sponsors/antfu.svg', as: 'image' }])
head.push(['link', { rel: 'prefetch', href: '/sponsors/sheremet-va.svg', as: 'image' }])
head.push(['link', { rel: 'prefetch', href: '/sponsors/patak-dev.svg', as: 'image' }])
head.push(['link', { rel: 'prefetch', href: '/netlify.svg', as: 'image' }])
}

return head
}
89 changes: 60 additions & 29 deletions docs/guide/extending-matchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Extending Matchers | Guide

# Extending Matchers

Since Vitest is compatible with both Chai and Jest, you can use either the `chai.use` API or `expect.extend`, whichever you prefer.
Since Vitest is compatible with both Chai and Jest, you can use either the [`chai.use`](https://www.chaijs.com/guide/plugins/) API or `expect.extend`, whichever you prefer.

This guide will explore extending matchers with `expect.extend`. If you are interested in Chai's API, check [their guide](https://www.chaijs.com/guide/plugins/).

Expand All @@ -23,46 +23,32 @@ expect.extend({
})
```

If you are using TypeScript, you can extend default `Assertion` interface in an ambient declaration file (e.g: `vitest.d.ts`) with the code below:
If you are using TypeScript, you can extend default `Matchers` interface in an ambient declaration file (e.g: `vitest.d.ts`) with the code below:

::: code-group
```ts [<Version>3.2.0</Version>]
```ts
import 'vitest'

interface CustomMatchers<R = unknown> {
toBeFoo: () => R
}

declare module 'vitest' {
interface Matchers<T = any> extends CustomMatchers<T> {}
}
```
```ts [<Version>3.0.0</Version>]
import 'vitest'

interface CustomMatchers<R = unknown> {
toBeFoo: () => R
}

declare module 'vitest' {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
interface Matchers<T = any> {
toBeFoo: () => R
}
}
```
:::

::: tip
Since Vitest 3.2, you can extend the `Matchers` interface to have type-safe assertions in `expect.extend`, `expect().*`, and `expect.*` methods at the same time. Previously, you had to define separate interfaces for each of them.
Importing `vitest` makes TypeScript think this is an ES module file, type declaration won't work without it.
:::

Extending the `Matchers` interface will add a type to `expect.extend`, `expect().*`, and `expect.*` methods at the same time.

::: warning
Don't forget to include the ambient declaration file in your `tsconfig.json`.
:::

The return value of a matcher should be compatible with the following interface:

```ts
interface ExpectationResult {
interface MatcherResult {
pass: boolean
message: () => string
// If you pass these, they will automatically appear inside a diff when
Expand All @@ -73,7 +59,7 @@ interface ExpectationResult {
```

::: warning
If you create an asynchronous matcher, don't forget to `await` the result (`await expect('foo').toBeFoo()`) in the test itself::
If you create an asynchronous matcher, don't forget to `await` the result (`await expect('foo').toBeFoo()`) in the test itself:

```ts
expect.extend({
Expand All @@ -86,13 +72,46 @@ await expect().toBeAsyncAssertion()
```
:::

The first argument inside a matcher's function is the received value (the one inside `expect(received)`). The rest are arguments passed directly to the matcher.
The first argument inside a matcher's function is the received value (the one inside `expect(received)`). The rest are arguments passed directly to the matcher. Since version 4.1, Vitest exposes several types that can be used by your custom matcher:

```ts
import type {
// the function type
Matcher,
// the return value
MatcherResult,
// state available as `this`
MatcherState,
} from 'vitest'
import { expect } from 'vitest'

// a simple matcher, using "function" to have access to "this"
const customMatcher: Matcher = function (received) {
// ...
}

// a matcher with arguments
const customMatcher: Matcher<MatcherState, [arg1: unknown, arg2: unknown]> = function (received, arg1, arg2) {
// ...
}

// a matcher with custom annotations
function customMatcher(this: MatcherState, received: unknown, arg1: unknown, arg2: unknown): MatcherResult {
// ...
return {
pass: false,
message: () => 'something went wrong!',
}
}

expect.extend({ customMatcher })
```

Matcher function has access to `this` context with the following properties:

### `isNot`

Returns true, if matcher was called on `not` (`expect(received).not.toBeFoo()`).
Returns true, if matcher was called on `not` (`expect(received).not.toBeFoo()`). You do not need to respect it, Vitest will reverse the value of `pass` automatically.

### `promise`

Expand All @@ -112,7 +131,7 @@ This contains a set of utility functions that you can use to display messages.

Full name of the current test (including describe block).

### `task` <Advanced /> <Version type="experimental">4.0.11</Version> {#task}
### `task` <Advanced /> <Version>4.1.0</Version> {#task}

Contains a reference to [the `Test` runner task](/api/advanced/runner#tasks) when available.

Expand All @@ -122,4 +141,16 @@ When using the global `expect` with concurrent tests, `this.task` is `undefined`

### `testPath`

Path to the current test.
File path to the current test.

### `environment`

The name of the current [`environment`](/config/environment) (for example, `jsdom`).

### `soft`

Was assertion called as a [`soft`](/api/expect#soft) one. You don't need to respect it, Vitest will always catch the error.

::: tip
These are not all of the available properties, only the most useful ones. The other state values are used by Vitest internally.
:::
11 changes: 8 additions & 3 deletions docs/guide/ide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
title: IDE Integrations | Guide
---

<script setup>
import { useData } from 'vitepress'
const { isDark } = useData()
</script>

# IDE Integrations

## VS Code <Badge>Official</Badge> {#vs-code}

<p text-center>
<img src="https://raw.githubusercontent.com/vitest-dev/vscode/main/img/cover.png" w-60>
<img :src="`https://raw.githubusercontent.com/vitest-dev/vscode/main/img/cover-${isDark ? 'light' : 'dark' }.png`" w-60>
</p>

[GitHub](https://github.com/vitest-dev/vscode) | [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=vitest.explorer)
Expand All @@ -19,7 +24,7 @@ title: IDE Integrations | Guide
WebStorm, PhpStorm, IntelliJ IDEA Ultimate, and other JetBrains IDEs come with built-in support for Vitest.

<p text-center>
<img src="https://raw.githubusercontent.com/kricact/WS-info/main/banners/vitest-jb.png" w-60>
<img :src="`/ide/vitest-jb-${isDark ? 'light' : 'dark'}.png`" w-60>
</p>

[WebStorm Help](https://www.jetbrains.com/help/webstorm/vitest.html) | [IntelliJ IDEA Ultimate Help](https://www.jetbrains.com/help/idea/vitest.html) | [PhpStorm Help](https://www.jetbrains.com/help/phpstorm/vitest.html)
Expand All @@ -33,7 +38,7 @@ Created by [The Wallaby Team](https://wallabyjs.com)
[Wallaby.js](https://wallabyjs.com) runs your Vitest tests immediately as you type, highlighting results in your IDE right next to your code.

<p text-left>
<img src="https://wallabyjs.com/assets/img/vitest_cover.png" w-142 />
<img :src="`/ide/vitest-wallaby-${isDark ? 'light' : 'dark'}.png`" alt="Vitest + Wallaby logos" w-142>
</p>

[VS Code](https://marketplace.visualstudio.com/items?itemName=WallabyJs.wallaby-vscode) | [JetBrains](https://plugins.jetbrains.com/plugin/15742-wallaby) |
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"tinyglobby": "catalog:",
"unocss": "catalog:",
"vite": "^6.3.5",
"vite-plugin-pwa": "^0.21.2",
"vite-plugin-pwa": "^1.2.0",
"vitepress": "2.0.0-alpha.15",
"vitepress-plugin-group-icons": "^1.6.5",
"vitepress-plugin-llms": "^1.10.0",
Expand Down
Binary file added docs/public/ide/vitest-jb-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/public/ide/vitest-jb-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/public/ide/vitest-wallaby-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/public/ide/vitest-wallaby-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 0 additions & 24 deletions docs/public/logo-shadow.svg

This file was deleted.

15 changes: 0 additions & 15 deletions docs/public/vite.svg

This file was deleted.

13 changes: 0 additions & 13 deletions docs/public/voidzero.svg

This file was deleted.

Loading
Loading