Skip to content

Instantly share code, notes, and snippets.

@gwing33
Created January 19, 2016 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwing33/086df87d1789bc51b772 to your computer and use it in GitHub Desktop.
Save gwing33/086df87d1789bc51b772 to your computer and use it in GitHub Desktop.
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