You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 41f551b
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/api/expect.md
+17-14Lines changed: 17 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,11 @@ type Awaitable<T> = T | PromiseLike<T>
9
9
`expect` is used to create assertions. In this context `assertions` are functions that can be called to assert a statement. Vitest provides `chai` assertions by default and also `Jest` compatible assertions built on top of `chai`. Since Vitest 4.1, for spy/mock testing, Vitest also provides Chai-style assertions (e.g., [`expect(spy).to.have.been.called()`](#called)) alongside Jest-style assertions (e.g., `expect(spy).toHaveBeenCalled()`). Unlike `Jest`, Vitest supports a message as the second argument - if the assertion fails, the error message will be equal to it.
@@ -2263,19 +2265,20 @@ If you want your matchers to appear in every test, you should call this method i
2263
2265
2264
2266
This function is compatible with Jest's `expect.extend`, so any library that uses it to create custom matchers will work with Vitest.
2265
2267
2266
-
If you are using TypeScript, since Vitest 0.31.0 you can extend default `Assertion` interface in an ambient declaration file (e.g: `vitest.d.ts`) with the code below:
2268
+
If you are using TypeScript, you can extend the default `Matchers` interface in an ambient declaration file (e.g: `vitest.d.ts`) with the code below:
Copy file name to clipboardExpand all lines: docs/guide/extending-matchers.md
+33-9Lines changed: 33 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ To extend default matchers, call `expect.extend` with an object containing your
12
12
13
13
```ts
14
14
expect.extend({
15
-
toBeFoo(received, expected) {
15
+
toBeFoo(received) {
16
16
const { isNot } =this
17
17
return {
18
18
// do not alter your "pass" based on isNot. Vitest does it for you
@@ -29,12 +29,24 @@ If you are using TypeScript, you can extend default `Matchers` interface in an a
29
29
import'vitest'
30
30
31
31
declaremodule'vitest' {
32
-
interfaceMatchers<T=any> {
32
+
interfaceMatchers<R, T> {
33
33
toBeFoo: () =>R
34
34
}
35
35
}
36
36
```
37
37
38
+
`R` is the assertion return type, and `T` is the type of the received value.
39
+
40
+
Return `R` from matchers that run synchronously. This makes the return type `void` for a regular assertion and `Promise<void>` when the assertion is used with `.resolves`, `.rejects`, [`expect.poll`](/api/expect#poll), or [`expect.element`](/api/browser/assertions). You can use `T` when an expected argument should have the same type as the received value:
41
+
42
+
```ts
43
+
declaremodule'vitest' {
44
+
interfaceMatchers<R, T> {
45
+
toEqualTyped: (expected:T) =>R
46
+
}
47
+
}
48
+
```
49
+
38
50
::: tip
39
51
Importing `vitest` makes TypeScript think this is an ES module file, type declaration won't work without it.
40
52
:::
@@ -45,30 +57,42 @@ Extending the `Matchers` interface will add a type to `expect.extend`, `expect()
45
57
Don't forget to include the ambient declaration file in your `tsconfig.json`.
46
58
:::
47
59
48
-
The return value of a matcher should be compatible with the following interface:
60
+
The return value of a matcher should be compatible with the following types:
49
61
50
62
```ts
51
-
interfaceMatcherResult {
63
+
interfaceSyncMatcherResult {
52
64
pass:boolean
53
65
message: () =>string
54
66
// If you pass these, they will automatically appear inside a diff when
55
67
// the matcher does not pass, so you don't need to print the diff yourself
The underlying [Chai assertion](https://www.chaijs.com/guide/plugins/) object. This is the same instance that Chai plugins receive, giving you access to Chai's flag system and chainable methods. This can be useful for building custom matchers that need to interact with Chai's internals.
### Assertion Types Expose Return and Received Types
229
+
230
+
Assertion interfaces now use two type parameters: `R` is the matcher return type and `T` is the received value type. Synchronous assertions use `void`, while assertions accessed through `.resolves`, `.rejects`, [`expect.poll`](/api/expect#poll), or [`expect.element`](/api/browser/assertions) use `Promise<void>`.
231
+
232
+
If you declare custom matchers, augment the `Matchers<R, T>` interface. It adds the matcher to instance assertions, asymmetric matchers, and the type accepted by `expect.extend`:
Vitest no longer reads custom matcher declarations from the global `jest.Matchers` interface. Libraries that support both Jest and Vitest should augment `jest.Matchers` and `vitest.Matchers` separately. This only affects TypeScript declarations; registering matchers with `expect.extend` works as before.
264
+
228
265
### `expect.poll` Fails When It Times Out
229
266
230
267
[`expect.poll`](/api/expect#poll) now rejects when its callback, or the polled assertion, does not settle within `timeout`. Previously a callback that resolved after the deadline, or an assertion that only passed on a late attempt, could still succeed. The callback now also receives an `AbortSignal` that aborts when the timeout elapses, so you can cancel in-flight work:
@@ -298,7 +298,7 @@ File snapshot matchers must be `async` — `toMatchFileSnapshot` returns a `Prom
298
298
:::
299
299
300
300
::: warning
301
-
When custom inline snapshot matcher is aynchronous, Vitest cannot automatically infer the call location for inline snapshot rewriting. You must capture the call site by setting the `'error'` flag on the chai assertion object:
301
+
When custom inline snapshot matcher is asynchronous, Vitest cannot automatically infer the call location for inline snapshot rewriting. You must capture the call site by setting the `'error'` flag on the chai assertion object:
302
302
303
303
```ts
304
304
import { expect, chai, Snapshots } from'vitest'
@@ -317,16 +317,16 @@ expect.extend({
317
317
318
318
:::
319
319
320
-
For TypeScript, extend the `Assertion` interface:
320
+
For TypeScript, augment the `Matchers<R, T>` interface:
thrownewError(`Invalid element or locator: ${elementOrLocator}. Expected an instance of HTMLElement, SVGElement or Locator, received ${getType(elementOrLocator)}`)
0 commit comments