Sinon: how to create a stub that resolves multiple calls

Viewed 544

It seems straightforward to create a stub that returns different values on repeated calls: Possible to stub method twice within a single test to return different results?

But, I how does one do this with an async method that resolves on multiple calls?

sinon(module, "myFunction")
  .resolves('a')
  .resolves('b')

To be clear, in the fragment above, the second resolves overwrites the first, so it always returns 'b'. I'd like the behavior where 'myFunction' first resolves 'a' and then resolves 'b'.

1 Answers

Here's the solution:

sinon(module, "myFunction")
  .onFirstCall().resolves('a')
  .onSecondCall().resolves('b')
Related