Categories
JavaScript Answers

How to get the arguments called in a Jest mock function with JavaScript?

Spread the love

Sometimes, we want to get the arguments called in a Jest mock function with JavaScript.

In this article, we’ll look at how to get the arguments called in a Jest mock function with JavaScript.

How to get the arguments called in a Jest mock function with JavaScript?

To get the arguments called in a Jest mock function with JavaScript, we call toHaveBeenCalledWith.

For instance, we write

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

to call expect with the function we mocked.

And then we call toHaveBeenCalledwith with the arguments that we want to check the mockedFunction is called with.

This is done in our test callback.

Conclusion

To get the arguments called in a Jest mock function with JavaScript, we call toHaveBeenCalledWith.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.