Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add .toBeCalledTimes and .toHaveBeenNthCalledWith
  • Loading branch information
rickhanlonii committed Mar 18, 2018
commit 7c5adf1ac56121eb9b43ae702f47211541f19b30
10 changes: 6 additions & 4 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ 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,9 +627,10 @@ 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
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
Expand All @@ -640,8 +642,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