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