Skip to content

Commit

Permalink
docs: update extend matcher (#3123)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Sheremet <sleuths.slews0s@icloud.com>
  • Loading branch information
bodinsamuel and sheremet-va committed Apr 5, 2023
1 parent c3cfa0e commit 74b7ab0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 8 additions & 6 deletions docs/api/expect.md
Expand Up @@ -1275,23 +1275,25 @@ type Awaitable<T> = T | PromiseLike<T>

This function is compatible with Jest's `expect.extend`, so any library that uses it to create custom matchers will work with Vitest.

If you are using TypeScript, you can extend default Matchers interface with the code bellow:
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:

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

declare global {
namespace Vi {
interface Assertion extends CustomMatchers {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
declare namespace Vi {
interface Assertion extends CustomMatchers {}
interface AsymmetricMatchersContaining extends CustomMatchers {}

// Note: augmenting jest.Matchers interface will also work.
}
```

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

:::tip
If you want to know more, checkout [guide on extending matchers](/guide/extending-matchers).
:::
14 changes: 8 additions & 6 deletions docs/guide/extending-matchers.md
Expand Up @@ -23,23 +23,25 @@ expect.extend({
})
```

If you are using TypeScript, you can extend default Matchers interface with the code bellow:
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:

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

declare global {
namespace Vi {
interface Assertion extends CustomMatchers {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
declare namespace Vi {
interface Assertion extends CustomMatchers {}
interface AsymmetricMatchersContaining extends CustomMatchers {}

// Note: augmenting jest.Matchers interface will also work.
}
```

::: 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
Expand Down

0 comments on commit 74b7ab0

Please sign in to comment.