From 0d59f6c056b2d988ef878ebf6cea65491d388dde Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Wed, 14 Nov 2018 16:55:06 +0200 Subject: [PATCH 1/2] initial support in non-global clock --- src/lolex-src.js | 8 ++++++++ test/lolex-test.js | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lolex-src.js b/src/lolex-src.js index 5385abed..5582fd06 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -31,6 +31,7 @@ function withGlobal(_global) { var addTimerReturnsObject = typeof timeoutResult === "object"; var hrtimePresent = (_global.process && typeof _global.process.hrtime === "function"); var nextTickPresent = (_global.process && typeof _global.process.nextTick === "function"); + var symbolUtilPromisifyCustom = (_global.process && typeof require === "function" && require("util") && require("util").promisify.custom); var performancePresent = (_global.performance && typeof _global.performance.now === "function"); var hasPerformancePrototype = (_global.Performance && (typeof _global.Performance).match(/^(function|object)$/)); var queueMicrotaskPresent = (typeof _global.queueMicrotask === "function"); @@ -568,6 +569,13 @@ function withGlobal(_global) { delay: timeout }); }; + if (symbolUtilPromisifyCustom) { + clock.setTimeout[symbolUtilPromisifyCustom] = function (delay) { + return new _global.Promise(function (resolve) { + return clock.setTimeout(resolve, delay); + }); + }; + } clock.clearTimeout = function clearTimeout(timerId) { return clearTimer(clock, timerId, "Timeout"); diff --git a/test/lolex-test.js b/test/lolex-test.js index a877de99..d9c43377 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -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"); @@ -393,6 +394,15 @@ describe("lolex", function () { this.clock.runAll(); assert.equals(calls, ["NaN", "Infinity", "-Infinity"]); }); + it("Handles promisification of setTimeout", function () { + if(!utilPromisify) this.skip(); + let timeout = utilPromisify(this.clock.setTimeout); + return Promise.resolve().then(function () { + var p1 = timeout(1e6); + this.clock.tick(1e6); + return p1; + }.bind(this)); + }); }); describe("setImmediate", function () { @@ -2545,7 +2555,7 @@ describe("lolex", function () { }); it("runs with timers and before them", function () { var last = ""; - clock.runMicrotasks(function () { + clock.queueMicrotask(function () { called = true; last = "tick"; }); From e766de8b337bcb0a897d924c674e55f6babd3488 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Wed, 14 Nov 2018 16:57:49 +0200 Subject: [PATCH 2/2] install support --- src/lolex-src.js | 3 ++- test/lolex-test.js | 46 +++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/lolex-src.js b/src/lolex-src.js index 5582fd06..49bd0fbf 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -31,7 +31,8 @@ function withGlobal(_global) { var addTimerReturnsObject = typeof timeoutResult === "object"; var hrtimePresent = (_global.process && typeof _global.process.hrtime === "function"); var nextTickPresent = (_global.process && typeof _global.process.nextTick === "function"); - var symbolUtilPromisifyCustom = (_global.process && typeof require === "function" && require("util") && require("util").promisify.custom); + var nodeUtil = (_global.process && typeof require === "function" && require("util")); + var symbolUtilPromisifyCustom = nodeUtil && nodeUtil.promisify && nodeUtil.promisify.custom; var performancePresent = (_global.performance && typeof _global.performance.now === "function"); var hasPerformancePrototype = (_global.Performance && (typeof _global.Performance).match(/^(function|object)$/)); var queueMicrotaskPresent = (typeof _global.queueMicrotask === "function"); diff --git a/test/lolex-test.js b/test/lolex-test.js index d9c43377..d06ce2fd 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -20,7 +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 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"); @@ -207,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 () { @@ -253,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; }); it("passes setTimeout parameters", function () { @@ -395,9 +396,11 @@ describe("lolex", function () { assert.equals(calls, ["NaN", "Infinity", "-Infinity"]); }); it("Handles promisification of setTimeout", function () { - if(!utilPromisify) this.skip(); - let timeout = utilPromisify(this.clock.setTimeout); - return Promise.resolve().then(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; @@ -1932,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();