DEV Community

Cover image for How I Write Marble Tests For RxJS Observables In Angular

How I Write Marble Tests For RxJS Observables In Angular

Michael Hoffmann on November 12, 2018

I am a really passionate Reactive Extensions user and mostly use them in RxJS which is integrated into the Angular framework. In Angular, I often ...
Collapse
 
anduser96 profile image
Andrei Gatej

You can avoid to explicitly specify the scheduler by running your tests inside TestScheduler.run(cb). TestScheduler.run will replace the AsyncScheduler with TestScheduler.

const testScheduler = new TestScheduler((actual, expected) => {})

testScheduler.run(({ cold, hot, expectObservable }) => {
 const src$ = interval(1000).pipe(take(3));
 const expected = '1000ms 0 999ms 1 999ms (2|)';

 expectObservable(src$).toBe(expected);
});
Collapse
 
green122 profile image
green122

Hi, Michael. Thanks for the nice and informative article.
Did you find a way to visualize actual observable, maybe to convert it to a marble diagram? Cause in the more complex cases i've started not to understand what is really going on inside my observable and how to compare it with the expected one.

Collapse
 
mokkapps profile image
Michael Hoffmann

Unfortunately not inside the tests. But this website is in general quite nice: rxviz.com/