How To Easily Write And Debug RxJS Marble Tests
End of 2018, I wrote an article about how I write marble tests for RxJS observables in Angular. The content is still valid, but I recently found a new library that I like and makes debugging marble tests easier.
If you do not know RxJS marble tests yet, I recommend you first read my article, which covers the basics.
As quick catchup, the following example shows a marble diagram that can be used in tests to represent an observable:
In this article, I want to talk about rx-sandbox, a marble diagram DSL-based test suite for RxJS 6. It also has support for RxJS 5 in pre-1.x versions if you need that in your application.
Why rx-sandbox?
I found this library as I was looking for a better way to debug marble tests as it was not possible to see such a test output using the jasmine-marbles library:
In my opinion, this is a straightforward and understandable representation of what went wrong in the test.
The library also has some other nice features:
- No dependencies on a specific test framework.
- Near-zero configuration, works out of box.
- Supports extended marble diagram DSL.
- Provides feature parity to TestScheduler.
Hello World Example
This is simple example of a marble test using rx-sandbox from the official GitHub repository:
More Realistic Example
As things are typically more complicated than in the simple examples, I have created a project which contains a more realistic scenario with this simple architecture:
The demo application contains these services:
NewsApiService
: Represents a service that simulates an API communication to fetch newsAppFacadeService
: The facade which is used betweenAppComponent
andNewsApiService
to handle the communication and add additional functionality on top of the API calls
The relevant marble tests are located in app-facade.service.spec.ts.
Create Test Instance
Marble Test
After creating the test setup we are now ready for our first test:
A failed test will show a similar output:
We can immediately see that the received observable emitted the events on different frames:
Additionally, the frames may be correct, but the source and expected observable values differ.
The output for each event is in this format:
So you will then compare these values from the received and expected observables. rx-sandbox will print you a diff to see the difference in the values:
Conclusion
In my experience, most developers struggle with interpreting the result of marble tests as libraries like jasmine-marbles
do not provide a good visual representation of the expected and received streams.
rx-sandbox
solves this problem by providing a visual representation of the expected & received marble string and a more readable diff of the values. Additionally, you can use the library in any frontend test framework.
Let me know your thoughts about this library in the comments.
The Mistakes I Made In My First Software Project
Before starting my professional career as a developer, I mainly developed Android apps using Java as the programming language. I got hired by a software service company, and we had to develop JavaScript-based applications for cars in my first project. So the first time in my life, I had to work with JavaScript, and I made many mistakes during this time which I now want to share with you.
JHipster - The Fastest Way To Build A Production-Ready Angular & Spring Boot Application
In the last years, I mainly worked on the frontend part of web & mobile applications, but I also did some minor backend work. Since mid of this year, I have been working to improve my backend knowledge and started to focus on Java backend development using Spring Boot.