Gathering detailed insights and metrics for rxjs-marbles
Gathering detailed insights and metrics for rxjs-marbles
Gathering detailed insights and metrics for rxjs-marbles
Gathering detailed insights and metrics for rxjs-marbles
An RxJS marble testing library for any test framework
npm install rxjs-marbles
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
301 Stars
502 Commits
18 Forks
4 Watching
8 Branches
7 Contributors
Updated on 26 Oct 2024
Minified
Minified + Gzipped
TypeScript (96.62%)
JavaScript (3.31%)
Shell (0.07%)
Cumulative downloads
Total Downloads
Last day
5.3%
10,999
Compared to previous day
Last week
1.3%
57,743
Compared to previous week
Last month
22.5%
228,784
Compared to previous month
Last year
35.2%
3,225,342
Compared to previous year
2
1
rxjs-marbles
is an RxJS marble testing library that should be compatible with any test framework. It wraps the RxJS TestScheduler
and provides methods similar to the helper methods used the TestScheduler
API.
It can be used with AVA, Jasmine, Jest, Mocha or Tape in the browser or in Node and it supports CommonJS and ES module bundlers.
I created this package because I wanted to use RxJS marble tests in a number of projects and those projects used different test frameworks.
There are a number of marble testing packages available - including the Mocha-based implementation in RxJS itself - but I wanted something that was simple, didn't involve messing with globals and beforeEach
/afterEach
functions and was consistent across test frameworks.
If you are looking for something similar, this might suit.
Install the package using NPM:
npm install rxjs-marbles --save-dev
If you're just getting started with marble testing, you might be interested in how I wasted some of my time by not carefully reading the manual: RxJS Marble Testing: RTFM.
In particular, you should read the RxJS documentation on marble syntax and synchronous assertion.
rxjs-marbles
contains framework-specific import locations. If there is a location for the test framework that you are using, you should use the specific import. Doing so will ensure that you receive the best possible integration with your test framework.
For example, importing from "rxjs-marbles/jest"
will ensure that Jest's matcher is used and the output for test failures will be much prettier.
Instead of passing your test function directly to it
, pass it to the library's marbles
function, like this:
1import { marbles } from "rxjs-marbles/mocha"; 2import { map } from "rxjs/operators"; 3 4describe("rxjs-marbles", () => { 5 6 it("should support marble tests", marbles(m => { 7 8 const source = m.hot("--^-a-b-c-|"); 9 const subs = "^-------!"; 10 const expected = "--b-c-d-|"; 11 12 const destination = source.pipe( 13 map(value => String.fromCharCode(value.charCodeAt(0) + 1)) 14 ); 15 m.expect(destination).toBeObservable(expected); 16 m.expect(source).toHaveSubscriptions(subs); 17 })); 18});
To see how rxjs-marbles
can be used with other test frameworks, see the examples within the repository.
With AVA and Tape, the callback passed to the marbles
function will receive an addional argument - the AVA ExecutionContext
or the Tape Test
- which can be used to specify the number of assertions in the test plan. See the framework-specific examples for details.
In addition to the marbles
function, the library exports a cases
function that can be used to reduce test boilerplate by specifying multiple cases for variations of a single test. The API is based on that of jest-in-case
, but also includes the marbles context.
The cases
implementation is framework-specific, so the import must specify the framework. For example, with Mocha, you would import cases
and use it instead of the it
function, like this:
1import { cases } from "rxjs-marbles/mocha"; 2import { map } from "rxjs/operators"; 3 4describe("rxjs-marbles", () => { 5 6 cases("should support cases", (m, c) => { 7 8 const source = m.hot(c.s); 9 const destination = source.pipe( 10 map(value => String.fromCharCode(value.charCodeAt(0) + 1)) 11 ); 12 m.expect(destination).toBeObservable(c.e); 13 14 }, { 15 "non-empty": { 16 s: "-a-b-c-|", 17 e: "-b-c-d-|" 18 }, 19 "empty": { 20 s: "-|", 21 e: "-|" 22 } 23 }); 24});
TestScheduler
behaviour changes in RxJS version 6In RxJS version 6, a run
method was added to the TestScheduler
and when it's used, the scheduler's behaviour is significantly changed.
rxjs-marbles
now defaults to using the scheduler's run
method. To use the scheduler's old behaviour, you can call the configure
function, passing { run: false }
, like this:
1import { configure } from "rxjs-marbles/mocha"; 2const { cases, marbles } = configure({ run: false });
WARNING: bind
is deprecated and can only be used with configure({ run: false })
.
Sometimes, passing the TestScheduler
instance to the code under test can be tedious. The context includes a bind
method that can be used to bind a scheduler's now
and schedule
methods to those of the context's TestScheduler
.
bind
can be passed specific scheduler instances or can be called with no arguments to bind RxJS's animationFrame
, asap
, async
and queue
schedulers to the context's TestScheduler
.
For example:
1it("should support binding non-test schedulers", marbles(m => { 2 3 m.bind(); 4 5 const source = m.hot("--^-a-b-c-|"); 6 const subs = "^--------!"; 7 const expected = "---a-b-c-|"; 8 9 // Note that delay is not passed a scheduler: 10 const destination = source.delay(m.time("-|")); 11 m.expect(destination).toBeObservable(expected); 12 m.expect(source).toHaveSubscriptions(subs); 13}));
WARNING: reframe
is deprecated and can only be used with configure({ run: false })
.
The RxJS TestScheduler
defaults to 10 virtual milliseconds per frame (each character in the diagram represents a frame) with a maximum of 750 virtual milliseconds for each test.
If the default is not suitable for your test, you can change it by calling the context's reframe
method, specifying the time per frame and the (optional) maximum time. The reframe
method must be called before any of the cold
, flush
, hot
or time
methods are called.
The examples include tests that use reframe
.
If the BDD syntax is something you really don't like, there are some alternative methods on the Context
that are more terse:
1const source = m.hot("--^-a-b-c-|", values); 2const subs = "^-------!"; 3const expected = m.cold("--b-c-d-|", values); 4 5const destination = source.map((value) => value + 1); 6m.equal(destination, expected); 7m.has(source, subs);
The rxjs-marbles
API includes the following functions:
1interface Configuration { 2 assert?: (value: any, message: string) => void; 3 assertDeepEqual?: (a: any, b: any) => void; 4 frameworkMatcher?: boolean; 5 run?: boolean; 6} 7 8function configure(options: Configuration): { marbles: MarblesFunction };
The configure
method can be used to specify the assertion functions that are to be used. Calling it is optional; it's only necessary if particular assertion functions are to be used. It returns an object containing a marbles
function that will use the specified configuration.
The default implementations simply perform the assertion and throw an error for failed assertions.
1function marbles(test: (context: Context) => any): () => any; 2function marbles<T>(test: (context: Context, t: T) => any): (t: T) => any;
marbles
is passed the test function, which it wraps, passing the wrapper to the test framework. When the test function is called, it is passed the Context
- which contains methods that correspond to the TestScheduler
helper methods:
1interface Context { 2 autoFlush: boolean; 3 bind(...schedulers: IScheduler[]): void; 4 cold<T = any>(marbles: string, values?: any, error?: any): ColdObservable<T>; 5 configure(options: Configuration): void; 6 equal<T = any>(actual: Observable<T>, expected: Observable<T>): void; 7 equal<T = any>(actual: Observable<T>, expected: string, values?: { [key: string]: T }, error?: any): void; 8 equal<T = any>(actual: Observable<T>, subscription: string, expected: Observable<T>): void; 9 equal<T = any>(actual: Observable<T>, subscription: string, expected: string, values?: { [key: string]: T }, error?: any): void; 10 expect<T = any>(actual: Observable<T>, subscription?: string): Expect<T>; 11 flush(): void; 12 has<T = any>(actual: Observable<T>, expected: string | string[]): void; 13 hot<T = any>(marbles: string, values?: any, error?: any): HotObservable<T>; 14 reframe(timePerFrame: number, maxTime?: number): void; 15 readonly scheduler: TestScheduler; 16 teardown(): void; 17 time(marbles: string): number; 18} 19 20interface Expect<T> { 21 toBeObservable(expected: ColdObservable<T> | HotObservable<T>): void; 22 toBeObservable(expected: string, values?: { [key: string]: T }, error?: any): void; 23 toHaveSubscriptions(expected: string | string[]): void; 24}
In Jasmine, Jest and Mocha, the test framework recognises asynchronous tests by their taking a done
callback or returning a promise.
The observe
helper can be useful when an observable cannot be tested using a marble test. Instead, expectations can be added to the observable stream and the observable can be returned from the test.
See the examples for usage.
With Jest and Jasmine, the test framework can be configured to use its own concept of fake time. AVA, Mocha and Tape don't have built-in support for fake time, but the functionality can be added via sinon.useFakeTimers()
.
In some situations, testing asynchronous observables with marble tests can be awkward. Where testing with marble tests is too difficult, it's possible to test observables using the test framework's concept of fake time, but the now
method of the AsyncScheduler
has to be patched. The fakeSchedulers
helper can be used to do this.
See the examples for usage.
Also, I've written an article on the fakeSchedulers
function: RxJS: Testing with Fake Time.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
32 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Morejasmine-marbles
Marble testing helpers for RxJS and Jasmine
jest-marbles
Marble testing helpers library for RxJs and Jest
rxjs-report-usage
Report RxJS API usage to the core team
@rxjs-stuff/marbles
A set of plugins that provide a natural feeling integration with Mocha and Chai for RxJS "marbles" testing.