ReactiveX / rxjs

A reactive programming library for JavaScript
https://rxjs.dev
Apache License 2.0
30.7k stars 3k forks source link

"State of the art" testing in RxJS 5 #2483

Closed zs-zs closed 6 years ago

zs-zs commented 7 years ago

Hi guys,

This isn't an issue with the code but rather a theoretical question on what is the "state of the art" method to test RxJS 5 code?

On this documentation page I see that there is a TestScheduler which can create stub observables given a marble syntax. As far as I understood, one can use these fake observables as inputs to test a specific operator or a whole chain of operators.

But I think most of the time I'm not interested in testing how the control flow was built up using the operator chain - instead I'd like to test the side effects of a subscription while mocking & stubbing some dependencies out to make the test faster. One such dependency is time - see my related StackOverflow question

It seems in RxJS 4 there was a way to set up a TestScheduler which you could use to set the virtual time manually. In RxJS 5 I don't find its equivalent. Do I miss something which is not documented yet?

kwonoj commented 7 years ago

We're in a state of huge refactor to testscheduler interface to allow various usecase sceanarios. While it is not ready yet, you still have way to setup current TestScheduler as you want to try out cases, esp. current marble-based test cases are also testing time-based dependencies as you described. In your question probably what you need is just inject testschduler, record subscription messages, and flush scheduler. If you'd like to try RxJS4-ish scheduler interface, there are 3rd party module I wrote for those purpose you can try out. So it is correct (for now) you don't see any documentation for v4-compatible test scheduler interface since there isn't thing like those yet.

intellix commented 7 years ago

Duplicate of: #1791

amoerie commented 7 years ago

@kwonoj is there a way to follow up on the progress of this refactor? We want to upgrade to RxJs 5 but want to wait until the unit testing infrastructure is ready and complete.

kwonoj commented 7 years ago

@amoerie I don't have visibility of its status (as I'm not owning those task at this moment).

But I don't think test blocks your migration - marble-based DSL will be available in a compatible manner (will have some new syntaxes though) and just requires setting up test scheduler of your own in your code, can be gracefully migrated even after refactoring. This is way I'm doing for my codebases now.

gsans commented 6 years ago

Let's say you want to run this code without waiting 11 seconds

interval(1000).take(10).subscribe(observer);

You can use the VirtualTimeScheduler like this

let vs = new VirtualTimeScheduler(VirtualAction);
interval(1000, vs).take(10).subscribe(observer);
vs.flush();