iko-karma-snapshot
Version:
Karma snapshot plugin for iko library
46 lines • 1.55 kB
JavaScript
import { Assertion, AssertionError, diff, errMsg } from "iko";
var context = window.__mocha_context__;
var snapshotState = window.__snapshot__;
function snapshotPath(node) {
var path = [];
while (node && node.parent) {
path.push(node.title);
node = node.parent;
}
return path.reverse();
}
Assertion.prototype.toMatchSnapshot = function (update) {
var received = this.toSnapshot();
var lang;
if (typeof received === "object") {
lang = received.lang;
received = received.code;
}
var path = snapshotPath(context.test);
var index = context.index++;
if (update || snapshotState.update) {
snapshotState.set(path, index, received, lang);
}
else {
var snapshot = snapshotState.get(path, index);
if (!snapshot) {
snapshotState.set(path, index, received, lang);
}
else {
var expected = snapshot.code;
var pass = snapshotState.match(received, expected);
if (!pass) {
var diffText = diff(expected, received);
var message = errMsg()
.matcherHint("toMatchSnapshot", "received", "")
.info("Expected value to match snapshot " + index + "\n");
if (diffText !== null) {
message.diff("\n", diffText);
}
throw new AssertionError(message.compose(), this.toMatchSnapshot);
}
}
}
return this;
};
//# sourceMappingURL=index.js.map