title: How do I test an angular service that uses setInterval using jasmine?
body: <p>I am attempting to setup a test of a setInterval call. setInterval always creates a new timer so no matter how far ahead you move tick() there will always be an error with this message:</p>
  
  <p>Error: 1 timer(s) still in the queue.</p>
  
  <p>If I override the setTimeout function with setInterval, then the test passes as expected. I have added code to allow this to my angular service using a public property myTimerFunc that is normally set to setInterval. The test code sets it to setTimeout.</p>
  
  <p><strong>Is there a better way to test setInterval?</strong></p>
  
  <p>It would be nice if the last timer in the queue would be ignored by jasmine. Then I could test without extra cruft.</p>
  
  <p>For example this code will fail unless you uncomment the line that overrides setInterval. I do not mind doing this in the test code but I do want to avoid adding extra stuff to the normal code just to allow this override.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { fakeAsync, tick } from '@angular/core/testing'
  describe('testing setTimeout and setInterval:', () =&gt; {
  it('setTimeout should work', fakeAsync(() =&gt; {
    let callback = jasmine.createSpy('setTimeoutCallback')
    setTimeout(() =&gt; {
      callback()
    }, 55)
    tick(56)
    expect(callback).toHaveBeenCalled();
  }));
  // let setInterval = setTimeout
  it('setInterval should work', fakeAsync(() =&gt; {
    let callback = jasmine.createSpy('setTimeoutCallback')
    setInterval(() =&gt; {
      callback()
    }, 55)
    tick(56)
    expect(callback).toHaveBeenCalled();
  }));
  })</code></pre>
  </div>
  </div>
  </p>