-
Notifications
You must be signed in to change notification settings - Fork 108
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
Set timeout promise support #227
Closed
+47
−12
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ var GlobalDate = Date; | |
var NOOP = function NOOP() { return undefined; }; | ||
var nextTickPresent = (global.process && typeof global.process.nextTick === "function"); | ||
var queueMicrotaskPresent = (typeof global.queueMicrotask === "function"); | ||
var utilPromisify = (global.process && typeof require === "function" && require("util") && require("util").promisify); | ||
var hrtimePresent = (global.process && typeof global.process.hrtime === "function"); | ||
var performanceNowPresent = (global.performance && typeof global.performance.now === "function"); | ||
var performanceMarkPresent = (global.performance && typeof global.performance.mark === "function"); | ||
|
@@ -206,13 +207,10 @@ describe("lolex", function () { | |
|
||
describe("setTimeout", function () { | ||
|
||
var evalCalled; | ||
beforeEach(function () { | ||
this.clock = lolex.createClock(); | ||
lolex.evalCalled = false; | ||
}); | ||
|
||
afterEach(function () { | ||
delete lolex.evalCalled; | ||
evalCalled = false; | ||
}); | ||
|
||
it("throws if no arguments", function () { | ||
|
@@ -252,24 +250,28 @@ describe("lolex", function () { | |
}); | ||
|
||
it("parses numeric string times", function () { | ||
this.clock.setTimeout(function () { lolex.evalCalled = true; }, "10"); | ||
this.clock.setTimeout(function () { evalCalled = true; }, "10"); | ||
this.clock.tick(10); | ||
|
||
assert(lolex.evalCalled); | ||
assert(evalCalled); | ||
}); | ||
|
||
it("parses no-numeric string times", function () { | ||
this.clock.setTimeout(function () { lolex.evalCalled = true; }, "string"); | ||
this.clock.setTimeout(function () { evalCalled = true; }, "string"); | ||
this.clock.tick(10); | ||
|
||
assert(lolex.evalCalled); | ||
assert(evalCalled); | ||
}); | ||
|
||
it("evals non-function callbacks", function () { | ||
this.clock.setTimeout("lolex.evalCalled = true", 10); | ||
global.evalFn = function () { | ||
evalCalled = true; | ||
}; | ||
this.clock.setTimeout("evalFn()", 10); | ||
this.clock.tick(10); | ||
|
||
assert(lolex.evalCalled); | ||
assert(evalCalled); | ||
delete global.evalFn; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete this before the assertion so that it's guaranteed to get called. If the assertion above fails, this won't be cleaned up. |
||
}); | ||
|
||
it("passes setTimeout parameters", function () { | ||
|
@@ -393,6 +395,17 @@ describe("lolex", function () { | |
this.clock.runAll(); | ||
assert.equals(calls, ["NaN", "Infinity", "-Infinity"]); | ||
}); | ||
it("Handles promisification of setTimeout", function () { | ||
if (!utilPromisify) { | ||
this.skip(); | ||
} | ||
var timeout = utilPromisify(this.clock.setTimeout); | ||
return global.Promise.resolve().then(function () { | ||
var p1 = timeout(1e6); | ||
this.clock.tick(1e6); | ||
return p1; | ||
}.bind(this)); | ||
}); | ||
}); | ||
|
||
describe("setImmediate", function () { | ||
|
@@ -1922,6 +1935,19 @@ describe("lolex", function () { | |
} | ||
}); | ||
|
||
it("global fake setTimeout with util promisify should work", function () { | ||
if (!utilPromisify) { | ||
this.skip(); | ||
} | ||
this.clock = lolex.install(); | ||
var delay = utilPromisify(setTimeout); | ||
return global.Promise.resolve().then(function () { | ||
var p = delay(1e6); | ||
this.clock.tick(1e6); | ||
return p; | ||
}.bind(this)); | ||
}); | ||
|
||
it("global fake setTimeout().unref() should return timer", function () { | ||
this.clock = lolex.install(); | ||
var stub = sinon.stub(); | ||
|
@@ -2545,7 +2571,7 @@ describe("lolex", function () { | |
}); | ||
it("runs with timers and before them", function () { | ||
var last = ""; | ||
clock.runMicrotasks(function () { | ||
clock.queueMicrotask(function () { | ||
called = true; | ||
last = "tick"; | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you store a reference to
Promise
at initialization time? Would help in Jest in the cases where people mess with globalsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, can you elaborate on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People have tests that do
inside of their test. It would be great to be resilient to that by grabbing a copy of the original
Promise
early. Use case being to test how their code behaves without promises availableThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SimenB You're basically just saying that
lolex
should do something like this, right?And then reference that when using
Promise
s internally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes 🙂