title: How to test set Interval in sinon js
body: <p>I'm trying to write a unit test for <code>setInterval()</code>, but i'm not sure how to spy on the <code>fetchState()</code>.</p>
  
  <p>maincode.js:</p>
  
  <pre><code>var pollStatus = function(interval, killPolling) {
     // Clear Interval if function is called again 
     if (killPolling || StatusPollObj) {
          clearInterval(StatusPollObj);
          StatusPollObj = false;
      }
  
      // Call once before setInterval Starts
      fetchState();
      StatusPollObj = setInterval(function() {
          if(somecondtion_to_check_inactivity) return;
          fetchState();
      }, interval);
  };
  </code></pre>
  
  <p>spec.js</p>
  
  <pre><code> it("state.json setInterval Call",function() {
      this.clock = sinon.useFakeTimers();
      var helper = new state.HELPER();
      var spy = sinon.spy(helper, "fetchState");
  
      helper.pollStatus('80000', false);
      expect(spy.called).to.be.true;
      this.clock.tick(80000);
      expect(spy.called).to.be.true;
  });
  </code></pre>