RxJS Marble Testing

7.0.1 · active · verified Sun Apr 19

rxjs-marbles is a flexible and framework-agnostic library designed for conducting marble tests in RxJS applications. It provides a consistent interface for testing observable streams across various JavaScript testing frameworks, including AVA, Jasmine, Jest, Mocha, and Tape, in both browser and Node.js environments. The library currently targets RxJS version 7.x, as indicated by its peer dependency on `rxjs: ^7.0.0`. Its key differentiator lies in abstracting away framework-specific boilerplate like global setups or `beforeEach`/`afterEach` hooks, allowing developers to focus solely on the marble diagrams. It wraps RxJS's internal `TestScheduler` and exposes similar helper methods, simplifying the process of defining hot/cold observables, subscriptions, and expected outputs using the familiar marble syntax. While no explicit release cadence is stated, the package is actively maintained, with version 7.0.1 being the current stable release. The library ships with TypeScript types, facilitating its use in TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic marble test using `rxjs-marbles` with Mocha. It defines a hot observable, applies a transformation, and asserts the output against a marble diagram.

import { marbles } from "rxjs-marbles/mocha";
import { map } from "rxjs/operators";
import { Observable } from 'rxjs';

describe("rxjs-marbles basic test", () => {

    it("should map values from a hot observable", marbles(m => {
        // Define a hot observable source using marble syntax
        const source =  m.hot("--^-a-b-c-|');
        // Define the subscription timeframe
        const subs =            "^-------!";
        // Define the expected output after mapping
        const expected =        "--b-c-d-|';

        // Apply the map operator to the source observable
        const destination: Observable<string> = source.pipe(
            map(value => String.fromCharCode(value.charCodeAt(0) + 1))
        );

        // Assert that the destination observable matches the expected marble diagram
        m.expect(destination).toBeObservable(expected);
        // Assert that the source was subscribed to during the defined 'subs' timeframe
        m.expect(source).toHaveSubscriptions(subs);
    }));
});

view raw JSON →