Gathering detailed insights and metrics for vitest-marbles
Gathering detailed insights and metrics for vitest-marbles
Gathering detailed insights and metrics for vitest-marbles
Gathering detailed insights and metrics for vitest-marbles
npm install vitest-marbles
Typescript
Module System
Min. Node Version
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A set of helper functions and Jest matchers for RxJs marble testing. This library will help you to test your reactive code in easy and clear way.
For RxJs 7:
1npm i jest-marbles@latest -D
For RxJs 6:
1npm i jest-marbles@2 -D
For RxJs 5:
1npm i jest-marbles@1 -D
In the test file:
1import {cold, hot, time} from 'jest-marbles';
Inside the test:
1expect(stream).toBeObservable(expected); 2expect(stream).toBeMarble(marbleString); 3expect(stream).toHaveSubscriptions(marbleString); 4expect(stream).toHaveSubscriptions(marbleStringsArray); 5expect(stream).toHaveNoSubscriptions(); 6expect(stream).toSatisfyOnFlush(() => { 7 expect(someMock).toHaveBeenCalled(); 8})
Verifies that the resulting stream emits certain values at certain time frames
1 it('Should merge two hot observables and start emitting from the subscription point', () => { 2 const e1 = hot('----a--^--b-------c--|', {a: 0}); 3 const e2 = hot(' ---d-^--e---------f-----|', {a: 0}); 4 const expected = cold('---(be)----c-f-----|', {a: 0}); 5 6 expect(e1.pipe(merge(e2))).toBeObservable(expected); 7 });
Sample output when the test fails (if change the expected result to '-d--(be)----c-f-----|'
):
Expected notifications to be:
"-d--(be)----c-f-----|"
But got:
"---(be)----c-f-----|"
Same as toBeObservable
but receives marble string instead
1 it('Should concatenate two cold observables into single cold observable', () => { 2 const a = cold('-a-|'); 3 const b = cold('-b-|'); 4 const expected = '-a--b-|'; 5 expect(a.pipe(concat(b))).toBeMarble(expected); 6 });
Verifies that the observable was subscribed in the provided time frames.
Useful, for example, when you want to verify that particular switchMap
worked as expected:
1 it('Should figure out single subscription points', () => { 2 const x = cold(' --a---b---c--|'); 3 const xsubs = ' ------^-------!'; 4 const y = cold(' ---d--e---f---|'); 5 const ysubs = ' --------------^-------------!'; 6 const e1 = hot(' ------x-------y------|', { x, y }); 7 const expected = cold('--------a---b----d--e---f---|'); 8 9 expect(e1.pipe(switchAll())).toBeObservable(expected); 10 expect(x).toHaveSubscriptions(xsubs); 11 expect(y).toHaveSubscriptions(ysubs); 12 });
The matcher can also accept multiple subscription marbles:
1 it('Should figure out multiple subscription points', () => { 2 const x = cold(' --a---b---c--|'); 3 4 const y = cold(' ----x---x|', {x}); 5 const ySubscription1 = ' ----^---!'; 6 // '--a---b---c--|' 7 const ySubscription2 = ' --------^------------!'; 8 const expectedY = cold(' ------a---a---b---c--|'); 9 10 const z = cold(' -x|', {x}); 11 // '--a---b---c--|' 12 const zSubscription = ' -^------------!'; 13 const expectedZ = cold(' ---a---b---c--|'); 14 15 expect(y.pipe(switchAll())).toBeObservable(expectedY); 16 expect(z.pipe(switchAll())).toBeObservable(expectedZ); 17 18 expect(x).toHaveSubscriptions([ySubscription1, ySubscription2, zSubscription]); 19 });
Sample output when the test fails (if change ySubscription1
to '-----------------^---!'
):
Expected observable to have the following subscription points:
["-----------------^---!", "--------^------------!", "-^------------!"]
But got:
["-^------------!", "----^---!", "--------^------------!"]
Verifies that the observable was not subscribed during the test. Especially useful when you want to verify that certain chain was not called due to an error:
1 it('Should verify that switchMap was not performed due to an error', () => { 2 const x = cold('--a---b---c--|'); 3 const y = cold('---#-x--', {x}); 4 const result = y.pipe(switchAll()); 5 expect(result).toBeMarble('---#'); 6 expect(x).toHaveNoSubscriptions(); 7 });
Sample output when the test fails (if remove error and change the expected marble to '------a---b---c--|'
):
Expected observable to have no subscription points
But got:
["----^------------!"]
Allows you to assert on certain side effects/conditions that should be satisfied when the observable has been flushed (finished)
1 it('should verify mock has been called', () => { 2 const mock = jest.fn(); 3 const stream$ = cold('blah|').pipe(tap(mock)); 4 expect(stream$).toSatisfyOnFlush(() => { 5 expect(mock).toHaveBeenCalledTimes(4); 6 }); 7 })
No vulnerabilities found.
No security vulnerabilities found.