Skip to content

Commit

Permalink
Alias parity for all spy matchers (#5826)
Browse files Browse the repository at this point in the history
* Add .toBeCalledTimes and .toHaveBeenNthCalledWith

* Update CHANGELOG.md
  • Loading branch information
rickhanlonii authored Mar 26, 2018
1 parent f31f253 commit abb6321
Show file tree
Hide file tree
Showing 5 changed files with 822 additions and 568 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
* `[jest-mock]` Add tracking of return values in the `mock` property
([#5752](https://github.com/facebook/jest/pull/5752))
* `[jest-mock]` Add tracking of thrown errors in the `mock` property
([5764](https://github.com/facebook/jest/pull/5764))
([#5764](https://github.com/facebook/jest/pull/5764))
* `[expect]`Add nthCalledWith spy matcher
([#5605](https://github.com/facebook/jest/pull/5605))
* `[jest-cli]` Add `isSerial` property that runners can expose to specify that
they can not run in parallel
([#5706](https://github.com/facebook/jest/pull/5706))
* `[expect]` Add `.toBeCalledTimes` and `toHaveBeenNthCalledWith` aliases
([#5826](https://github.com/facebook/jest/pull/5826))
* `[jest-cli]` Interactive Snapshot Mode improvements
([#5864](https://github.com/facebook/jest/pull/5864))

Expand Down
14 changes: 9 additions & 5 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ describe('drinkAll', () => {

### `.toHaveBeenCalledTimes(number)`

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

Use `.toHaveBeenCalledTimes` to ensure that a mock function got called exact
number of times.

Expand Down Expand Up @@ -626,10 +628,12 @@ test('applying to all flavors does mango last', () => {
});
```

### `.nthCalledWith(nthCall, arg1, arg2, ....)`
### `.toHaveBeenNthCalledWith(nthCall, arg1, arg2, ....)`

Also under the alias: `.nthCalledWith(arg1, arg2, ...)`

If you have a mock function, you can use `.nthCalledWith` to test what arguments
it was nth called with. For example, let's say you have a
If you have a mock function, you can use `.toHaveBeenNthCalledWith` to test what
arguments it was nth called with. For example, let's say you have a
`drinkEach(drink, Array<flavor>)` function that applies `f` to a bunch of
flavors, and you want to ensure that when you call it, the first flavor it
operates on is `'lemon'` and the second one is `'octopus'`. You can write:
Expand All @@ -640,8 +644,8 @@ Note that, nth argument must be positive integer starting from 1.
test('drinkEach drinks each drink', () => {
const drink = jest.fn();
drinkEach(drink, ['lemon', 'octopus']);
expect(drink).nthCalledWith(1, 'lemon');
expect(drink).nthCalledWith(2, 'octopus');
expect(drink).toHaveBeenNthCalledWith(1, 'lemon');
expect(drink).toHaveBeenNthCalledWith(2, 'octopus');
});
```

Expand Down
Loading

0 comments on commit abb6321

Please sign in to comment.