code: function mockTimeout() {
    var events = [];
    var id = 0;
    var now = 0;
  
    return {
      mocks: {setTimeout: mockSetTimeout, clearTimeout: mockClearTimeout},
      flush: flush, get pending() { return events.length; }
    };
  
    function mockSetTimeout(fn, delay) {
      delay = delay || 0;
      events.push({time: now + delay, fn: fn, id: id});
      events.sort(function(a, b) { return a.time - b.time; });
      return id++;
    }
  
    function mockClearTimeout(id) {
      for (var i = 0; i < events.length; ++i) {
        if (events[i].id === id) {
          events.splice(i, 1);
          break;
        }
      }
    }
  
    function flush(delay) {
      if (delay !== undefined)
        now += delay;
      else if (events.length)
        now = events[events.length - 1].time;
      else
        throw new Error('No timer events registered');
  
      while (events.length && events[0].time <= now) {
        events.shift().fn();
      }
    }
  }
docs: setTimeout/clearTimeout mocking, mostly stolen from angular-mocks.js