Skip to content

Commit

Permalink
docs: add few missing admonitions to the Expect API page (#13892)
Browse files Browse the repository at this point in the history
Co-authored-by: Seong Min Park <psm03120@gmail.com>
  • Loading branch information
mrazauskas and notoriousmango committed Feb 10, 2023
1 parent fa1930d commit ee1895f
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 13 deletions.
4 changes: 4 additions & 0 deletions docs/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

import TypeScriptExamplesNote from './_TypeScriptExamplesNote.md';

<TypeScriptExamplesNote />
Expand Down
28 changes: 24 additions & 4 deletions website/versioned_docs/version-25.x/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

## Methods

import TOCInline from '@theme/TOCInline';
Expand Down Expand Up @@ -67,7 +71,9 @@ 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:
:::info

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

```ts
expect.extend({
Expand Down Expand Up @@ -102,6 +108,8 @@ 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.
Expand Down Expand Up @@ -807,7 +815,11 @@ test('drinkEach drinks each drink', () => {
});
```

Note: the nth argument must be positive integer starting from 1.
:::note

The nth argument must be positive integer starting from 1.

:::

### `.toHaveReturned()`

Expand Down Expand Up @@ -906,7 +918,11 @@ test('drink returns expected nth calls', () => {
});
```

Note: the nth argument must be positive integer starting from 1.
:::note

The nth argument must be positive integer starting from 1.

:::

### `.toHaveLength(number)`

Expand Down Expand Up @@ -1344,7 +1360,11 @@ test('throws on octopus', () => {
});
```

> Note: You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.
:::tip

You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.

:::

You can provide an optional argument to test that a specific error is thrown:

Expand Down
28 changes: 24 additions & 4 deletions website/versioned_docs/version-26.x/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

## Methods

import TOCInline from '@theme/TOCInline';
Expand Down Expand Up @@ -67,7 +71,9 @@ 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:
:::info

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

```ts
expect.extend({
Expand Down Expand Up @@ -102,6 +108,8 @@ 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.
Expand Down Expand Up @@ -807,7 +815,11 @@ test('drinkEach drinks each drink', () => {
});
```

Note: the nth argument must be positive integer starting from 1.
:::note

The nth argument must be positive integer starting from 1.

:::

### `.toHaveReturned()`

Expand Down Expand Up @@ -906,7 +918,11 @@ test('drink returns expected nth calls', () => {
});
```

Note: the nth argument must be positive integer starting from 1.
:::note

The nth argument must be positive integer starting from 1.

:::

### `.toHaveLength(number)`

Expand Down Expand Up @@ -1344,7 +1360,11 @@ test('throws on octopus', () => {
});
```

> Note: You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.
:::tip

You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.

:::

You can provide an optional argument to test that a specific error is thrown:

Expand Down
47 changes: 43 additions & 4 deletions website/versioned_docs/version-27.x/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

## Methods

import TOCInline from '@theme/TOCInline';
Expand Down Expand Up @@ -67,9 +71,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:
:::info

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 +95,21 @@ 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 Expand Up @@ -808,7 +835,11 @@ test('drinkEach drinks each drink', () => {
});
```

Note: the nth argument must be positive integer starting from 1.
:::note

The nth argument must be positive integer starting from 1.

:::

### `.toHaveReturned()`

Expand Down Expand Up @@ -907,7 +938,11 @@ test('drink returns expected nth calls', () => {
});
```

Note: the nth argument must be positive integer starting from 1.
:::note

The nth argument must be positive integer starting from 1.

:::

### `.toHaveLength(number)`

Expand Down Expand Up @@ -1359,7 +1394,11 @@ test('throws on octopus', () => {
});
```

> Note: You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.
:::tip

You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.

:::

You can provide an optional argument to test that a specific error is thrown:

Expand Down
6 changes: 5 additions & 1 deletion website/versioned_docs/version-28.x/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

## Methods

import TOCInline from '@theme/TOCInline';
Expand Down Expand Up @@ -67,7 +71,7 @@ test('numeric ranges', () => {
});
```

:::note
:::info

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

Expand Down
4 changes: 4 additions & 0 deletions website/versioned_docs/version-29.0/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

import TypeScriptExamplesNote from './_TypeScriptExamplesNote.md';

<TypeScriptExamplesNote />
Expand Down
4 changes: 4 additions & 0 deletions website/versioned_docs/version-29.1/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

import TypeScriptExamplesNote from './_TypeScriptExamplesNote.md';

<TypeScriptExamplesNote />
Expand Down
4 changes: 4 additions & 0 deletions website/versioned_docs/version-29.2/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

import TypeScriptExamplesNote from './_TypeScriptExamplesNote.md';

<TypeScriptExamplesNote />
Expand Down
4 changes: 4 additions & 0 deletions website/versioned_docs/version-29.3/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

import TypeScriptExamplesNote from './_TypeScriptExamplesNote.md';

<TypeScriptExamplesNote />
Expand Down
4 changes: 4 additions & 0 deletions website/versioned_docs/version-29.4/ExpectAPI.md
Expand Up @@ -5,8 +5,12 @@ title: Expect

When you're writing tests, you often need to check that values meet certain conditions. `expect` gives you access to a number of "matchers" that let you validate different things.

:::tip

For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended).

:::

import TypeScriptExamplesNote from './_TypeScriptExamplesNote.md';

<TypeScriptExamplesNote />
Expand Down

0 comments on commit ee1895f

Please sign in to comment.