Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RxJS JestJS / Jasmine2 Custom Matcher
/* * *
* Just a quick tester to help with Rx testing...
* - - -
* Be sure to include in the jest config...
* "setupTestFrameworkScriptFile": "<rootDir>/{pathTo}/customMatchers.js",
* "testRunner": "<rootDir>/../node_modules/jest-cli/src/testRunners/jasmine/jasmine2.js"
* - - -
* Usage...should be something similar to what is below (untested)
*
* const subject = new Rx.Subject();
* const scheduler = new Rx.TestScheduler();
* const onNext = Rx.ReactiveTest.onNext;
*
* results = scheduler.startScheduler(() => {
* return subject.asObserver();
* });
*
* scheduler.scheduleAbsolute({}, 300, () => {
* subject.onNext({topic: 'helloWorld'});
* });
*
* expect(results.messages).toHaveEqualElements(onNext(300, {topic: 'helloWorld'}), ...onNext({})) // If you have more...
* * */
'use strict';
function areElementsEqual(actual, expected, comparer) {
var Rx = require('rx');
var theComparer = comparer || Rx.internals.isEqual;
if (expected.length !== actual.length) {
return false;
}
for (var id = 0; id < expected.length; id++) {
if (!theComparer(expected[id], actual[id])) {
return false;
}
}
return true;
}
var customMatchers = {
toHaveEqualElements: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var len = arguments.length;
var expecteded = new Array(len-1);
for (var id = 1; id < len; id++) {
expecteded[id-1] = arguments[id];
}
return { pass: areElementsEqual(actual, expecteded) };
}
};
}
}
jasmine.getEnv().beforeEach(function() {
jasmine.addMatchers(customMatchers);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.