Fix toBeCloseTo matcher for Node.js 12 and Chrome 74

This commit is contained in:
Paul van Brenk
2019-05-19 12:46:32 -04:00
parent e04d3d8a62
commit 69a7449e50
2 changed files with 15 additions and 1 deletions

View File

@@ -90,4 +90,18 @@ describe("toBeCloseTo", function() {
result = matcher.compare(1.23, 1.234);
expect(result.pass).toBe(true);
});
it("handles edge cases with rounding", function () {
var matcher = jasmineUnderTest.matchers.toBeCloseTo(),
result;
// these cases resulted in false negatives in version of V8
// included in Node.js 12 and Chrome 74 (and Edge Chromium)
result = matcher.compare(4.030904708957288, 4.0309, 5);
expect(result.pass).toBe(true);
result = matcher.compare(4.82665525779431,4.82666, 5);
expect(result.pass).toBe(true);
result = matcher.compare(-2.82665525779431, -2.82666, 5);
expect(result.pass).toBe(true);
});
});

View File

@@ -26,7 +26,7 @@ getJasmineRequireObj().toBeCloseTo = function() {
var maxDelta = Math.pow(10, -precision) / 2;
return {
pass: Math.round(delta * pow) / pow <= maxDelta
pass: Math.round(delta * pow) <= maxDelta * pow
};
}
};