Warning
This package has been deprecated in favor of @1log/rxjs.
1log plugin for logging RxJS observables. Please see an article Log and test RxJS observables with 1log for an introduction.
💡 TIP
If you just need to quickly install the library to play around with it, install packages
1logand1log-rxjsand add the following imports:import '1log/defaultConfig'; import '1log-rxjs/defaultConfig'; import { log } from '1log';
Assuming you have RxJS installed,
-
Install the npm package:
yarn add 1log-rxjsor
npm install 1log-rxjs --save -
Where you import
'1log/defaultConfig', alsoimport '1log-rxjs/defaultConfig', which runsinstallPlugins(statePlugin). Where you import'1log/defaultJestConfig', alsoimport '1log-rxjs/defaultJestConfig', which is the same as importing a file with the following contents:import { installPlugins } from '1log'; import { observablePlugin } from '1log-rxjs'; import { Observable } from 'rxjs'; // Add a serializer for observables. expect.addSnapshotSerializer({ test: (value) => value !== null && value !== undefined && value.constructor === Observable, serialize: () => `[Observable]`, }); // Add a serializer for subscribers. expect.addSnapshotSerializer({ test: (value) => value instanceof Subscriber, serialize: () => `[Subscriber]`, }); installPlugins(observablePlugin);
import { log } from '1log';
import { timer } from 'rxjs';
timer(500).pipe(log).subscribe();💡 TIP
This plugin logs
Observables but not instances of classes inheriting fromObservable. If you need to log a subject, first convert it to an observable usingasObservablemethod.
💡 TIP
In Chrome, you can right-click an observable or a subscriber from a create/subscribe log message and say "Store as global variable". The object will be stored with a name like
temp1, and you'll be able to runtemp1.subscribe()(observable) ortemp1.next(yourValue)/temp1.error(yourError)/temp1.complete()(subscriber).
import { getMessages, log } from '1log';
import { timer } from 'rxjs';
test('timer', () => {
timer(500).pipe(log).subscribe();
jest.runAllTimers();
expect(getMessages()).toMatchInlineSnapshot(`
[create 1] +0ms [Observable]
[create 1] [subscribe 1] +0ms [Subscriber]
[create 1] [subscribe 1] [next 1] +500ms 0
[create 1] [subscribe 1] [complete] +0ms
· [create 1] [subscribe 1] [unsubscribe] +0ms
`);
});💡 TIP
In older versions of RxJS you had to use
TestSchedulerin tests, but since version 6.2.1, RxJS works well with Jest's fake time.
