Skip to content

Commit

Permalink
support util.promisify in Node
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Feb 5, 2020
1 parent e08b136 commit 705af40
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
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

0 comments on commit 705af40

Please sign in to comment.