From 62b815c485fbe2b62e6ba67e827cbf9144da393c Mon Sep 17 00:00:00 2001 From: Chris Young Date: Thu, 21 Dec 2017 10:51:07 -0800 Subject: [PATCH] Use timeout objects when in node Fixes: https://github.com/jasmine/jasmine/issues/1469 --- spec/core/ClockSpec.js | 61 +++++++++++++++++++++++++++++++++--------- src/core/Clock.js | 37 +++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 15 deletions(-) diff --git a/spec/core/ClockSpec.js b/spec/core/ClockSpec.js index 6b3ed1d0..bcce2fa1 100644 --- a/spec/core/ClockSpec.js +++ b/spec/core/ClockSpec.js @@ -1,5 +1,7 @@ describe("Clock", function() { + var NODE_JS = typeof process !== 'undefined' && process.versions && typeof process.versions.node === 'string'; + it("does not replace setTimeout until it is installed", function() { var fakeSetTimeout = jasmine.createSpy("global setTimeout"), fakeGlobal = { setTimeout: fakeSetTimeout }, @@ -294,13 +296,19 @@ describe("Clock", function() { fakeGlobal = { setTimeout: fakeSetTimeout }, delayedFn = jasmine.createSpy('delayedFn'), mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} }, - clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate); + clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate), + timeout = new clock.FakeTimeout(); clock.install(); clock.setTimeout(delayedFn, 0, 'a', 'b'); expect(fakeSetTimeout).not.toHaveBeenCalled(); - expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b']); + + if (!NODE_JS) { + expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b']); + } else { + expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], false, timeout); + } }); it("returns an id for the delayed function", function() { @@ -312,12 +320,16 @@ describe("Clock", function() { delayedFn = jasmine.createSpy('delayedFn'), mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} }, clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate), - timeoutId; + timeout; clock.install(); - timeoutId = clock.setTimeout(delayedFn, 0); + timeout = clock.setTimeout(delayedFn, 0); - expect(timeoutId).toEqual(123); + if (!NODE_JS) { + expect(timeout).toEqual(123); + } else { + expect(timeout.constructor.name).toEqual('FakeTimeout'); + } }); it("clears the scheduled function with the scheduler", function() { @@ -342,13 +354,19 @@ describe("Clock", function() { fakeGlobal = { setInterval: fakeSetInterval }, delayedFn = jasmine.createSpy('delayedFn'), mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} }, - clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate); + clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate), + timeout = new clock.FakeTimeout; clock.install(); clock.setInterval(delayedFn, 0, 'a', 'b'); expect(fakeSetInterval).not.toHaveBeenCalled(); - expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], true); + + if (!NODE_JS) { + expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], true); + } else { + expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], true, timeout); + } }); it("returns an id for the delayed function", function() { @@ -360,12 +378,16 @@ describe("Clock", function() { delayedFn = jasmine.createSpy('delayedFn'), mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} }, clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate), - intervalId; + interval; clock.install(); - intervalId = clock.setInterval(delayedFn, 0); + interval = clock.setInterval(delayedFn, 0); - expect(intervalId).toEqual(123); + if (!NODE_JS) { + expect(interval).toEqual(123); + } else { + expect(interval.constructor.name).toEqual('FakeTimeout'); + } }); it("clears the scheduled function with the scheduler", function() { @@ -401,7 +423,8 @@ describe("Clock", function() { setInterval: fakeSetInterval }, mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} }, - clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate); + clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate), + timeout = new clock.FakeTimeout(); fakeSetTimeout.apply = null; fakeSetInterval.apply = null; @@ -409,13 +432,25 @@ describe("Clock", function() { clock.install(); clock.setTimeout(fn, 0); - expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, []); + + if (!NODE_JS) { + expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, []); + } else { + expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, [], false, timeout); + } + expect(function() { clock.setTimeout(fn, 0, 'extra'); }).toThrow(); clock.setInterval(fn, 0); - expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, [], true); + + if (!NODE_JS) { + expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, [], true); + } else { + expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, [], true, timeout); + } + expect(function() { clock.setInterval(fn, 0, 'extra'); }).toThrow(); diff --git a/src/core/Clock.js b/src/core/Clock.js index 52fafcfe..21829c87 100644 --- a/src/core/Clock.js +++ b/src/core/Clock.js @@ -1,4 +1,7 @@ getJasmineRequireObj().Clock = function() { + + var NODE_JS = typeof process !== 'undefined' && process.versions && typeof process.versions.node === 'string'; + /** * _Note:_ Do not construct this directly, Jasmine will make one during booting. You can get the current clock with {@link jasmine.clock}. * @class Clock @@ -22,6 +25,7 @@ getJasmineRequireObj().Clock = function() { delayedFunctionScheduler, timer; + self.FakeTimeout = FakeTimeout; /** * Install the mock clock over the built-in methods. @@ -145,7 +149,15 @@ getJasmineRequireObj().Clock = function() { } function setTimeout(fn, delay) { - return delayedFunctionScheduler.scheduleFunction(fn, delay, argSlice(arguments, 2)); + if (!NODE_JS) { + return delayedFunctionScheduler.scheduleFunction(fn, delay, argSlice(arguments, 2)); + } + + var timeout = new FakeTimeout(); + + delayedFunctionScheduler.scheduleFunction(fn, delay, argSlice(arguments, 2), false, timeout); + + return timeout; } function clearTimeout(id) { @@ -153,7 +165,15 @@ getJasmineRequireObj().Clock = function() { } function setInterval(fn, interval) { - return delayedFunctionScheduler.scheduleFunction(fn, interval, argSlice(arguments, 2), true); + if (!NODE_JS) { + return delayedFunctionScheduler.scheduleFunction(fn, interval, argSlice(arguments, 2), true); + } + + var timeout = new FakeTimeout(); + + delayedFunctionScheduler.scheduleFunction(fn, interval, argSlice(arguments, 2), true, timeout); + + return timeout; } function clearInterval(id) { @@ -165,5 +185,18 @@ getJasmineRequireObj().Clock = function() { } } + /** + * Mocks Node.js Timeout class + */ + function FakeTimeout() {} + + FakeTimeout.prototype.ref = function () { + return this; + }; + + FakeTimeout.prototype.unref = function () { + return this; + }; + return Clock; };