I recently run into trouble while testing a service that returns an observable depending on an rxjs timer. Because a quick google search didn't bring up a clear answer, I share my solution with you.

The service was looking similar to this one. It returns a status (in this case valid or invalid) depending on the giving time.

While writing the test, the following error occurred.

Error: 1 periodic timer(s) still in the queue.

After trying a few variations here is what a did.

Actually, it is pretty straight forward, but make sure you pay attention to these parts:

  • mark the test function as fakeAsync
  • use tick()
  • it is necessary to unsubscribe at the end of the test

I hope this helps if you run into a similar problem.