If I have a mock function say myMockFunction
I know I can check its calls like so:
const firstCall = myMockFunction.mock.calls[0];
Is there an easy/clean way to get the most recent call?
I think that the following would work:
const mostRecentCall = myMockFunction.mock.calls[myMockFunction.mock.calls.length -1];
This seems rather hacky/tiresome, is there a cleaner/easier way?
If calls is an array and you don't care to preserve its contents, you could do myMockFunction.mock.calls.pop()
to get the last call.
You should be able to access the last element by slicing backwards:
myMockFunction.mock.calls.slice(-1)[0]
Of course, if you don't have any calls yet, you'll get an undefined
. Depending on your tests, that may or may not be an issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With