/1log-rxjs

1log plugin for logging RxJS observables

Primary LanguageTypeScriptMIT LicenseMIT

Warning

This package has been deprecated in favor of @1log/rxjs.

1log-rxjs

npm version gzip size tree shaking types coverage status

1log plugin for logging RxJS observables. Please see an article Log and test RxJS observables with 1log for an introduction.

Installing

💡 TIP

If you just need to quickly install the library to play around with it, install packages 1log and 1log-rxjs and add the following imports:

import '1log/defaultConfig';
import '1log-rxjs/defaultConfig';
import { log } from '1log';

Assuming you have RxJS installed,

  1. Install 1log.

  2. Install the npm package:

    yarn add 1log-rxjs
    

    or

    npm install 1log-rxjs --save
    
  3. Where you import '1log/defaultConfig', also import '1log-rxjs/defaultConfig', which runs installPlugins(statePlugin). Where you import '1log/defaultJestConfig', also import '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);

Usage

import { log } from '1log';
import { timer } from 'rxjs';

timer(500).pipe(log).subscribe();

screenshot

💡 TIP

This plugin logs Observables but not instances of classes inheriting from Observable. If you need to log a subject, first convert it to an observable using asObservable method.

💡 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 run temp1.subscribe() (observable) or temp1.next(yourValue) / temp1.error(yourError) / temp1.complete() (subscriber).

Usage in tests

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 TestScheduler in tests, but since version 6.2.1, RxJS works well with Jest's fake time.


Contributing guidelines