textbook / rxjest

Jest matchers for working with RxJS observables
ISC License
0 stars 0 forks source link

RxJeSt

Node.js CI NPM

Jest matchers for working with RxJS observables.

Usage

Add the matchers to Jest by importing this package in a setup file:

// jest.config.js
module.exports = {
  // ...
  "setupFilesAfterEnv": [
    "<rootDir>/setupTests.js"
  ]
}
// setupTests.js
import "rxjest";  // or require("rxjest");

Matchers

Once added, the following matchers will be available:

.toEmit

.toEmit checks that the supplied observable emits the specified value at some point before completing:

it("asserts that a matching value was emitted", async () => {
    await expect(from(["foo", "bar", "baz"])).toEmit("bar", { within: 25 });
});

Note that this is an asynchronous matcher that needs to be awaited or returned.

This matcher takes an optional second argument, an object containing the following options:

This matcher has the following failure cases:

.toError

.toError checks that the supplied observable errors rather than completing:

it("asserts that the observable errors", async () => {
    await expect(throwError(() => new Error("oh no!"))).toError();
});

Note that this is an asynchronous matcher that needs to be awaited or returned.

This matcher takes an optional argument, an object containing the following options:

This matcher has the following failure cases:

.toErrorWith

.toErrorWith checks that the supplied observable errors with a matching message rather than completing:

it("asserts that the observable errors", async () => {
    await expect(throwError(() => new Error("oh no!"))).toErrorWith(/^oh no!$/);
});

Note that this is an asynchronous matcher that needs to be awaited or returned.

This matcher has the following failure cases:

Version support

Linting

If you're using the Jest plugin for ESLint and have jest/valid-expect enabled, you can configure it to understand that the matchers are asynchronous as follows:

{
  // ...
  "rules": {
    // ...
    "jest/valid-expect": [
      "error",
      {
        "asyncMatchers": [
          "toEmit",
          "toError",
          "toErrorWith"
        ]
      }
    ]
  }
}

Development

You can fork and clone this repository to work on it. Once you've cloned the code locally, install the dependencies with npm ci then check everything is installed and running correctly with npm run ship.

Scripts

The following convenience scripts are provided for development and can be run with npm run <script>:

Testing

There are three levels of testing: