Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: use admonitions in ExpectAPI.md #12679

Merged
merged 2 commits into from
Apr 21, 2022
Merged
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
30 changes: 25 additions & 5 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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:
:::note

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

```ts
interface CustomMatchers<R = unknown> {
Expand All @@ -83,6 +85,8 @@ declare global {
}
```

:::

#### 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 @@ -800,7 +804,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 @@ -899,7 +907,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 @@ -1205,7 +1217,11 @@ describe('the La Croix cans on my desk', () => {
});
```

> Note: `.toEqual` won't perform a _deep equality_ check for two errors. Only the `message` property of an Error is considered for equality. It is recommended to use the `.toThrow` matcher for testing against errors.
:::tip

`.toEqual` won't perform a _deep equality_ check for two errors. Only the `message` property of an Error is considered for equality. It is recommended to use the `.toThrow` matcher for testing against errors.

:::

If differences between properties do not help you to understand why a test fails, especially if the report is large, then you might move the comparison into the `expect` function. For example, use `equals` method of `Buffer` class to assert whether or not buffers contain the same content:

Expand Down Expand Up @@ -1340,7 +1356,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