chai-karma-snapshot
Advanced tools
Comparing version 0.4.2 to 0.5.0
102
lib/index.js
@@ -14,6 +14,2 @@ "use strict"; | ||
function normalizeNewlines(string) { | ||
return string.replace(/\r\n|\r/g, "\n"); | ||
} | ||
function addSerializer(plugin) { | ||
@@ -24,7 +20,7 @@ serializationPlugins = [plugin].concat(serializationPlugins); | ||
function serialize(data) { | ||
return normalizeNewlines(prettyFormat(data, { | ||
return prettyFormat(data, { | ||
escapeRegex: true, | ||
plugins: serializationPlugins, | ||
printFunctionName: false, | ||
})); | ||
}); | ||
} | ||
@@ -44,89 +40,23 @@ | ||
var snapshotState = window.__snapshot__; | ||
var newSnapshotState = window.__snapshot__ = { | ||
update: snapshotState.update, | ||
suite: { | ||
visited: false, | ||
dirty: false, | ||
children: {}, | ||
snapshots: {} | ||
} | ||
}; | ||
function getSnapshot(path, index) { | ||
var suite = snapshotState.suite; | ||
for (var i = 0; i < path.length - 1; i++) { | ||
var key = path[i]; | ||
var s = suite.children[key]; | ||
if (s === undefined) { | ||
return undefined; | ||
} else { | ||
suite = s; | ||
} | ||
} | ||
var testName = path[path.length - 1]; | ||
var snapshotList = suite.snapshots[testName]; | ||
if (snapshotList !== undefined) { | ||
return snapshotList[index]; | ||
} | ||
return undefined; | ||
} | ||
function addSnapshot(path, index, lang, code, dirty) { | ||
var suite = newSnapshotState.suite; | ||
suite.visited = true; | ||
if (dirty) { | ||
suite.dirty = true; | ||
} | ||
for (var i = 0; i < path.length - 1; i++) { | ||
var key = path[i]; | ||
var s = suite.children[key]; | ||
if (s === undefined) { | ||
suite.children[key] = suite = { | ||
visited: true, | ||
dirty: dirty, | ||
children: {}, | ||
snapshots: {}, | ||
}; | ||
} else { | ||
suite = s; | ||
suite.visited = true; | ||
if (dirty) { | ||
suite.dirty = true; | ||
} | ||
} | ||
} | ||
var testName = path[path.length - 1]; | ||
var snapshotList = suite.snapshots[testName]; | ||
if (snapshotList === undefined) { | ||
suite.snapshots[testName] = snapshotList = []; | ||
} | ||
snapshotList[index] = { | ||
visited: true, | ||
dirty: dirty, | ||
lang: lang, | ||
code: code | ||
}; | ||
} | ||
utils.addMethod(chai.Assertion.prototype, "matchSnapshot", function (lang, update) { | ||
if (lang === void 0) { | ||
lang = null; | ||
} | ||
var obj = serialize(chai.util.flag(this, "object")); | ||
var path = snapshotPath(context.runnable); | ||
var index = context.index++; | ||
var snapshot = getSnapshot(path, index); | ||
if (!update && !snapshotState.update && snapshot) { | ||
addSnapshot(path, index, lang, snapshot.code, false); | ||
if (obj !== snapshot.code) { | ||
throw new chai.AssertionError("Received value does not match stored snapshot.", { | ||
actual: obj, | ||
expected: snapshot.code, | ||
showDiff: true | ||
}, chai.util.flag(this, "ssfi")); | ||
if (update || snapshotState.update) { | ||
snapshotState.set(path, index, obj, lang); | ||
} else { | ||
var snapshot = snapshotState.get(path, index); | ||
if (!snapshot) { | ||
snapshotState.set(path, index, obj, lang); | ||
} else { | ||
if (obj !== snapshot.code) { | ||
throw new chai.AssertionError("Received value does not match stored snapshot " + index, { | ||
actual: obj, | ||
expected: snapshot.code, | ||
showDiff: true | ||
}, chai.util.flag(this, "ssfi")); | ||
} | ||
} | ||
} else { | ||
addSnapshot(path, index, lang, obj, true); | ||
} | ||
@@ -133,0 +63,0 @@ }); |
{ | ||
"name": "chai-karma-snapshot", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"main": "lib/index.js", | ||
@@ -33,2 +33,6 @@ "typings": "lib/index.d.ts", | ||
"scripts": {}, | ||
"peerDependencies": { | ||
"karma-snapshot": "^0.4.0", | ||
"karma-mocha-snapshot": "^0.2.1" | ||
}, | ||
"dependencies": { | ||
@@ -35,0 +39,0 @@ "babel-runtime": "^6.23.0", |
7769
4
66