Search code examples
javascriptunit-testingsinoncleartimeout

How to check if clearTimeout was called with sinon.useFakeTimers?


I'm using sinon with fake timers and I want to check if clearTimeout was called with a specific timeout-id.

var clock = sinon.useFakeTimers();
functionUnderTest();
// How can I know if functionUnderTest cleared a specific timeout?

Solution

  • The clock object has sinon's timer related functions, you can spy them and then assert that they were called

    var clock = sinon.useFakeTimers();
    sinon.spy(clock, "clearTimeout");
    
    functionUnderTest();
    
    sinon.assert.calledWith(clock.clearTimeout, 42);