Skip to content

Commit f55f3d8

Browse files
UselessPicklescpojer
authored andcommitted
[expect] Improve mock "return" matchers to properly handle calls that throw (#6172)
* Improve mock "return" matchers to properly handle calls that throw * Fix unit test that was broken because expect().toThrow() does not proeprly detect a thrown value of undefined. * update CHANGELOG * Fix comment typo * Simplify structure of how mock function results are maintained.
1 parent 9867e16 commit f55f3d8

File tree

8 files changed

+675
-98
lines changed

8 files changed

+675
-98
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
([#6181](https://github.com/facebook/jest/pull/6181))
9393
* `[expect]` Include custom mock names in error messages
9494
([#6199](https://github.com/facebook/jest/pull/6199))
95+
* `[expect]` Improve return matchers
96+
([#6172](https://github.com/facebook/jest/pull/6172))
9597

9698
### Fixes
9799

docs/ExpectAPI.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,9 @@ Note: the nth argument must be positive integer starting from 1.
683683
Also under the alias: `.toReturn()`
684684

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

689690
```js
690691
test('drinks returns', () => {
@@ -700,9 +701,13 @@ test('drinks returns', () => {
700701

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

703-
Use `.toHaveReturnedTimes` to ensure that a mock function returned an exact
704-
number of times. For example, let's say you have a mock `drink` that returns
705-
`true`. You can write:
704+
Use `.toHaveReturnedTimes` to ensure that a mock function returned successfully
705+
(i.e., did not throw an error) an exact number of times. Any calls to the mock
706+
function that throw an error are not counted toward the number of times the
707+
function returned.
708+
709+
For example, let's say you have a mock `drink` that returns `true`. You can
710+
write:
706711

707712
```js
708713
test('drink returns twice', () => {
@@ -741,7 +746,9 @@ test('drink returns La Croix', () => {
741746
Also under the alias: `.lastReturnedWith(value)`
742747

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

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

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

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

docs/MockFunctionAPI.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Returns the mock name string set by calling `mockFn.mockName(value)`.
2222

2323
### `mockFn.mock.calls`
2424

25-
An array that represents all calls that have been made into this mock function.
26-
Each call is represented by an array of arguments that were passed during the
27-
call.
25+
An array containing the call arguments of all calls that have been made to this
26+
mock function. Each item in the array is an array of arguments that were passed
27+
during the call.
2828

2929
For example: A mock function `f` that has been called twice, with the arguments
3030
`f('arg1', 'arg2')`, and then with the arguments `f('arg3', 'arg4')`, would have
@@ -34,33 +34,35 @@ a `mock.calls` array that looks like this:
3434
[['arg1', 'arg2'], ['arg3', 'arg4']];
3535
```
3636

37-
### `mockFn.mock.returnValues`
37+
### `mockFn.mock.results`
3838

39-
An array containing values that have been returned by all calls to this mock
40-
function. For any call to the mock that throws an error, a value of `undefined`
41-
will be stored in `mock.returnValues`.
39+
An array containing the results of all calls that have been made to this mock
40+
function. Each entry in this array is an object containing a boolean `isThrow`
41+
property, and a `value` property. `isThrow` is true if the call terminated due
42+
to a `throw`, or false if the the call returned normally. The `value` property
43+
contains the value that was thrown or returned.
4244

4345
For example: A mock function `f` that has been called three times, returning
4446
`result1`, throwing an error, and then returning `result2`, would have a
45-
`mock.returnValues` array that looks like this:
47+
`mock.results` array that looks like this:
4648

4749
```js
48-
['result1', undefined, 'result2'];
49-
```
50-
51-
### `mockFn.mock.thrownErrors`
52-
53-
An array containing errors that have been thrown by all calls to this mock
54-
function.
55-
56-
For example: A mock function `f` that has been called twice, throwing an
57-
`Error`, and then executing successfully without an error, would have the
58-
following `mock.thrownErrors` array:
59-
60-
```js
61-
f.mock.thrownErrors.length === 2; // true
62-
f.mock.thrownErrors[0] instanceof Error; // true
63-
f.mock.thrownErrors[1] === undefined; // true
50+
[
51+
{
52+
isThrow: false,
53+
value: 'result1',
54+
},
55+
{
56+
isThrow: true,
57+
value: {
58+
/* Error instance */
59+
},
60+
},
61+
{
62+
isThrow: false,
63+
value: 'result2',
64+
},
65+
];
6466
```
6567

6668
### `mockFn.mock.instances`

0 commit comments

Comments
 (0)