diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index f74f8ed7..4ea8cd73 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -5961,6 +5961,14 @@ getJasmineRequireObj().toBeCloseTo = function() { ); } + // Infinity is close to Infinity and -Infinity is close to -Infinity, + // regardless of the precision. + if (expected === Infinity || expected === -Infinity) { + return { + pass: actual === expected + }; + } + var pow = Math.pow(10, precision + 1); var delta = Math.abs(expected - actual); var maxDelta = Math.pow(10, -precision) / 2; diff --git a/spec/core/matchers/toBeCloseToSpec.js b/spec/core/matchers/toBeCloseToSpec.js index 2594e9ef..d2d18c23 100644 --- a/spec/core/matchers/toBeCloseToSpec.js +++ b/spec/core/matchers/toBeCloseToSpec.js @@ -110,4 +110,42 @@ describe('toBeCloseTo', function() { result = matcher.compare(-2.82665525779431, -2.82666, 5); expect(result.pass).toBe(true); }); + + describe('Infinity handling', function() { + it('passes when the actual and expected are both Infinity', function() { + const matcher = jasmineUnderTest.matchers.toBeCloseTo(); + const result = matcher.compare(Infinity, Infinity, 0); + expect(result.pass).toBe(true); + }); + + it('passes when the actual and expected are both -Infinity', function() { + const matcher = jasmineUnderTest.matchers.toBeCloseTo(); + const result = matcher.compare(-Infinity, -Infinity, 0); + expect(result.pass).toBe(true); + }); + + it('fails when the actual is Infinity and the expected is -Infinity', function() { + const matcher = jasmineUnderTest.matchers.toBeCloseTo(); + const result = matcher.compare(Infinity, -Infinity, 0); + expect(result.pass).toBe(false); + }); + + it('fails when the actual is -Infinity and the expected is Infinity', function() { + const matcher = jasmineUnderTest.matchers.toBeCloseTo(); + const result = matcher.compare(-Infinity, Infinity, 0); + expect(result.pass).toBe(false); + }); + + it('fails when the actual is a number and the expected is Infinity', function() { + const matcher = jasmineUnderTest.matchers.toBeCloseTo(); + const result = matcher.compare(42, Infinity, 0); + expect(result.pass).toBe(false); + }); + + it('fails when the actual is a number and the expected is -Infinity', function() { + const matcher = jasmineUnderTest.matchers.toBeCloseTo(); + const result = matcher.compare(42, -Infinity, 0); + expect(result.pass).toBe(false); + }); + }); }); diff --git a/src/core/matchers/toBeCloseTo.js b/src/core/matchers/toBeCloseTo.js index b5392144..a4345ad0 100644 --- a/src/core/matchers/toBeCloseTo.js +++ b/src/core/matchers/toBeCloseTo.js @@ -27,6 +27,14 @@ getJasmineRequireObj().toBeCloseTo = function() { ); } + // Infinity is close to Infinity and -Infinity is close to -Infinity, + // regardless of the precision. + if (expected === Infinity || expected === -Infinity) { + return { + pass: actual === expected + }; + } + var pow = Math.pow(10, precision + 1); var delta = Math.abs(expected - actual); var maxDelta = Math.pow(10, -precision) / 2;