-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
83 lines (72 loc) · 2.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var prettyFormat = require("pretty-format");
/**
* Serialization code were copied from Jest.
*
* https://github.com/facebook/jest/blob/6979b8e27e88735bfed997d84393f291c8a0a21d/packages/jest-snapshot/src/plugins.js#L23-L30
*/
var serializationPlugins = [
prettyFormat.plugins.DOMCollection,
prettyFormat.plugins.DOMElement,
prettyFormat.plugins.Immutable,
prettyFormat.plugins.ReactElement,
prettyFormat.plugins.ReactTestComponent,
];
function addSerializer(plugin) {
serializationPlugins = [plugin].concat(serializationPlugins);
}
function serialize(data) {
return prettyFormat(data, {
escapeRegex: true,
plugins: serializationPlugins,
printFunctionName: false,
});
}
function snapshotPath(node) {
var path = [];
while (node && node.parent) {
path.push(node.title);
node = node.parent;
}
return path.reverse();
}
function matchSnapshot(chai, utils) {
var context = window.__mocha_context__;
var snapshotState = window.__snapshot__;
utils.addMethod(chai.Assertion.prototype, "matchSnapshot", aMethodForExpect);
chai.assert.matchSnapshot = aMethodForAssert;
function aMethodForAssert(lang, update, msg) {
// This basically wraps the 'expect' version of the assertion to allow using 'assert' syntax.
return new chai.Assertion(lang, update, msg).to.matchSnapshot();
}
function aMethodForExpect(lang, update) {
var obj = serialize(chai.util.flag(this, "object"));
var index = context.index++;
var path;
// For a hook, use the currentTest for path
if (context.runnable.type === "hook") {
path = snapshotPath(context.runnable.ctx.currentTest);
} else {
path = snapshotPath(context.runnable);
}
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 (!snapshotState.match(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"));
}
}
}
}
}
exports.addSerializer = addSerializer;
exports.matchSnapshot = matchSnapshot;