Skip to content

Extends Chai with assertions about RxJS observable streams.

Notifications You must be signed in to change notification settings

hellosmithy/chai-rx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Warning

This package relates to the rx package at v4. It will not work with the newer rxjs package which refers to Rx v5 and above.

Chai Assertions for RxJS Observables

ChaiRx extends Chai with a simple utility method emit for testing emits from an RxJS Observable stream using the Rx.TestScheduler.

import Rx from "rx";
import chai from "chai";
import chaiRx from "chai-rx";

chai.use(chaiRx);

const { onNext, onCompleted } = Rx.ReactiveTest;
const scheduler = new Rx.TestScheduler();

const xs = scheduler.createHotObservable(
  onNext(150, 1),
  onNext(210, 2),
  onNext(220, 3),
  onCompleted(230)
);

// Note we'll start at 200 for subscribe, hence missing the 150 mark
const output = scheduler.startScheduler(() => xs.map((x) => x * x), {
  created: 100,
  subscribed: 200,
  disposed: 300,
});

expect(output).to.emit([onNext(210, 4), onNext(220, 9), onCompleted(230)]);

Usage

expect / should syntax

const xs = scheduler.createHotObservable(
  onNext(250, { foo: "bar" }),
  onError(300, new Error("An error"))
);
const output = scheduler.startScheduler(() => xs);

// expect
expect(output).to.emit([
  onNext(250, { foo: "bar" }),
  onError(300, ({ error }) => error.message === "An error"),
]);

// should
output.should.emit([
  onNext(250, { foo: "bar" }),
  onError(300, ({ error }) => error.message === "An error"),
]);

Language chains

const const xs = scheduler.createHotObservable(onNext(250));
const output = scheduler.startScheduler(() => xs);

// with `not`
expect(output).to.not.emit([
  onNext(300)
]);

Installation

npm install chai-rx

ES6 Imports

import chai from "chai";
import chaiRx from "chai-rx";

chai.use(chaiRx);

AMD

var chai = require("chai");
var chaiRx = require("chai-rx");

chai.use(chaiRx);

<script> tag

If you include ChaiRx directly with a <script> tag, after the one for Chai itself, then it will automatically plug in to Chai and be ready for use:

<script src="chai.js"></script>
<script src="chai-rx.js"></script>

About

Extends Chai with assertions about RxJS observable streams.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •