-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mocks to NodeJS "timers" module #467
Changes from 3 commits
35b61fa
f00cfcd
087acc1
4083bac
d84b829
b2cca27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
"use strict"; | ||
|
||
const globalObject = require("@sinonjs/commons").global; | ||
let timersModule; | ||
if (typeof require === "function" && typeof module === "object") { | ||
try { | ||
timersModule = require("timers"); | ||
} catch (e) { | ||
// ignored | ||
} | ||
} | ||
|
||
/** | ||
* @typedef {object} IdleDeadline | ||
|
@@ -39,13 +47,13 @@ | |
|
||
/** | ||
* @typedef RequestAnimationFrame | ||
* @property {function(number):void} requestAnimationFrame | ||
Check warning on line 50 in src/fake-timers-src.js GitHub Actions / lint
|
||
* @returns {number} - the id | ||
*/ | ||
|
||
/** | ||
* @typedef Performance | ||
* @property {function(): number} now | ||
Check warning on line 56 in src/fake-timers-src.js GitHub Actions / lint
|
||
*/ | ||
|
||
/* eslint-disable jsdoc/require-property-description */ | ||
|
@@ -317,7 +325,7 @@ | |
return timer && timer.callAt >= from && timer.callAt <= to; | ||
} | ||
|
||
/** | ||
Check warning on line 328 in src/fake-timers-src.js GitHub Actions / lint
|
||
* @param {Clock} clock | ||
* @param {Timer} job | ||
*/ | ||
|
@@ -619,7 +627,7 @@ | |
} | ||
|
||
/* eslint consistent-return: "off" */ | ||
/** | ||
Check warning on line 630 in src/fake-timers-src.js GitHub Actions / lint
|
||
* Timer comparitor | ||
* | ||
* @param {Timer} a | ||
|
@@ -751,7 +759,7 @@ | |
} | ||
} | ||
|
||
/** | ||
Check warning on line 762 in src/fake-timers-src.js GitHub Actions / lint
|
||
* Gets clear handler name for a given timer type | ||
* | ||
* @param {string} ttype | ||
|
@@ -763,7 +771,7 @@ | |
return `clear${ttype}`; | ||
} | ||
|
||
/** | ||
Check warning on line 774 in src/fake-timers-src.js GitHub Actions / lint
|
||
* Gets schedule handler name for a given timer type | ||
* | ||
* @param {string} ttype | ||
|
@@ -775,7 +783,7 @@ | |
return `set${ttype}`; | ||
} | ||
|
||
/** | ||
Check warning on line 786 in src/fake-timers-src.js GitHub Actions / lint
|
||
* Creates an anonymous function to warn only once | ||
*/ | ||
function createWarnOnce() { | ||
|
@@ -787,7 +795,7 @@ | |
} | ||
const warnOnce = createWarnOnce(); | ||
|
||
/** | ||
Check warning on line 798 in src/fake-timers-src.js GitHub Actions / lint
|
||
* @param {Clock} clock | ||
* @param {number} timerId | ||
* @param {string} ttype | ||
|
@@ -886,6 +894,14 @@ | |
} | ||
} | ||
} | ||
if (clock.timersModuleMocks) { | ||
for (const [ | ||
methodName, | ||
originalMethod, | ||
] of clock.timersModuleMocks) { | ||
timersModule[methodName] = originalMethod; | ||
} | ||
} | ||
} | ||
|
||
if (config.shouldAdvanceTime === true) { | ||
|
@@ -968,8 +984,8 @@ | |
|
||
/** | ||
* @typedef {object} Timers | ||
* @property {setTimeout} setTimeout | ||
Check warning on line 987 in src/fake-timers-src.js GitHub Actions / lint
|
||
* @property {clearTimeout} clearTimeout | ||
Check warning on line 988 in src/fake-timers-src.js GitHub Actions / lint
|
||
* @property {setInterval} setInterval | ||
* @property {clearInterval} clearInterval | ||
* @property {Date} Date | ||
|
@@ -1733,7 +1749,9 @@ | |
); | ||
} | ||
} | ||
|
||
if (_global === globalObject && timersModule) { | ||
clock.timersModuleMocks = []; | ||
} | ||
for (i = 0, l = clock.methods.length; i < l; i++) { | ||
const nameOfMethodToReplace = clock.methods[i]; | ||
if (nameOfMethodToReplace === "hrtime") { | ||
|
@@ -1753,6 +1771,15 @@ | |
} else { | ||
hijackMethod(_global, nameOfMethodToReplace, clock); | ||
} | ||
if ( | ||
clock.timersModuleMocks !== undefined && | ||
timersModule[nameOfMethodToReplace] | ||
) { | ||
const original = timersModule[nameOfMethodToReplace]; | ||
clock.timersModuleMocks.push([nameOfMethodToReplace, original]); | ||
timersModule[nameOfMethodToReplace] = | ||
_global[nameOfMethodToReplace]; | ||
} | ||
} | ||
|
||
return clock; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
"use strict"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stuff the tests inside of this into a suitably named new describe block in |
||
|
||
const { FakeTimers, assert, refute } = require("./helpers/setup-tests"); | ||
const { sinon } = require("@sinonjs/referee-sinon"); | ||
var timersModule; | ||
|
||
if (typeof require === "function" && typeof module === "object") { | ||
try { | ||
timersModule = require("timers"); | ||
} catch (e) { | ||
// ignored | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just use Mocha's built-ins to selectively run it in supported environments before(function () {
if (!timersModule) {
this.skip();
}
}); Enables a bit better reporting :) |
||
if (!timersModule) { | ||
// eslint-disable-next-line no-console | ||
console.warn("timers module is not supported in the current environment."); | ||
return; | ||
} | ||
|
||
/** | ||
* Returns elements that are present in both lists. | ||
* | ||
* @function | ||
* @template E | ||
* @param {E[]} [list1] | ||
* @param {E[]} [list2] | ||
* @return {E[]} | ||
*/ | ||
function getIntersection(list1, list2) { | ||
return list1.filter((value) => list2.indexOf(value) !== -1); | ||
} | ||
|
||
/** | ||
* Get property names and original values from timers module. | ||
* | ||
* @function | ||
* @param {string[]} [toFake] | ||
* @return {{propertyName: string, originalValue: any}[]} | ||
*/ | ||
function getOriginals(toFake) { | ||
return toFake.map((propertyName) => ({ | ||
propertyName, | ||
originalValue: timersModule[propertyName], | ||
})); | ||
} | ||
|
||
describe("issue #466", function () { | ||
afterEach(function () { | ||
if (this.clock) { | ||
this.clock.uninstall(); | ||
delete this.clock; | ||
} | ||
}); | ||
|
||
it("should install all timers on timers module", function () { | ||
const toFake = getIntersection( | ||
Object.getOwnPropertyNames(timersModule), | ||
Object.getOwnPropertyNames(FakeTimers.timers) | ||
); | ||
const originals = getOriginals(toFake); | ||
|
||
this.clock = FakeTimers.install(); | ||
|
||
for (const { propertyName, originalValue } of originals) { | ||
refute.same(timersModule[propertyName], originalValue); | ||
} | ||
}); | ||
|
||
it("should uninstall all timers on timers module", function () { | ||
const toFake = getIntersection( | ||
Object.getOwnPropertyNames(timersModule), | ||
Object.getOwnPropertyNames(FakeTimers.timers) | ||
); | ||
const originals = getOriginals(toFake); | ||
|
||
this.clock = FakeTimers.install(); | ||
this.clock.uninstall(); | ||
|
||
for (const { propertyName, originalValue } of originals) { | ||
assert.same(timersModule[propertyName], originalValue); | ||
} | ||
}); | ||
|
||
it("should have synchronized clocks on global and timers module", function () { | ||
this.clock = FakeTimers.install(); | ||
|
||
const globalStub = sinon.stub(); | ||
const timersStub = sinon.stub(); | ||
|
||
timersModule.setTimeout(timersStub, 5); | ||
setTimeout(globalStub, 5); | ||
this.clock.tick(5); | ||
assert(globalStub.calledOnce); | ||
assert(timersStub.calledOnce); | ||
}); | ||
|
||
it("fakes and resets provided methods on timers module", function () { | ||
const toFake = ["setTimeout", "Date"]; | ||
const originals = getOriginals(toFake); | ||
this.clock = FakeTimers.install({ toFake }); | ||
|
||
for (const { propertyName, originalValue } of originals) { | ||
if (originalValue === undefined) { | ||
assert.same(timersModule[propertyName], originalValue); | ||
} else { | ||
refute.same(timersModule[propertyName], originalValue); | ||
} | ||
} | ||
}); | ||
|
||
it("resets faked methods on timers module", function () { | ||
const toFake = ["setTimeout", "Date"]; | ||
const originals = getOriginals(toFake); | ||
|
||
this.clock = FakeTimers.install({ toFake }); | ||
this.clock.uninstall(); | ||
|
||
for (const { propertyName, originalValue } of originals) { | ||
assert.same(timersModule[propertyName], originalValue); | ||
} | ||
}); | ||
|
||
it("does not fake methods not provided on timers module", function () { | ||
const toFake = ["setTimeout", "Date"]; | ||
const notToFake = ["clearTimeout", "setInterval", "clearInterval"]; | ||
const originals = getOriginals(notToFake); | ||
|
||
this.clock = FakeTimers.install({ toFake }); | ||
|
||
for (const { propertyName, originalValue } of originals) { | ||
assert.same(timersModule[propertyName], originalValue); | ||
} | ||
}); | ||
|
||
it("does not fake timers module on custom global object", function () { | ||
const original = timersModule.setTimeout; | ||
this.clock = FakeTimers.withGlobal({ | ||
Date: Date, | ||
setTimeout: sinon.fake(), | ||
clearTimeout: sinon.fake(), | ||
}).install(); | ||
assert.same(timersModule.setTimeout, original); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are not actual mocks, though, so could trim that bit off.
To be consistent with the
methods
prop, just name ittimersModuleMethods