Solvedrxjs Documentation: Testing
✔️Accepted Answer
Is there any sort of documentation/examples available for TestScheduler
? I can't seem to find anything related to RxJs5 on the web.
Other Answers:
+1 on all this. The marble diagrams are nice, but it's entirely unclear how you'd use them to test an observable containing multiple time-based operators (i.e. real world problems). Having previously worked with Rx.net, RxJava, and RxJs4, with easy testability being one of the greatest features of Rx, it's fairly frustrating that all the standard Rx testing conventions have been seemingly abandoned.
I have stumbled across https://github.com/kwonoj/rxjs-testscheduler-compat, which in the absence of any better official guidance, looks to be the best way forward for getting our app tested.
So I did some fiddling and came up with something that allows me to control timing:
function installMockTimer(maxFrames?: number): TestScheduler {
//Create a test scheduler
const scheduler = new TestScheduler((a, b) => expect(a).toEqual(b));
//schedular.maxFrames controls how many frames (i.e. milliseconds) will be simulated
//once flush is invoked. It defaults to 750, so if you setup a timer for more than 750
//frames, then nothing will happen
scheduler.maxFrames = Number(maxFrames) || scheduler.maxFrames;
//replace timer with a fake
const originalTimer = Observable.timer;
spyOn(Observable, "timer")
.and.callFake(function(initialDelay, dueTime) {
return originalTimer.call(this, initialDelay, dueTime, scheduler);
});
//return the scheduler. This is what the client code uses to advance time
return scheduler;
}
it("", ()=>{
const scheduler = installMockTimer(5000);
//we don't need to change max frames here. Just did it to illustrate that it's possible
scheduler.maxFrames = 2000;
Observable.timer(0, 1000).subscribe((data)=> console.log(scheduler.frame);
scheduler.flush();
//LOG : 0
//LOG: 1000
//LOG: 2000
});
Could a step 1 of documenting testing be a doc about where to get the methods for marble tests at least?
I'm not using wallaby and I'm trying to find the reference to expectObservable
but I can't find it anywhere in Rx
I can recommend you to look at this project while there's no official solution.
https://github.com/cartant/rxjs-marbles
Supports: Jasmine, Mocha, Jest, AVA and Tape. Syntax differs slightly but most marble-test features are there, although not the ones from RxJS4.
See Stackblitz for some examples.
It would be splendid if there was documentation for how one would go about testing complex Observable operator chains.
This is coming from a Angular 2 perspective, where one would probably use dependency injection to inject a different Scheduler inside of the tests: i.e when doing
Observable.timer(1000, AsyncScheduler)
, one would change theAsyncScheduler
to aTestScheduler
.I've tried finding documentation (or pretty much any instructions from anyone) on how one would go about actually instrumenting these Schedulers to test Observable sequences, but it's very hard to actually figure out since the APIs have seemingly changed quite a bit going from version 4 to 5 (e.g.
scheduleAbsolute
which is used in most testing examples for Rxjs 4, is totally absent).Neither the unit tests nor the Scheduler docs were very helpful either in this regard.