rxjs-marbles
Version:
An RxJS marble testing library for any test framework
54 lines (53 loc) • 1.79 kB
JavaScript
import { circularDeepEqual } from "fast-equals";
function stringify(x) {
if (x === undefined) {
return "undefined";
}
return JSON.stringify(x, function (key, value) {
if (Array.isArray(value)) {
return ("[" +
value.map(function (i) {
return "\n\t" + stringify(i);
}) +
"\n]");
}
return value;
})
.replace(/\\"/g, '"')
.replace(/\\t/g, "\t")
.replace(/\\n/g, "\n");
}
function deleteErrorNotificationStack(marble) {
const { notification } = marble;
if (notification) {
const { kind, error } = notification;
if (kind === "E" && error instanceof Error) {
notification.error = { name: error.name, message: error.message };
}
}
return marble;
}
export function observableMatcher(actual, expected, assert, assertDeepEqual, frameworkMatcher) {
if (Array.isArray(actual) && Array.isArray(expected)) {
actual = actual.map(deleteErrorNotificationStack);
expected = expected.map(deleteErrorNotificationStack);
if (frameworkMatcher) {
assertDeepEqual(actual, expected);
}
else {
const passed = circularDeepEqual(actual, expected);
if (passed) {
assert(true, "");
return;
}
let message = "\nExpected \n";
actual.forEach((x) => (message += `\t${stringify(x)}\n`));
message += "\t\nto deep equal \n";
expected.forEach((x) => (message += `\t${stringify(x)}\n`));
assert(passed, message);
}
}
else {
assertDeepEqual(actual, expected);
}
}