Last Updated: February 25, 2016
·
639
· alanandrade

SinonJs & QUnit timers

If you use QUnit and SinonJS to test your code be careful with the clock.

QUnit's asyncTest will rely on normal timers but Sinon takes control over time by default. Here the proof http://sinonjs.org/docs/#sinon-sandbox

How to fix it? It depends on your needs


// Test that depends on possible real asynchronous stuff
module('Faith', {
  setup: function () { sinon.config.useFakeTimers = false }
});

asyncTest( 'It will execute', 1, function(){
  var faith = new Faith();
  faith.lookUp({
    onComplete: function() {
      ok( true, 'Youre faithful now' );
      start();
    }
  });
});

// Test that depends on time just to pass.
module( 'Human' ); // Don't override defaults
test( 'it gets old' , function () {
   var human = new Human();
  ok( !human.isOld(), 'Human still young!' );
  sinon.clock.tick( 5000000000 );
  ok( human.isOld(), 'Human has got old' );
});