Skip to content

Commit

Permalink
docs: use admonitions in ExpectAPI.md (#12679)
Browse files Browse the repository at this point in the history
  • Loading branch information
Biki-das authored Apr 21, 2022
1 parent 8436c06 commit 75c7c40
Showing 1 changed file with 25 additions and 5 deletions.
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

0 comments on commit 75c7c40

Please sign in to comment.