HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howcallsjestcheckforspiesmultiplearguments

Problem

I have the following function in a React component:

onUploadStart(file, xhr, formData) {
  formData.append('filename', file.name);
  formData.append('mimeType', file.type);
}


This is my test that at least gets the spy to be called:

const formData = { append: jest.fn() };
const file = { name: 'someFileName', type: 'someMimeType' };
eventHandlers.onUploadStart(file, null, formData);

expect(formData.append).toHaveBeenCalledWith(
  ['mimeType', 'someMimeType'],
  ['fileName', 'someFileName']
);


However, the assertion is not working:

Expected mock function to have been called with:
 [["mimeType", "someMimeType"], ["fileName", "someFileName"]]
But it was called with:
  ["mimeType", "someMimeType"], ["filename", "someFileName"]


What is the right way to use toHaveBeenCalledWith?

Solution

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.

Context

Stack Overflow Q#40018216, score: 292

Revisions (0)

No revisions yet.