Skip to content

Commit 21ffcef

Browse files
Alaevthomasjinlo
authored andcommitted
docs and changelog update for nthCalledWith (jestjs#5734)
* docs and changelog update for nthCalledWith * Update CHANGELOG.md
1 parent 2baac96 commit 21ffcef

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
([#5670](https://github.com/facebook/jest/pull/5670))
1717
* `[expect]` Add inverse matchers (`expect.not.arrayContaining`, etc.,
1818
[#5517](https://github.com/facebook/jest/pull/5517))
19+
* `[expect]`Add nthCalledWith spy matcher
20+
([#5605](https://github.com/facebook/jest/pull/5605))
1921
* `[jest-cli]` Add `isSerial` property that runners can expose to specify that
2022
they can not run in parallel
2123
[#5706](https://github.com/facebook/jest/pull/5706)

docs/ExpectAPI.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,25 @@ test('applying to all flavors does mango last', () => {
622622
});
623623
```
624624

625+
### `.nthCalledWith(nthCall, arg1, arg2, ....)`
626+
627+
If you have a mock function, you can use `.nthCalledWith` to test what arguments
628+
it was nth called with. For example, let's say you have a
629+
`drinkEach(drink, Array<flavor>)` function that applies `f` to a bunch of
630+
flavors, and you want to ensure that when you call it, the first flavor it
631+
operates on is `'lemon'` and the second one is `'octopus'`. You can write:
632+
633+
Note that, nth argument must be positive integer starting from 1.
634+
635+
```js
636+
test('drinkEach drinks each drink', () => {
637+
const drink = jest.fn();
638+
drinkEach(drink, ['lemon', 'octopus']);
639+
expect(drink).nthCalledWith(1, 'lemon');
640+
expect(drink).nthCalledWith(2, 'octopus');
641+
});
642+
```
643+
625644
### `.toBeCloseTo(number, numDigits)`
626645

627646
Using exact equality with floating point numbers is a bad idea. Rounding means

0 commit comments

Comments
 (0)