Use timeout objects when in node

Fixes: https://github.com/jasmine/jasmine/issues/1469
This commit is contained in:
Chris Young
2017-12-21 10:51:07 -08:00
parent 65b4499dec
commit 62b815c485
2 changed files with 83 additions and 15 deletions

View File

@@ -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;
};