Skip to content

Commit

Permalink
improve extending matchers docs
Browse files Browse the repository at this point in the history
- add satisfies keyword as it improves types
- change ambient declaration file to how it is configured inside [testing lib](https://github.com/testing-library/jest-dom/blob/main/types/vitest.d.ts) since the current way didn't work for me
- rename MatcherResult to ExpectationResult as this is the current name of custom matcher return type according to source code (see MatchersObject > RawMatcherFn > ExpectationResult)
  • Loading branch information
vorant94 committed Jul 14, 2024
1 parent f851982 commit 4d1b587
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions docs/guide/extending-matchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This guide will explore extending matchers with `expect.extend`. If you are inte
To extend default matchers, call `expect.extend` with an object containing your matchers.

```ts
import type { MatchersObject } from '@vitest/expect'

expect.extend({
toBeFoo(received, expected) {
const { isNot } = this
Expand All @@ -20,19 +22,17 @@ expect.extend({
message: () => `${received} is${isNot ? ' not' : ''} foo`
}
}
})
} satisfies MatchersObject)
```

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:

```ts
import type { Assertion, AsymmetricMatchersContaining } from 'vitest'

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

declare module 'vitest' {
declare module '@vitest/expect' {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
Expand All @@ -45,7 +45,7 @@ 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 MatcherResult {
interface ExpectationResult {
pass: boolean
message: () => string
// If you pass these, they will automatically appear inside a diff when
Expand Down

0 comments on commit 4d1b587

Please sign in to comment.