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

support util.promisify in Node #292

Merged
merged 1 commit into from
Mar 23, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function withGlobal(_global) {
hrtimePresent && typeof _global.process.hrtime.bigint === "function";
var nextTickPresent =
_global.process && typeof _global.process.nextTick === "function";
var utilPromisify = _global.process && require("util").promisify;
var performancePresent =
_global.performance && typeof _global.performance.now === "function";
var hasPerformancePrototype =
Expand Down Expand Up @@ -743,6 +744,21 @@ function withGlobal(_global) {
delay: timeout
});
};
if (typeof _global.Promise !== "undefined" && utilPromisify) {
clock.setTimeout[
utilPromisify.custom
] = function promisifiedSetTimeout(timeout, arg) {
return new _global.Promise(function setTimeoutExecutor(
resolve
) {
addTimer(clock, {
func: resolve,
args: [arg],
delay: timeout
});
});
};
}

clock.clearTimeout = function clearTimeout(timerId) {
return clearTimer(clock, timerId, "Timeout");
Expand Down Expand Up @@ -783,6 +799,22 @@ function withGlobal(_global) {
});
};

if (typeof _global.Promise !== "undefined" && utilPromisify) {
clock.setImmediate[
utilPromisify.custom
] = function promisifiedSetImmediate(arg) {
return new _global.Promise(function setImmediateExecutor(
resolve
) {
addTimer(clock, {
func: resolve,
args: [arg],
immediate: true
});
});
};
}

clock.clearImmediate = function clearImmediate(timerId) {
return clearTimer(clock, timerId, "Immediate");
};
Expand Down
60 changes: 60 additions & 0 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var performanceMarkPresent =
global.performance && typeof global.performance.mark === "function";
var setImmediatePresent =
global.setImmediate && typeof global.setImmediate === "function";
var utilPromisify = global.process && require("util").promisify;

describe("issue #59", function() {
var context = {
Expand Down Expand Up @@ -445,6 +446,36 @@ describe("FakeTimers", function() {
this.clock.runAll();
assert.equals(calls, ["NaN", "Infinity", "-Infinity"]);
});

if (typeof global.Promise !== "undefined" && utilPromisify) {
describe("when util.promisified", function() {
it("sets timers on instance", function() {
var resolved = false;
utilPromisify(this.clock.setTimeout)(100).then(function() {
resolved = true;
});

return this.clock.tickAsync(100).then(function() {
assert.isTrue(resolved);
});
});

it("resolves with the first additional argument to setTimeout", function() {
var resolvedValue;
utilPromisify(this.clock.setTimeout)(
100,
"the first",
"the second"
).then(function(value) {
resolvedValue = value;
});

return this.clock.tickAsync(100).then(function() {
assert.equals(resolvedValue, "the first");
});
});
});
}
});

describe("setImmediate", function() {
Expand Down Expand Up @@ -527,6 +558,35 @@ describe("FakeTimers", function() {

clock.tick(0);
});

if (typeof global.Promise !== "undefined" && utilPromisify) {
describe("when util.promisified", function() {
it("calls the given callback immediately", function() {
var resolved = false;
utilPromisify(this.clock.setImmediate)().then(function() {
resolved = true;
});

return this.clock.tickAsync(0).then(function() {
assert.isTrue(resolved);
});
});

it("resolves with the first argument to setImmediate", function() {
var resolvedValue;
utilPromisify(this.clock.setImmediate)(
"the first",
"the second"
).then(function(value) {
resolvedValue = value;
});

return this.clock.tickAsync(0).then(function() {
assert.equals(resolvedValue, "the first");
});
});
});
}
});

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