@hirez_io/observer-spy 👀💪
A simple little class that helps making Observable testing a breeze

What's the problem?
Testing RxJS observables is usually hard, especially when testing advanced use cases.
This library:
✅ Easy to understand
✅ Reduces the complexity
✅ Makes testing advanced observables easy
Installation
yarn add -D @hirez_io/observer-spy
or
npm install -D @hirez_io/observer-spy
Usage
new ObserverSpy()
In order to test observables, you can use an ObserverSpy
instance to "record" all the messages a source observable emits and to get them as an array.
You can also spy on the error
or complete
states of the observer.
Example:
it('should spy on Observable values', () => {
const observerSpy = new ObserverSpy();
const fakeValues = ['first', 'second', 'third'];
const fakeObservable = of(...fakeValues);
const subscription = fakeObservable.subscribe(observerSpy);
subscription.unsubscribe();
expect(observerSpy.receivedNext()).toBe(true);
expect(observerSpy.getValues()).toEqual(fakeValues);
expect(observerSpy.getValuesLength()).toEqual(3);
expect(observerSpy.getFirstValue()).toEqual('first');
expect(observerSpy.getValueAt(1)).toEqual('second');
expect(observerSpy.getLastValue()).toEqual('third');
expect(observerSpy.receivedComplete()).toBe(true);
});
it('should spy on Observable errors', () => {
const observerSpy = new ObserverSpy();
const fakeObservable = throwError('FAKE ERROR');
fakeObservable.subscribe(observerSpy);
expect(observerSpy.receivedError()).toBe(true);
expect(observerSpy.getError()).toEqual('FAKE ERROR');
});