Last Updated: February 25, 2016
·
861
· 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' );
});

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside