Using the rxjs5 marble testing method inside another project

I have a webapp project that uses rxjs5 to implement flux , and am currently looking for solutions to write unit tests on it.

In fact, I implemented custom observables inside, for example:

function getActivityObservable(events, timeout) { return Observable.create((observer) => { const deb = debounce(() => observer.next(false), timeout || DEFAULT_TIMEOUT); const sub = events.subscribe((e) => { if (!e) { deb.cancel(); observer.next(false); } else { observer.next(true); deb(e); } }); return () => { if (sub) sub.unsubscribe(); if (deb) deb.cancel(); }; }).distinctUntilChanged(); } 

I would like to test it using the marble testing method and write something like (I took an example from the rxjs repository)

  describe("getActivityObservable", () => { it("should debounce by selector observable", () => { const e1 = hot("--a--bc--d----|"); const e1subs = "^ !"; const expected = "----a---c--d--|"; expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); }); 

My question is:

Is it possible to use the marble testing method (with operators like hot , cold , etc.) outside the rxjs5 project. I do not understand how to use this good tool in my project.

Thank you for your help.

+7
javascript unit-testing karma-runner jasmine rxjs5
source share
1 answer

You can, but like Ben comment: "It's not very ergonomic."

I use the mocha and monkey fix it :

 const isEqual = require('lodash.isequal'); const TestScheduler = require('rxjs/testing/TestScheduler').TestScheduler; const assertDeepEqualFrame = (actual, expected) => { if (!isEqual(actual, expected)) { throw new Error('Frames not equal!'); } } const oit = global.it; global.it = function(description, cb, timeout) { if (cb.length === 0) { oit(description, function() { global.rxTestScheduler = new TestScheduler(assertDeepEqualFrame); cb(); global.rxTestScheduler.flush(); }); } else { // async test oit.apply(this, arguments); } }; 

I have a lot of inspiration from ngrx / store and especially this file: https://github.com/ngrx/store/blob/master/spec/helpers/test-helper.ts

and then I can write my test, for example:

 it('should filter with an always-true predicate', () => { const source = hot('-1--2--^-3-4-5-6--7-8--9--|'); const expected = '--3-4-5-6--7-8--9--|'; const predicate = () => { return true; }; expectObservable(source.filter(predicate)).toBe(expected); }); 

Edit You can see how I install the monkey patch it here: https://github.com/tjoskar/ng2-lazyload-image/blob/5e1c64a3611530ce26857a566b2d76dff890a3c5/test/helpers/test-helper.ts

+3
source share

All Articles