115
votes

How can I get the arguments called in jest mock function?

I want to inspect the object that is passed as argument.

3
If I discover how resolve the question what is the problem? In this question I don't put code because I didn't know how start. That was the question purpose.Bruno Quaresma
@PLATANIUM umm what? First of all he is free to answer his own question given the site actually allows him to do that. 2nd, his answer is correct.Martin Dawson

3 Answers

196
votes

Just use mockObject.calls. In my case I used:

const call = mockUpload.mock.calls[0][0]

Here's the documentation about the mock property

54
votes

Here is a simple way to assert the parameter passed.

expect(mockedFunction).toHaveBeenCalledWith("param1","param2");
0
votes

I prefer lastCalledWith() over toHaveBeenCalledWith(). They are both the same but the former is shorter and help me reduce the cognitive load when reading code.

expect(mockedFn).lastCalledWith('arg1', 'arg2')