Skip to content
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

Merged
merged 6 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ clock instance, not the browser's internals.

Calling `install` with no arguments achieves this. You can call `uninstall`
later to restore things as they were again.
Note that in NodeJS also the [timers](https://nodejs.org/api/timers.html) module will receive fake timers when using global scope.

```js
// In the browser distribution, a global `FakeTimers` is already available
Expand Down Expand Up @@ -146,7 +147,9 @@ The `loopLimit` argument sets the maximum number of timers that will be run when

### `var clock = FakeTimers.install([config])`

Installs FakeTimers using the specified config (otherwise with epoch `0` on the global scope). The following configuration options are available
Installs FakeTimers using the specified config (otherwise with epoch `0` on the global scope).
Note that in NodeJS also the [timers](https://nodejs.org/api/timers.html) module will receive fake timers when using global scope.
The following configuration options are available

| Parameter | Type | Default | Description |
| -------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand Down
31 changes: 30 additions & 1 deletion src/fake-timers-src.js
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
Expand Down Expand Up @@ -39,13 +47,13 @@

/**
* @typedef RequestAnimationFrame
* @property {function(number):void} requestAnimationFrame

Check warning on line 50 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @Property "requestAnimationFrame" description
* @returns {number} - the id
*/

/**
* @typedef Performance
* @property {function(): number} now

Check warning on line 56 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @Property "now" description
*/

/* eslint-disable jsdoc/require-property-description */
Expand Down Expand Up @@ -85,6 +93,7 @@
* @property {function(): void} uninstall Uninstall the clock.
* @property {Function[]} methods - the methods that are faked
* @property {boolean} [shouldClearNativeTimers] inherited from config
* @property {{methodName:string, original:any}[] | undefined} timersModuleMethods
*/
/* eslint-enable jsdoc/require-property-description */

Expand Down Expand Up @@ -317,7 +326,7 @@
return timer && timer.callAt >= from && timer.callAt <= to;
}

/**

Check warning on line 329 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns declaration
* @param {Clock} clock
* @param {Timer} job
*/
Expand Down Expand Up @@ -619,7 +628,7 @@
}

/* eslint consistent-return: "off" */
/**

Check warning on line 631 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

JSDoc @returns declaration present but return expression not available in function
* Timer comparitor
*
* @param {Timer} a
Expand Down Expand Up @@ -751,7 +760,7 @@
}
}

/**

Check warning on line 763 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns declaration
* Gets clear handler name for a given timer type
*
* @param {string} ttype
Expand All @@ -763,7 +772,7 @@
return `clear${ttype}`;
}

/**

Check warning on line 775 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns declaration
* Gets schedule handler name for a given timer type
*
* @param {string} ttype
Expand All @@ -775,7 +784,7 @@
return `set${ttype}`;
}

/**

Check warning on line 787 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns declaration
* Creates an anonymous function to warn only once
*/
function createWarnOnce() {
Expand All @@ -787,7 +796,7 @@
}
const warnOnce = createWarnOnce();

/**

Check warning on line 799 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns declaration
* @param {Clock} clock
* @param {number} timerId
* @param {string} ttype
Expand Down Expand Up @@ -886,6 +895,12 @@
}
}
}
if (clock.timersModuleMethods !== undefined) {
for (let j = 0; j < clock.timersModuleMethods.length; j++) {
const entry = clock.timersModuleMethods[j];
timersModule[entry.methodName] = entry.original;
}
}
}

if (config.shouldAdvanceTime === true) {
Expand Down Expand Up @@ -968,8 +983,8 @@

/**
* @typedef {object} Timers
* @property {setTimeout} setTimeout

Check warning on line 986 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @Property "setTimeout" description
* @property {clearTimeout} clearTimeout

Check warning on line 987 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @Property "clearTimeout" description
* @property {setInterval} setInterval
* @property {clearInterval} clearInterval
* @property {Date} Date
Expand Down Expand Up @@ -1733,7 +1748,9 @@
);
}
}

if (_global === globalObject && timersModule) {
clock.timersModuleMethods = [];
}
for (i = 0, l = clock.methods.length; i < l; i++) {
const nameOfMethodToReplace = clock.methods[i];
if (nameOfMethodToReplace === "hrtime") {
Expand All @@ -1753,6 +1770,18 @@
} else {
hijackMethod(_global, nameOfMethodToReplace, clock);
}
if (
clock.timersModuleMethods !== undefined &&
timersModule[nameOfMethodToReplace]
) {
const original = timersModule[nameOfMethodToReplace];
clock.timersModuleMethods.push({
methodName: nameOfMethodToReplace,
original: original,
});
timersModule[nameOfMethodToReplace] =
_global[nameOfMethodToReplace];
}
}

return clock;
Expand Down
141 changes: 141 additions & 0 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ const {
utilPromisifyAvailable,
} = require("./helpers/setup-tests");

var timersModule;

if (typeof require === "function" && typeof module === "object") {
try {
timersModule = require("timers");
} catch (e) {
// ignored
}
}

describe("FakeTimers", function () {
describe("setTimeout", function () {
beforeEach(function () {
Expand Down Expand Up @@ -4664,6 +4674,137 @@ describe("FakeTimers", function () {
assert.isFalse(stub.called);
});
});
describe("Node timers module", function () {
before(function () {
if (!timersModule) {
this.skip();
}
});

/**
* 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],
}));
}

afterEach(function () {
if (this.clock) {
this.clock.uninstall();
delete this.clock;
}
});

it("should install all timers", 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", 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 clock with globalObject", 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", 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", 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", 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 when installing 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);
});
});
});

describe("loop limit stack trace", function () {
Expand Down