What is jasmine-marbles?
The jasmine-marbles package is a library for testing RxJS observables using marble diagrams in Jasmine. It allows developers to create and test complex asynchronous code in a more readable and maintainable way by using marble syntax to represent the behavior of observables over time.
What are jasmine-marbles's main functionalities?
Testing Observable Streams
This feature allows you to test the behavior of cold observables by using marble diagrams. The `cold` function creates a cold observable, and `expectObservable` is used to assert that the observable behaves as expected.
const { cold, hot, expectObservable, expectSubscriptions } = require('jasmine-marbles');
it('should test cold observable', () => {
const source = cold('--a--b--c|');
const expected = '--a--b--c|';
expectObservable(source).toBe(expected);
});
Testing Hot Observables
This feature allows you to test the behavior of hot observables. The `hot` function creates a hot observable, and `expectObservable` is used to assert that the observable behaves as expected.
const { cold, hot, expectObservable, expectSubscriptions } = require('jasmine-marbles');
it('should test hot observable', () => {
const source = hot('-a-^--b--c|');
const expected = '---b--c|';
expectObservable(source).toBe(expected);
});
Testing Subscriptions
This feature allows you to test the subscription behavior of observables. The `expectSubscriptions` function is used to assert that the observable's subscriptions match the expected subscription pattern.
const { cold, hot, expectObservable, expectSubscriptions } = require('jasmine-marbles');
it('should test subscriptions', () => {
const source = cold('--a--b--c|');
const subs = '^-------!';
expectSubscriptions(source.subscriptions).toBe(subs);
});
Other packages similar to jasmine-marbles
rxjs-marbles
rxjs-marbles is another library for testing RxJS observables using marble diagrams. It provides similar functionality to jasmine-marbles but is designed to work with various testing frameworks like Jest, Mocha, and Jasmine. It offers a more flexible API and better integration with different testing environments.
jest-marbles
jest-marbles is a library specifically designed for testing RxJS observables using marble diagrams in Jest. It provides a similar API to jasmine-marbles but is tailored for use with the Jest testing framework, offering better integration and more idiomatic Jest syntax.