
Introduction
Before we start diving into unit testing - we should clarify what are schedulers in RxJS
You already know that Observables produce values over time. And the moment when exactly value will be emitted is controlled by special entity - scheduler.
Knowing how schedulers work are extremely important in understanding how to unit test you async code
Possibly you already know that topic - but plz don’t skip - recalling that will help us to be on the same page.
Jasmine framework provides a special ‘done' callback to testing async code.
How to apply it to Observables? Let's find out.
In a previous lesson, we reviewed jasmine and its done callback for testing.
But noticed that it is not good for big delay values.
And this is where VirtualTimeScheduler can help us.
It provides a virtual time mechanism so you can emulated passed time inside RxJS scheduler to run delayed tasks instantly.
Besides the methods we reviewed previously - angular also has another possible way of testing observables that use asyncScheduler.
For that method, fakeAsync helper function is used.
Before we start getting deeper in TestScheduler usages We have to understand how it differs from VirtualTimeScheduler.
I’ve already told you that TestScheduler inherits from VirtualTimeScheduler.
So you may think that we can use TestScheduler in the same way as VirtualTimeScheduler.
And you are right!
Another testing method that is more visual and allows us to write tests in a more obvious way is marble testing. In that method, all observable sequences are represented as marble diagrams.
And RxJS TestScheduler has special methods that give us the possibility to create tests with marble diagrams.
Jasmine-marbles is just a wrapper library for RxJS TestScheduler.
So it provides the same functionality.
But also it brings some amenities to make tests code shorter.
Starting from RxJS ver6 TestScheduler has a special method 'run' which allows time progressive syntax in marble digrams.
So we can specify explicitly delay values in a convenient manner.
Let's take a closer look.
In one of the previous lessons, we reviewed jasmine-marbles.
But there is one more wrapper library: rxjs-marbles.
Does it provide the same functionality or can it do even more?
Let's find out that.
Now you know a lot about the main RxJS testing tools.
To remember it even better - Let's recall what we have learned.
If you already tried to code unit tests for Observables - then you may be overwhelmed with a variety of methods on how to do that.
Which one is right for you?
Will it be jasmine with 'done' callback which we usually use for async code?
Or maybe TestScheduler?
Or you are going to use jasmine-marbles which is being mentioned in official Angular documentation?
I am not even naming other methods.
What is common for them and where they differ? How to put all of them in one solid picture in your head?
This is what I am going to do in my lessons - I will make you understand the system.