From 0650e29ac9ce8d6ce8d1375f9a2c3bf7ca51443e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominykas=20Blyz=CC=8Ce=CC=87?= Date: Fri, 19 Apr 2019 16:49:17 +0300 Subject: [PATCH] Fall back to setTimeout when setImmediate is not available --- package.json | 2 +- src/lolex-src.js | 16 ++++++++-------- test/lolex-test.js | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 039525e0..5eb61d52 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "scripts": { "lint": "eslint .", "test-node": "mocha test/ integration-test/ -R dot --check-leaks", - "test-headless": "mochify", + "test-headless": "mochify --timeout=10000", "test-cloud": "mochify --wd", "test": "npm run lint && npm run test-node && npm run test-headless", "bundle": "browserify -s lolex -o lolex.js src/lolex-src.js", diff --git a/src/lolex-src.js b/src/lolex-src.js index c82f1468..d7268758 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -524,7 +524,7 @@ function withGlobal(_global) { }; - var globalSetImmediate = global.setImmediate; + var originalSetTimeout = _global.setImmediate || _global.setTimeout; /** * @param start {Date|number} the system time - non-integer values are floored @@ -722,7 +722,7 @@ function withGlobal(_global) { // finish up after native setImmediate callback to allow // all native es6 promises to process their callbacks after // each timer fires. - globalSetImmediate(nextPromiseTick); + originalSetTimeout(nextPromiseTick); return; } @@ -742,7 +742,7 @@ function withGlobal(_global) { } clock.duringTick = false; - // corner case: during runJobs, new timers were scheduled which could be in the range [clock.now, tickTo] + // corner case: during runJobs new timers were scheduled which could be in the range [clock.now, tickTo] timer = firstTimerInRange(clock, tickFrom, tickTo); if (timer) { try { @@ -805,7 +805,7 @@ function withGlobal(_global) { if (typeof global.Promise !== "undefined") { clock.tickAsync = function tickAsync(ms) { return new global.Promise(function (resolve, reject) { - globalSetImmediate(function () { + originalSetTimeout(function () { try { doTick(ms, true, resolve, reject); } catch (e) { @@ -837,7 +837,7 @@ function withGlobal(_global) { if (typeof global.Promise !== "undefined") { clock.nextAsync = function nextAsync() { return new global.Promise(function (resolve, reject) { - globalSetImmediate(function () { + originalSetTimeout(function () { try { var timer = firstTimer(clock); if (!timer) { @@ -855,7 +855,7 @@ function withGlobal(_global) { } clock.duringTick = false; - globalSetImmediate(function () { + originalSetTimeout(function () { if (err) { reject(err); } else { @@ -898,7 +898,7 @@ function withGlobal(_global) { return new global.Promise(function (resolve, reject) { var i = 0; function doRun() { - globalSetImmediate(function () { + originalSetTimeout(function () { try { var numTimers; if (i < clock.loopLimit) { @@ -946,7 +946,7 @@ function withGlobal(_global) { if (typeof global.Promise !== "undefined") { clock.runToLastAsync = function runToLastAsync() { return new global.Promise(function (resolve, reject) { - globalSetImmediate(function () { + originalSetTimeout(function () { try { var timer = lastTimer(clock); if (!timer) { diff --git a/test/lolex-test.js b/test/lolex-test.js index 268148d4..9087b858 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -960,7 +960,7 @@ describe("lolex", function () { describe("tickAsync", function () { beforeEach(function () { - this.clock = lolex.install(0); + this.clock = lolex.install(); }); afterEach(function () { @@ -1846,7 +1846,7 @@ describe("lolex", function () { describe("nextAsync", function () { beforeEach(function () { - this.clock = lolex.install(0); + this.clock = lolex.install(); }); afterEach(function () { @@ -2400,7 +2400,7 @@ describe("lolex", function () { }); it("the loop limit can be set when installing a clock", function () { - this.clock = lolex.install(0, null, null, 1); + this.clock = lolex.install({ loopLimit: 1 }); var test = this; var catchSpy = sinon.spy();