Testing observable’s values when using Angular fakeAsync

The problem

I’ve found two main approaches when unit testing observables behavior in javascript:

  • the marbles testing framework for comparing an observable to a mocked observable
  • checking the observable directly controlling time with the Zone.js fakeAsync(). Check this great article from Victor Savkin about fakeAsync().

The Zone.js fakeAsync() intercepts the asynchronous javascript features allowing for control of time. So, observables need no modification and, by default, they use the default scheduler and not the TestScheduler required by the marbles testing framework. While it is possible to make observables to use a provided scheduler, it is somewhat cumbersome and I had a lot of problemas with that route. So I decided to test the observables directly.

Also, there were this problem with the marbles testing framework. For testing an operator is awesome and very simple, but it is not adequate to test a real observable with its timely value emissions because the comparison to a mocked observable also takes into account the time when values are emitted.

Read the full article.