-
Notifications
You must be signed in to change notification settings - Fork 0
/
jestExpectExtend.js
47 lines (45 loc) · 1.42 KB
/
jestExpectExtend.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
const Helpers = require('./lib/helpers');
expect.extend({
shallowEpsilonEquals: (expected, other, precision) => {
let expectedValue;
let otherValue;
let errorKey;
const comparisionPrecision = (precision !== undefined) ? precision : 14;
const pass = Object.keys(other).every((key) => {
errorKey = key;
expectedValue = expected[key].toFixed(comparisionPrecision);
otherValue = other[key].toFixed(comparisionPrecision);
return (Math.abs(otherValue - expectedValue) < (Helpers.EPSILON));
});
if (pass) {
return {
message: () => (`expected ${expectedValue} NOT to be close to ${otherValue} for [${errorKey}]`),
pass: true,
};
}
return {
message: () => (`expected ${expectedValue} to be close to ${otherValue} for [${errorKey}]`),
pass: false,
};
},
matrixEquals: (expected, other, precision) => {
let expectedValue;
let otherValue;
let errorKey;
const pass = expected.data.every((value, i) => {
errorKey = i;
expectedValue = expected.data[i].toFixed(precision || 14);
otherValue = other.data[i].toFixed(precision || 14);
return (Math.abs(otherValue - expectedValue)) < Helpers.EPSILON;
});
if (pass) {
return {
pass: true,
};
}
return {
message: () => (`expected ${expectedValue} to be close to ${otherValue} for [${errorKey}]`),
pass: false,
};
},
});