skip to content

Marble Diagram Tests

TestScheduler.run executes streams on virtual time, and cold, hot and expectObservable compare them against marble strings. Interviewers ask how to test debounceTime without real waiting.

part ofRxJSoverview, primer and where to startread it →
on this pageshow

explore

questions

5

In an RxJS marble diagram string such as '--a--b--|', what do the dashes, the letters, | and # represent?

level: juniorimportance: should knowfreq 30%

basics

~20 s

In RxJS marble diagrams each dash is one frame of virtual time, a letter or digit is a next value at that frame, | is completion and # is an error. In '--a--b--|', a emits at frame 2, b at 5, completion at 8.

open as a page

In an RxJS marble test, when do you create a source with hot() instead of cold(), and what does ^ mark?

level: middleimportance: should knowfreq 32%

basics

~20 s

In RxJS marble tests, cold() replays its timeline from each subscription, so it suits request stubs; hot() runs one shared timeline, so it suits user input or subjects. In a hot() diagram, ^ marks frame zero, where the tested code subscribes.

open as a page

In an RxJS marble test, how do you use expectSubscriptions and subscription marbles to prove that switchMap unsubscribes a stale inner stream?

level: seniorimportance: should knowfreq 25%

basics

~20 s

In RxJS marble tests, hot() and cold() fixtures log every subscription. Pass fixture.subscriptions to expectSubscriptions(...).toBe(...) with subscription marbles, where ^ marks subscribe and ! unsubscribe; an array covers several subscriptions, proving the stale inner ended when the next began.

open as a page

Why can an RxJS TestScheduler.run marble test hang forever or see no values at all, and how do you fix each case?

level: seniorimportance: nice to knowfreq 20%

basics

~20 s

RxJS TestScheduler.run removes the frame limit, so an endless source such as interval keeps virtual time flushing forever; add an unsubscription marble. Promises and direct timers are not virtualised, so their values never arrive; test those parts asynchronously or stub them.

open as a page