title: How to write a test case for a setInterval() function
body: <p>I have a timer and assume a specific function will be executed when the counter is counting to 3.</p>
  
  <pre class="lang-js prettyprint-override"><code>var a_interval_function = function(){
      var counter = 1;
      var interval = setInterval(function(){
          if(counter === 5){
              clearInterval(interval);
          }
  
          // run the function when the counter is 3
          if(counter === 3){
              a_function_should_be_runned();
          }   
  
          counter++;
      }, 500);
  
      return interval;
  }
  </code></pre>
  
  <p>However, I don't know how to establish a valid test case for testing the counter as well as the timing when the function is executed. Does anyone know how to do it? Something like the following:</p>
  
  <pre class="lang-js prettyprint-override"><code>// and some test case like this
  it('a timer test', function(done){
      var interval = a_interval_function();
      expect(a_function_should_be_runned.state).to.equal({
          name: 'runned',
          counter: 3,
          time: 300,
      });
  });
  </code></pre>
  
  <p>Thanks.</p>