Skip to content Skip to sidebar Skip to footer

Sinon Clock.tick Doesn't Advance Time For Settimeout

I am writing a test for an async function which performs a series of tasks and at one point waits for 60 seconds before doing some more tasks. I am trying to use sinon.useFakeTimer

Solution 1:

Make your test function async and await a resolved Promise after advancing your clock to give the Promise callback queued by the await in foo a chance to run before doing your assertion:

const sinon = require('sinon');
const expect = require('chai').expect;

const { foo } = require('./foo');

describe('Module Foo ', function() {
  it('call function after 1 minute', asyncfunction() {  // <= async test functionvar clock = sinon.useFakeTimers();
    const bar = sinon.stub();

    foo(bar);
    expect(bar.called).to.be.false;  // Success!
    clock.tick(100000);
    awaitPromise.resolve();  // <= give queued Promise callbacks a chance to runexpect(bar.called).to.be.true;  // Success!
  });
});

For complete details see my answer here which uses Jest and Jesttimer mocks but the concepts are the same and also apply to Mocha and Sinonfake timers.

Post a Comment for "Sinon Clock.tick Doesn't Advance Time For Settimeout"