Search code examples
javascriptjasmineexpectations

Jasmine : spy on a function called X times, and get the n-th call


I can't seem to find a solution online.

Here is a sample of code so you get the issue :

// Spy on the wanted function
spyOn(object, 'myFunction');

// Call it 3 times with different parameters
object.myFunction('');
object.myFunction('', 0);
object.myFunction('', 0, true);

// Now all of these expects work
expect(object.myFunction).toHaveBeenCalledTimes(3);
expect(object.myFunction).toHaveBeenCalledWith('', 0);
expect(object.myFunction).toHaveBeenCalledWith('');
expect(object.myFunction).toHaveBeenCalledWith('', 0, true);

I would like to test if every call was correctly made. Is there a way to say something like this ?

expect(object.myFunction).nthCall(2).toHaveBeenCalledWith('', 0, true);

???


Solution

  • There's calls property, you can use like: expect(object.myFunction.calls.argsFor(2)).toEqual(['', 0, true])