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

[expect] Improve mock "return" matchers to properly handle calls that throw #6172

Merged
merged 7 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
([#6032](https://github.com/facebook/jest/pull/6032))
* `[expect]` Add return matchers
([#5879](https://github.com/facebook/jest/pull/5879))
* `[expect]` Improve return matchers
([#6172](https://github.com/facebook/jest/pull/6172))

### Fixes

Expand Down
21 changes: 15 additions & 6 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,9 @@ Note: the nth argument must be positive integer starting from 1.
Also under the alias: `.toReturn()`

If you have a mock function, you can use `.toHaveReturned` to test that the mock
function returned a value. For example, let's say you have a mock `drink` that
returns `true`. You can write:
function successfully returned (i.e., did not throw an error) at least one time.
For example, let's say you have a mock `drink` that returns `true`.
You can write:

```js
test('drinks returns', () => {
Expand All @@ -700,8 +701,12 @@ test('drinks returns', () => {

Also under the alias: `.toReturnTimes(number)`

Use `.toHaveReturnedTimes` to ensure that a mock function returned an exact
number of times. For example, let's say you have a mock `drink` that returns
Use `.toHaveReturnedTimes` to ensure that a mock function returned successfully
(i.e., did not throw an error) an exact number of times. Any calls to the mock
function that throw an error are not counted toward the number of times the
function returned.

For example, let's say you have a mock `drink` that returns
`true`. You can write:

```js
Expand Down Expand Up @@ -741,7 +746,9 @@ test('drink returns La Croix', () => {
Also under the alias: `.lastReturnedWith(value)`

Use `.toHaveLastReturnedWith` to test the specific value that a mock function
last returned.
last returned. If the last call to the mock function threw an error, then this
matcher will fail no matter what value you provided as the expected return
value.

For example, let's say you have a mock `drink` that returns the name of the
beverage that was consumed. You can write:
Expand All @@ -764,7 +771,9 @@ test('drink returns La Croix (Orange) last', () => {
Also under the alias: `.nthReturnedWith(nthCall, value)`

Use `.toHaveNthReturnedWith` to test the specific value that a mock function
returned for the nth call.
returned for the nth call. If the nth call to the mock function threw an error,
then this matcher will fail no matter what value you provided as the expected
return value.

For example, let's say you have a mock `drink` that returns the name of the
beverage that was consumed. You can write:
Expand Down
Loading