diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index f261e3c4bb0f..bf536c0f0d53 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -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 { + 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 { + toBeWithinRange(floor: number, ceiling: number): R; +} +declare global { + namespace jest { + interface Expect extends CustomMatchers {} + interface Matchers extends CustomMatchers {} + interface InverseAsymmetricMatchers extends CustomMatchers {} + } +} +export {}; +``` #### Async Matchers diff --git a/website/versioned_docs/version-25.x/ExpectAPI.md b/website/versioned_docs/version-25.x/ExpectAPI.md index c80f784f6d37..f15881001b4b 100644 --- a/website/versioned_docs/version-25.x/ExpectAPI.md +++ b/website/versioned_docs/version-25.x/ExpectAPI.md @@ -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 { toBeWithinRange(floor: number, ceiling: number): R; } @@ -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 { + 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. diff --git a/website/versioned_docs/version-26.x/ExpectAPI.md b/website/versioned_docs/version-26.x/ExpectAPI.md index c80f784f6d37..f15881001b4b 100644 --- a/website/versioned_docs/version-26.x/ExpectAPI.md +++ b/website/versioned_docs/version-26.x/ExpectAPI.md @@ -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 { toBeWithinRange(floor: number, ceiling: number): R; } @@ -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 { + 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. diff --git a/website/versioned_docs/version-28.x/ExpectAPI.md b/website/versioned_docs/version-28.x/ExpectAPI.md index aa25136d861b..1048f02585e6 100644 --- a/website/versioned_docs/version-28.x/ExpectAPI.md +++ b/website/versioned_docs/version-28.x/ExpectAPI.md @@ -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 { toBeWithinRange(floor: number, ceiling: number): R; } @@ -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 { + toBeWithinRange(a: number, b: number): R; + } + } +} +export {}; +``` #### Async Matchers