Skip to content

Commit

Permalink
fix: handle negative infinity timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed May 8, 2018
1 parent 60b7228 commit ee17cdf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lolex-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ function withGlobal(_global) {
var NativeDate = Date;
var uniqueTimerId = 1;

function isNumberFinite(num) {
if (Number.isFinite) {
return Number.isFinite(num);
}

if (typeof num !== "number") {
return false;
}

return isFinite(num);
}

/**
* Parse strings like "01:10:00" (meaning 1 hour, 10 minutes, 0 seconds) into
* number of milliseconds. This is used to support human-readable strings passed
Expand Down Expand Up @@ -201,6 +213,9 @@ function withGlobal(_global) {
timer.type = timer.immediate ? "Immediate" : "Timeout";

if (timer.hasOwnProperty("delay")) {
if (!isNumberFinite(timer.delay)) {
timer.delay = 0;
}
timer.delay = timer.delay > maxTimeout ? 1 : timer.delay;
timer.delay = Math.max(0, timer.delay);
}
Expand Down
14 changes: 14 additions & 0 deletions test/lolex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ describe("lolex", function () {
this.clock.tick(61);
assert.equals(callbackCalled, true);
});
it("handles Infinity and negative Infinity correctly", function () {
var calls = [];
this.clock.setTimeout(function () {
calls.push("NaN");
}, NaN);
this.clock.setTimeout(function () {
calls.push("Infinity");
}, Number.POSITIVE_INFINITY);
this.clock.setTimeout(function () {
calls.push("-Infinity");
}, Number.NEGATIVE_INFINITY);
this.clock.runAll();
assert.equals(calls, ["NaN", "Infinity", "-Infinity"]);
});
});

describe("setImmediate", function () {
Expand Down

0 comments on commit ee17cdf

Please sign in to comment.