How can I test sequence of function calls by Sinon.js?

http://sinonjs.org/docs/

sinon.assert.callOrder(spy1, spy2, ...)

Passes if the provided spies where called in the specified order.


For people who run into this looking for a way to define stubs with a specified behaviour on a call sequence. There is no direct way (as far as I've seen) to say: "This stub will be called X times, on the 1st call it will take parameter a and return b on the 2nd call it will take parameter c and return d ..." and so on.

The closest I found to this behaviour is:

  const expectation = sinon.stub()
            .exactly(N)
            .onCall(0)
            .returns(b)
            .onCall(1)
            .returns(d)
            // ...
            ;
   // Test that will end up calling the stub
   // expectation.callCount should be N
   // expectation.getCalls().map(call => call.args) should be [ a, b, ... ]

This way we can return specific values on each call in the sequence and then assert that the calls were made with the parameters we expected.