RxReact/Jest Helpers are simple tools for testing RxJS observables. This can be useful any project, not just React.
In your project:
npm install @rxreact/jest-helpers --save
yarn add @rxreact/jest-helpers
RxJS and Jest are peer dependencies and need to be installed separately.
Then import the library:
This library adds a function for making an observable hot, and adds two matchers to Jest to checking if an observable has emitted a value.
If you have used
expect(jest.fn()).toHaveBeenCalledWith(value), these helpers should feel familiar.
Checking if a observable emitted
To check if an observable emitted any value:
Checking if a observable emitted a specific value
Checking that a observable did not emit anything
Checking that a observable did not emit a specific value
Watching a cold observable
Often observables are cold - they will forget any values they might have emitted before something subscribes to them. To get around this, you can use
watchSignal to get a hot version of the observable with
shareReplay, which will record everything the observable emits.
watchSignal cleans up after itself after every test to prevent memory leaks. So, make sure to only call
watchSignal in a
beforeEach or a test body.
;;it"watches a cold signal",;
Loading the library
To use the
expect(observable$).toEmitValue(value) functions in your test, you must import the library in the test file.
If you need
watchSignal (and you probably will), simply importing it will do the job:
If you have a test that only tests hot signals, you will still need to import the library to you test file to get the jest extensions:
toEitValue() work by waiting 100 ms for the observable to emit, to account for asynchronous observables. So you must
await every expectation, to make sure you catch failures: