close

How to check multiple arguments on multiple calls for jest spies?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to check multiple arguments on multiple calls for jest spies? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to check multiple arguments on multiple calls for jest spies ?

  1. How to check multiple arguments on multiple calls for jest spies ?

    to check multiple arguments on multiple calls for jest spies I was able mock multiple calls and check the arguments this way:
    expect(mockFn.mock.calls).toEqual([ [arg1, arg2, ...], // First call [arg1, arg2, ...] // Second call ]);
    where mockFn is your mocked function name.

  2. How to check multiple arguments on multiple calls for jest spies ?

    to check multiple arguments on multiple calls for jest spies I was able mock multiple calls and check the arguments this way:
    expect(mockFn.mock.calls).toEqual([ [arg1, arg2, ...], // First call [arg1, arg2, ...] // Second call ]);
    where mockFn is your mocked function name.

Method 1

I was able mock multiple calls and check the arguments this way:

expect(mockFn.mock.calls).toEqual([
  [arg1, arg2, ...], // First call
  [arg1, arg2, ...]  // Second call
]);

where mockFn is your mocked function name.

Method 2

You can also create an array of the expected arguments per call and loop over it:

const expectedArgs = ['a', 'b', 'c', 'd']
expectedArgs.forEach((arg, index) => 
    expect(myFunc).toHaveBeenNthCalledWith(index + 1, arg))

This solution considers the order of the calls. If you do not care about the order, you can use toHaveBeenCalledWith without the index instead.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment