-
Notifications
You must be signed in to change notification settings - Fork 1
/
chai_json_equal.js
41 lines (35 loc) · 1.09 KB
/
chai_json_equal.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
function jsonify (x) {
return JSON.parse(JSON.stringify(x));
}
module.exports = function (chai, utils) {
chai.Assertion.addChainableMethod('jsonEqual',
function (x) {
var expected = jsonify(x),
actual = jsonify(this._obj);
this.assert(
utils.eql(actual, expected),
'expected #{act} to have the same json representation as #{exp}',
'expected #{act} to have a different json representation than #{exp}',
expected,
actual
);
}, function () {
utils.flag(this, 'jsonEqual', true);
}
);
function transformBeforeAssert (_super) {
return function (expected) {
var jsonEqualEnabled = utils.flag(this, 'jsonEqual');
if (jsonEqualEnabled) {
expected = jsonify(expected);
utils.flag(this, 'object', jsonify(this._obj));
}
_super.call(this, expected);
};
}
function passThrough (_super) {
return _super;
}
chai.Assertion.overwriteChainableMethod('contain', transformBeforeAssert, passThrough);
chai.Assertion.overwriteMethod('members', transformBeforeAssert);
};