Monday 8 May 2017

Sinon useFakeTimers Off by an hour with source code

Using Node, Sinon, and Proxyquire

In my source code:

var DANGER_ALERT_LOCKOUT_TIME = 3 * 60 * 60 * 1000


function _setDangerAlertLockout() {
  if (!lock.dangerAlert) {
    lock.dangerAlert = true;

    setTimeout(()=> {
      lock.dangerAlert = false;
    }, DANGER_ALERT_LOCKOUT_TIME);
  }
}

module.exports = {
  dangerAlert: dangerAlert,
  normalAlert: normalAlert,
  lock: lock
}


In my test:

  it('should set danger alert lock off after time', ()=> {
    clock = sinon.useFakeTimers(new Date().setHours(15));
    clock.tick(3 * 60 * 60 * 1000);
    alerts.lock.normalAlert.should.be.false;
  });

The test fails when I set the clock forward 3 hours, which is the same as DANGER_ALERT_LOCKOUT_TIME. I also tried 3.5 hours and still failure.

But I set the clock forward 4 hours with clock.tick(4 * 60 * 60 * 1000); then the test passes.

Why this discrepancy of one hour and no less?



via dman

No comments:

Post a Comment