244

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

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

0

7 Answers 7

367

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

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

Here's the documentation about the mock property

6
  • Other than mock function, is there any other way to get the arguments of the function in jest?
    – Coder
    Commented Aug 27, 2018 at 11:55
  • 13
    Thank you! To others and my future self: If you get a compilation error Tuple type '[]' of length '0' has no element at index '0'. on the second 0, you can get around it by using const call = (mockUpload.mock.calls[0] as any[])[0];. Commented Dec 13, 2019 at 23:59
  • Somehow Jest tries to refine the types when you pass an implementation like jest.fn(() => "foobar"), but not when you just use jest.fn(), in this case calls is of type any (instead of being an array potentially empty).
    – Eric Burel
    Commented Jun 14, 2021 at 13:08
  • 4
    Digged the types a bit more: the right way in TypeScript is jest.fn((args) => "foobar"): explicitely defining args will results in a better typings, and mock.calls[0][0] will be accepted by TypeScript. This is because if you use 2 args in your mock, mock.calls[0] is an array of length 2. If you use 1 arg, length 1, and 0 args => length 0. So in your mock just use the same number of arguments as your function is expecting, even if you don't use them.
    – Eric Burel
    Commented Jun 14, 2021 at 13:12
  • 2
    Regarding the Tuple type '[]' of length '0' has no element at index '0'. error: in my case, I had typed my mock using SpyInstance without understanding the two generic parameters for that type. SpyInstance<R, A> takes the return type R and an array of argument types A. Specifying a type of SpyInstance<unknown, [...unknown[]]> allowed me to get any number of arguments from the calls of my mock without any errors. Commented Nov 13, 2021 at 4:46
88

Here is a simple way to assert the parameter passed.

expect(mockedFunction).toHaveBeenCalledWith("param1","param2");
4
  • 1
    what if the second argument is a function that I have no access?
    – Hugo Licon
    Commented Feb 11, 2020 at 21:59
  • 4
    In that case, go with this approach mockUpload.mock.calls[0] Commented Feb 12, 2020 at 4:47
  • 5
    Same as other answer. This does not answer the question. He is not asking how to CHECK what argument it was called with, but rather GET that argument so he can inspect it. Commented Oct 20, 2020 at 12:47
  • Agreed. Mostly, we might need to check the mocked values are being passed as part of inspection, Its more readable, than calls[0][1]., Commented Apr 21, 2022 at 22:05
28

You can use toHaveBeenCalledWith() together with expect.stringContaining or expect.arrayContaining() or expect.objectContaining()

...
const { host } = new URL(url);
expect(mockedFunction).toHaveBeenCalledWith("param1", expect.stringContaining(`http://${host}...`);
1
  • 6
    Thank you so much! This combination was exactly what I was looking for. Also, if you do not care about the first or second parameter, you can use expect.anything() on it.
    – soutoner
    Commented Jan 13, 2022 at 11:57
8

Use an argument captor, something like this:

let barArgCaptor;
const appendChildMock = jest
    .spyOn(foo, "bar")
    .mockImplementation(
    (arg) => (barArgCaptor = arg)
    );

Then you could expect on the captor's properties to your heart's content:

expect(barArgCaptor.property1).toEqual("Fool of a Took!");
expect(barArgCaptor.property2).toEqual(true);
0
5

I find I often want to validate the exact arguments of exact calls to my mock functions; my approach is:

let mockFn = jest.fn((/* ... */) => { /* ... */ });

// ... do some testing which triggers `mockFn` ...

expect(mockFn.mock.calls).toEqual([

  // Ensure `mockFn` was called exactly 3 times:
  
  // The 1st time with arguments "a" and 1,
  [ 'a', 1 ],

  // 2nd time with arguments "b" and 2,
  [ 'b', 2 ],
  
  // 3rd time with arguments "c" and 3
  [ 'c', 3 ]

]);

4

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')
2
  • 10
    This does not answer the question. He is not asking how to CHECK what argument it was called with, but rather GET that argument so he can inspect it. Commented Oct 20, 2020 at 12:46
  • 4
    @JaviMarzán I came from google because I wanted to know how to assert the function arguments. People who come to this question may not necessarily want to inspect the arguments, but to get the argument and assert it. That's why I leave the answer here in the hope of helping others who have similar problem. Commented Oct 20, 2020 at 13:04
0

we can add a mockImplementation to the mock method and have it return the arguments back which can be used to evaluate.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – jasie
    Commented Aug 16, 2022 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.