From 0d14a2139a1277605cc23c8dbfe61643059e1c62 Mon Sep 17 00:00:00 2001 From: Biki-das Date: Sat, 24 Sep 2022 14:51:00 +0530 Subject: [PATCH 1/5] Added typed info for docs --- docs/ExpectAPI.md | 39 +++++++++++++++++++ .../versioned_docs/version-25.x/ExpectAPI.md | 23 ++++++++++- .../versioned_docs/version-26.x/ExpectAPI.md | 21 +++++++++- .../versioned_docs/version-28.x/ExpectAPI.md | 23 ++++++++++- 4 files changed, 103 insertions(+), 3 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index f261e3c4bb0f..89a4150ac9cc 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -183,6 +183,7 @@ test('asymmetric ranges', () => { }); ``` + :::tip The type declaration of the matcher can live in a `.d.ts` file or in an imported `.ts` module (see JS and TS examples above respectively). If you keep the declaration in a `.d.ts` file, make sure that it is included in the program and that it is a valid module, i.e. it has at least an empty `export {}`. @@ -205,6 +206,44 @@ expect.extend({ ::: +:::note + +In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this (`toBeWithinRange.ts`): + +```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 `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-25.x/ExpectAPI.md b/website/versioned_docs/version-25.x/ExpectAPI.md index c80f784f6d37..ad6e7b99da3c 100644 --- a/website/versioned_docs/version-25.x/ExpectAPI.md +++ b/website/versioned_docs/version-25.x/ExpectAPI.md @@ -67,9 +67,17 @@ 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: +_Note_: + +In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this (`toBeWithinRange.ts`): ```ts +expect.extend({ + toBeWithinRange(received, floor, ceiling) { + // ... + }, +}); + interface CustomMatchers { toBeWithinRange(floor: number, ceiling: number): R; } @@ -83,6 +91,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..a74ed145634c 100644 --- a/website/versioned_docs/version-26.x/ExpectAPI.md +++ b/website/versioned_docs/version-26.x/ExpectAPI.md @@ -67,9 +67,15 @@ 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: +_Note_: In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this (`toBeWithinRange.ts`): ```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..0b2cf6c12580 100644 --- a/website/versioned_docs/version-28.x/ExpectAPI.md +++ b/website/versioned_docs/version-28.x/ExpectAPI.md @@ -69,9 +69,17 @@ 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: +In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this (`toBeWithinRange.ts`): + +::: ```ts +expect.extend({ + toBeWithinRange(received, floor, ceiling) { + // ... + }, +}); + interface CustomMatchers { toBeWithinRange(floor: number, ceiling: number): R; } @@ -85,6 +93,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 From d49063f9ae3e0809334df06097db747fef436c41 Mon Sep 17 00:00:00 2001 From: Biki-das Date: Sat, 24 Sep 2022 14:53:56 +0530 Subject: [PATCH 2/5] removed ::: --- docs/ExpectAPI.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index 89a4150ac9cc..88869dfd28aa 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -183,7 +183,6 @@ test('asymmetric ranges', () => { }); ``` - :::tip The type declaration of the matcher can live in a `.d.ts` file or in an imported `.ts` module (see JS and TS examples above respectively). If you keep the declaration in a `.d.ts` file, make sure that it is included in the program and that it is a valid module, i.e. it has at least an empty `export {}`. @@ -204,8 +203,6 @@ expect.extend({ }); ``` -::: - :::note In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this (`toBeWithinRange.ts`): @@ -243,7 +240,6 @@ 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. From 7f6fb98c5b4d6537f308f7474042ea7f6f8b4db6 Mon Sep 17 00:00:00 2001 From: Biki-das Date: Sat, 24 Sep 2022 14:57:50 +0530 Subject: [PATCH 3/5] Final cleanup --- docs/ExpectAPI.md | 2 +- website/versioned_docs/version-28.x/ExpectAPI.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index 88869dfd28aa..0750593c7021 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -238,7 +238,7 @@ declare global { export {}; ``` -::: + #### Async Matchers diff --git a/website/versioned_docs/version-28.x/ExpectAPI.md b/website/versioned_docs/version-28.x/ExpectAPI.md index 0b2cf6c12580..9fd519f5a448 100644 --- a/website/versioned_docs/version-28.x/ExpectAPI.md +++ b/website/versioned_docs/version-28.x/ExpectAPI.md @@ -106,7 +106,7 @@ declare global { export {}; ``` -::: + #### Async Matchers From 198784eeed655df65aae364b6dc4051c10f25b97 Mon Sep 17 00:00:00 2001 From: Biki-das Date: Sat, 24 Sep 2022 15:05:17 +0530 Subject: [PATCH 4/5] Prettier fix and small changes --- docs/ExpectAPI.md | 4 +--- website/versioned_docs/version-25.x/ExpectAPI.md | 4 +--- website/versioned_docs/version-26.x/ExpectAPI.md | 2 +- website/versioned_docs/version-28.x/ExpectAPI.md | 4 +--- 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index 0750593c7021..bf536c0f0d53 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -205,7 +205,7 @@ expect.extend({ :::note -In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this (`toBeWithinRange.ts`): +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({ @@ -238,8 +238,6 @@ declare global { 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-25.x/ExpectAPI.md b/website/versioned_docs/version-25.x/ExpectAPI.md index ad6e7b99da3c..780e31de2ce6 100644 --- a/website/versioned_docs/version-25.x/ExpectAPI.md +++ b/website/versioned_docs/version-25.x/ExpectAPI.md @@ -67,9 +67,7 @@ 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 (`toBeWithinRange.ts`): +_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({ diff --git a/website/versioned_docs/version-26.x/ExpectAPI.md b/website/versioned_docs/version-26.x/ExpectAPI.md index a74ed145634c..f15881001b4b 100644 --- a/website/versioned_docs/version-26.x/ExpectAPI.md +++ b/website/versioned_docs/version-26.x/ExpectAPI.md @@ -67,7 +67,7 @@ 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 (`toBeWithinRange.ts`): +_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({ diff --git a/website/versioned_docs/version-28.x/ExpectAPI.md b/website/versioned_docs/version-28.x/ExpectAPI.md index 9fd519f5a448..1048f02585e6 100644 --- a/website/versioned_docs/version-28.x/ExpectAPI.md +++ b/website/versioned_docs/version-28.x/ExpectAPI.md @@ -69,7 +69,7 @@ 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 (`toBeWithinRange.ts`): +In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this: ::: @@ -106,8 +106,6 @@ declare global { 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. From 817d1efabfad04a7848bb785cc9bf8b8e7e1404d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 28 Sep 2022 08:47:10 +0200 Subject: [PATCH 5/5] Update website/versioned_docs/version-25.x/ExpectAPI.md --- website/versioned_docs/version-25.x/ExpectAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/versioned_docs/version-25.x/ExpectAPI.md b/website/versioned_docs/version-25.x/ExpectAPI.md index 780e31de2ce6..f15881001b4b 100644 --- a/website/versioned_docs/version-25.x/ExpectAPI.md +++ b/website/versioned_docs/version-25.x/ExpectAPI.md @@ -67,7 +67,7 @@ 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: +_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({