Use timeout objects when in node
Fixes: https://github.com/jasmine/jasmine/issues/1469
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
describe("Clock", function() {
|
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() {
|
it("does not replace setTimeout until it is installed", function() {
|
||||||
var fakeSetTimeout = jasmine.createSpy("global setTimeout"),
|
var fakeSetTimeout = jasmine.createSpy("global setTimeout"),
|
||||||
fakeGlobal = { setTimeout: fakeSetTimeout },
|
fakeGlobal = { setTimeout: fakeSetTimeout },
|
||||||
@@ -294,13 +296,19 @@ describe("Clock", function() {
|
|||||||
fakeGlobal = { setTimeout: fakeSetTimeout },
|
fakeGlobal = { setTimeout: fakeSetTimeout },
|
||||||
delayedFn = jasmine.createSpy('delayedFn'),
|
delayedFn = jasmine.createSpy('delayedFn'),
|
||||||
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
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.install();
|
||||||
clock.setTimeout(delayedFn, 0, 'a', 'b');
|
clock.setTimeout(delayedFn, 0, 'a', 'b');
|
||||||
|
|
||||||
expect(fakeSetTimeout).not.toHaveBeenCalled();
|
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() {
|
it("returns an id for the delayed function", function() {
|
||||||
@@ -312,12 +320,16 @@ describe("Clock", function() {
|
|||||||
delayedFn = jasmine.createSpy('delayedFn'),
|
delayedFn = jasmine.createSpy('delayedFn'),
|
||||||
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
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),
|
||||||
timeoutId;
|
timeout;
|
||||||
|
|
||||||
clock.install();
|
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() {
|
it("clears the scheduled function with the scheduler", function() {
|
||||||
@@ -342,13 +354,19 @@ describe("Clock", function() {
|
|||||||
fakeGlobal = { setInterval: fakeSetInterval },
|
fakeGlobal = { setInterval: fakeSetInterval },
|
||||||
delayedFn = jasmine.createSpy('delayedFn'),
|
delayedFn = jasmine.createSpy('delayedFn'),
|
||||||
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
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.install();
|
||||||
clock.setInterval(delayedFn, 0, 'a', 'b');
|
clock.setInterval(delayedFn, 0, 'a', 'b');
|
||||||
|
|
||||||
expect(fakeSetInterval).not.toHaveBeenCalled();
|
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() {
|
it("returns an id for the delayed function", function() {
|
||||||
@@ -360,12 +378,16 @@ describe("Clock", function() {
|
|||||||
delayedFn = jasmine.createSpy('delayedFn'),
|
delayedFn = jasmine.createSpy('delayedFn'),
|
||||||
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
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),
|
||||||
intervalId;
|
interval;
|
||||||
|
|
||||||
clock.install();
|
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() {
|
it("clears the scheduled function with the scheduler", function() {
|
||||||
@@ -401,7 +423,8 @@ describe("Clock", function() {
|
|||||||
setInterval: fakeSetInterval
|
setInterval: fakeSetInterval
|
||||||
},
|
},
|
||||||
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
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;
|
fakeSetTimeout.apply = null;
|
||||||
fakeSetInterval.apply = null;
|
fakeSetInterval.apply = null;
|
||||||
@@ -409,13 +432,25 @@ describe("Clock", function() {
|
|||||||
clock.install();
|
clock.install();
|
||||||
|
|
||||||
clock.setTimeout(fn, 0);
|
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() {
|
expect(function() {
|
||||||
clock.setTimeout(fn, 0, 'extra');
|
clock.setTimeout(fn, 0, 'extra');
|
||||||
}).toThrow();
|
}).toThrow();
|
||||||
|
|
||||||
clock.setInterval(fn, 0);
|
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() {
|
expect(function() {
|
||||||
clock.setInterval(fn, 0, 'extra');
|
clock.setInterval(fn, 0, 'extra');
|
||||||
}).toThrow();
|
}).toThrow();
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
getJasmineRequireObj().Clock = function() {
|
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}.
|
* _Note:_ Do not construct this directly, Jasmine will make one during booting. You can get the current clock with {@link jasmine.clock}.
|
||||||
* @class Clock
|
* @class Clock
|
||||||
@@ -22,6 +25,7 @@ getJasmineRequireObj().Clock = function() {
|
|||||||
delayedFunctionScheduler,
|
delayedFunctionScheduler,
|
||||||
timer;
|
timer;
|
||||||
|
|
||||||
|
self.FakeTimeout = FakeTimeout;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Install the mock clock over the built-in methods.
|
* Install the mock clock over the built-in methods.
|
||||||
@@ -145,7 +149,15 @@ getJasmineRequireObj().Clock = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setTimeout(fn, delay) {
|
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) {
|
function clearTimeout(id) {
|
||||||
@@ -153,7 +165,15 @@ getJasmineRequireObj().Clock = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setInterval(fn, interval) {
|
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) {
|
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;
|
return Clock;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user