RxJS Marble Testing
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
-
TypeError: Cannot read properties of undefined (reading 'run')
cause The test function was not correctly wrapped by the `marbles` helper function, meaning no `TestScheduler` instance ('m' argument) was provided.fixEnsure your test callback is passed to `marbles`, like `it('should work', marbles(m => { ... }));`. -
Expected true to be false
cause A common assertion error indicating that the expected marble diagram does not match the actual observable output.fixCarefully re-examine your source, subscription, and expected marble diagrams. Use `m.log('label', observable)` to debug and inspect the actual emissions of your observables during the test run. -
TypeError: (0 , rxjs_operators_1.map) is not a function
cause This error typically occurs when RxJS operators are imported incorrectly, often in CommonJS environments or when using incompatible RxJS versions with your module resolution settings.fixVerify that RxJS operators are imported from `rxjs/operators` (e.g., `import { map } from 'rxjs/operators';`). If using CommonJS, ensure your build setup correctly transpires ESM imports, or use `const { map } = require('rxjs/operators');` if targeting CJS directly (though ESM imports are preferred for modern RxJS).
Warnings
- breaking Ensure your `rxjs` version is compatible with the `rxjs-marbles` peer dependency. `rxjs-marbles@7.x` requires `rxjs@^7.0.0`. Mismatched versions can lead to `TestScheduler` API incompatibilities and runtime errors.
- gotcha Failing to use framework-specific import paths (e.g., `rxjs-marbles/jest` instead of `rxjs-marbles`) results in less optimal test integration, potentially poorer error reporting, and missing framework-specific matchers.
- gotcha A common pitfall is misunderstanding the core RxJS marble testing syntax, especially synchronous assertion rules and frame timing. `rxjs-marbles` acts as a wrapper for RxJS's `TestScheduler`, so its behavior is dictated by RxJS's underlying marble test mechanics.
Install
-
npm install rxjs-marbles -
yarn add rxjs-marbles -
pnpm add rxjs-marbles
Imports
- marbles
import { marbles } from 'rxjs-marbles';import { marbles } from 'rxjs-marbles/mocha'; - map
import { map } from 'rxjs';import { map } from 'rxjs/operators'; - TestScheduler
import { TestScheduler } from 'rxjs/testing';
Quickstart
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);
}));
});