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
35 changes: 34 additions & 1 deletion docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,40 @@ expect.extend({
});
```

:::
:::note

In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this:

```ts title="toBeWithinRange.ts"
expect.extend({
toBeWithinRange(received: number, floor: number, ceiling: number) {
// ...
},
});
declare global {
namespace jest {
interface Matchers<R> {
toBeWithinRange(a: number, b: number): R;
}
}
}
```

If you want to move the typings to a separate file (e.g. `types/jest/index.d.ts`), you may need to an export, e.g.:

```ts
interface CustomMatchers<R = unknown> {
toBeWithinRange(floor: number, ceiling: number): R;
}
declare global {
namespace jest {
interface Expect extends CustomMatchers {}
interface Matchers<R> extends CustomMatchers<R> {}
interface InverseAsymmetricMatchers extends CustomMatchers {}
}
}
export {};
```

#### Async Matchers

Expand Down
19 changes: 19 additions & 0 deletions website/versioned_docs/version-25.x/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ test('numeric ranges', () => {
_Note_: In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this:

```ts
expect.extend({
toBeWithinRange(received, floor, ceiling) {
// ...
},
});

interface CustomMatchers<R = unknown> {
toBeWithinRange(floor: number, ceiling: number): R;
}
Expand All @@ -83,6 +89,19 @@ declare global {
}
```

If you want to move the typings to a separate file (e.g. `types/jest/index.d.ts`), you may need to an export, e.g.:

```ts
declare global {
namespace jest {
interface Matchers<R> {
toBeWithinRange(a: number, b: number): R;
}
}
}
export {};
```

#### Async Matchers

`expect.extend` also supports async matchers. Async matchers return a Promise so you will need to await the returned value. Let's use an example matcher to illustrate the usage of them. We are going to implement a matcher called `toBeDivisibleByExternalValue`, where the divisible number is going to be pulled from an external source.
Expand Down
19 changes: 19 additions & 0 deletions website/versioned_docs/version-26.x/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ test('numeric ranges', () => {
_Note_: In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this:

```ts
expect.extend({
toBeWithinRange(received, floor, ceiling) {
// ...
},
});

interface CustomMatchers<R = unknown> {
toBeWithinRange(floor: number, ceiling: number): R;
}
Expand All @@ -83,6 +89,19 @@ declare global {
}
```

If you want to move the typings to a separate file (e.g. `types/jest/index.d.ts`), you may need to an export, e.g.:

```ts
declare global {
namespace jest {
interface Matchers<R> {
toBeWithinRange(a: number, b: number): R;
}
}
}
export {};
```

#### Async Matchers

`expect.extend` also supports async matchers. Async matchers return a Promise so you will need to await the returned value. Let's use an example matcher to illustrate the usage of them. We are going to implement a matcher called `toBeDivisibleByExternalValue`, where the divisible number is going to be pulled from an external source.
Expand Down
21 changes: 20 additions & 1 deletion website/versioned_docs/version-28.x/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ test('numeric ranges', () => {

In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this:

:::

```ts
expect.extend({
toBeWithinRange(received, floor, ceiling) {
// ...
},
});

interface CustomMatchers<R = unknown> {
toBeWithinRange(floor: number, ceiling: number): R;
}
Expand All @@ -85,7 +93,18 @@ declare global {
}
```

:::
If you want to move the typings to a separate file (e.g. `types/jest/index.d.ts`), you may need to an export, e.g.:

```ts
declare global {
namespace jest {
interface Matchers<R> {
toBeWithinRange(a: number, b: number): R;
}
}
}
export {};
```

#### Async Matchers

Expand Down