How to get TestScheduler to point in RxJs5?

I am trying to write a unit test sample with Observable.intervalin RxJs version 5. I run the following code, but my observables only fire once, and not 20 times, as expected.

it('does its interval thing synchonously', ()=> {

        let x = [];
        let scheduler = new Rx.TestScheduler();
        let interval$ = Rx.Observable.interval(500, scheduler).take(20);

        interval$.subscribe(
            value => {
                x.push(value);
                console.log(value)
            },
        );

        for(let i = 0; i < 20; i++) {
            scheduler.flush();
        }

        expect(x.length).toBe(20);

    });

How do I make my TestSchedulerObservable move forward 10,000 milliseconds?

+4
source share
1 answer

I understand that it is TestSchedulerintended for use with marble testing and works with observables, consisting of returned methods createColdObservableand it createHotObservable.

Instead, you can use VirtualTimeScheduler- on which is based TestScheduler:

let scheduler = new Rx.VirtualTimeScheduler();
let interval$ = Rx.Observable.interval(500, scheduler).take(20);

let values = [];
interval$.subscribe(value => values.push(value));

scheduler.flush();
console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
Run codeHide result

, flush.

+3

Source: https://habr.com/ru/post/1682455/


All Articles