Member-only story
RxJS Marble Testing: RTFM
Last week, I finally got around to something that I’d been putting off for far too long: incorporating marble tests into projects in which I am using RxJS.
I’m not going to go into detail about the syntax with which marble tests are declared — as that’s covered in the Writing Marble Tests document in the RxJS repo — but I will go over a few aspects of marble testing that confused me more than they would have if I had read the manual with greater care. Also, I’ll introduce a marble testing package I’ve published that can be used with any test framework.
Marble Tests
The marble tests below call the Mocha-based, basic methods that are used throughout the RxJS code base. Being opinionated regarding the test framework, they are not included in the RxJS distribution. Essentially, the basic methods are thin wrappers around the TestScheduler
and they reduce the boilerplate that would otherwise be required by the tests.
A simple test looks something like this:
const a = cold("--1--2--|");
const asub = "^-------!";
const expected = "--2--3--|";const result = a.map(s => `${Number(s) + 1}`);
expectObservable(result).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asub);