Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle negative infinity timeout #165

Merged
merged 1 commit into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without the code change, -Infinity is before Infinity in the array

});
});

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