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

fix #504: make instances of original Date pass as instances of the fake Date #505

Merged
merged 3 commits into from
Sep 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
*/

/**
* Queues a function to be called during a browser's idle periods

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

View workflow job for this annotation

GitHub Actions / lint

Expected only 0 line after block description
*
* @callback RequestIdleCallback
* @param {function(IdleDeadline)} callback

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

View workflow job for this annotation

GitHub Actions / lint

Syntax error in type: function(IdleDeadline)
* @param {{timeout: number}} options - an options object
* @returns {number} the id
*/
Expand All @@ -51,13 +52,13 @@

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

Check warning on line 55 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 61 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 @@ -104,7 +105,8 @@
/* eslint-enable jsdoc/require-property-description */

/**
* Configuration object for the `install` method.

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

View workflow job for this annotation

GitHub Actions / lint

Expected only 0 line after block description
*
* @typedef {object} Config
* @property {number|Date} [now] a number (in milliseconds) or a Date object (default epoch)
* @property {string[]} [toFake] names of the methods that should be faked.
Expand All @@ -117,7 +119,8 @@

/* eslint-disable jsdoc/require-property-description */
/**
* The internal structure to describe a scheduled fake timer

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

View workflow job for this annotation

GitHub Actions / lint

Expected only 0 line after block description
*
* @typedef {object} Timer
* @property {Function} func
* @property {*[]} args
Expand All @@ -130,7 +133,8 @@
*/

/**
* A Node timer

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

View workflow job for this annotation

GitHub Actions / lint

Expected only 0 line after block description
*
* @typedef {object} NodeImmediate
* @property {function(): boolean} hasRef
* @property {function(): NodeImmediate} ref
Expand All @@ -141,7 +145,8 @@
/* eslint-disable complexity */

/**
* Mocks available features in the specified global namespace.

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

View workflow job for this annotation

GitHub Actions / lint

Expected only 0 line after block description
*
* @param {*} _global Namespace to mock (e.g. `window`)
* @returns {FakeTimers}
*/
Expand Down Expand Up @@ -270,7 +275,8 @@
/**
* Parse strings like "01:10:00" (meaning 1 hour, 10 minutes, 0 seconds) into
* number of milliseconds. This is used to support human-readable strings passed
* to clock.tick()

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

View workflow job for this annotation

GitHub Actions / lint

Expected only 0 line after block description
*
* @param {string} str
* @returns {number}
*/
Expand Down Expand Up @@ -305,7 +311,8 @@
}

/**
* Get the decimal part of the millisecond value as nanoseconds

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

View workflow job for this annotation

GitHub Actions / lint

Expected only 0 line after block description
*
* @param {number} msFloat the number of milliseconds
* @returns {number} an integer number of nanoseconds in the range [0,1e6)
*
Expand All @@ -322,6 +329,7 @@

/**
* Used to grok the `now` parameter to createClock.
*
* @param {Date|number} epoch the system time
* @returns {number}
*/
Expand Down Expand Up @@ -440,6 +448,10 @@
super(...arguments);
}
}

static [Symbol.hasInstance](instance) {
return instance instanceof NativeDate;
}
}

ClockDate.isFake = true;
Expand All @@ -464,6 +476,7 @@
/**
* A normal Class constructor cannot be called without `new`, but Date can, so we need
* to wrap it in a Proxy in order to ensure this functionality of Date is kept intact
*
* @type {ClockDate}
*/
const ClockDateProxy = new Proxy(ClockDate, {
Expand All @@ -490,6 +503,7 @@
* Most of the properties are the original native ones,
* but we need to take control of those that have a
* dependency on the current clock.
*
* @returns {object} the partly fake Intl implementation
*/
function createIntl() {
Expand Down Expand Up @@ -662,6 +676,7 @@
/* eslint consistent-return: "off" */
/**
* Timer comparitor
*
* @param {Timer} a
* @param {Timer} b
* @returns {number}
Expand Down Expand Up @@ -793,6 +808,7 @@

/**
* Gets clear handler name for a given timer type
*
* @param {string} ttype
*/
function getClearHandler(ttype) {
Expand All @@ -804,6 +820,7 @@

/**
* Gets schedule handler name for a given timer type
*
* @param {string} ttype
*/
function getScheduleHandler(ttype) {
Expand Down Expand Up @@ -1159,11 +1176,13 @@

/**
* A high resolution timestamp in milliseconds.
*
* @typedef {number} DOMHighResTimeStamp
*/

/**
* performance.now()
*
* @returns {DOMHighResTimeStamp}
*/
function fakePerformanceNow() {
Expand Down
8 changes: 8 additions & 0 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3369,6 +3369,11 @@ describe("FakeTimers", function () {
it("mirrors toString output", function () {
assert.same(this.clock.Date.toString(), Date.toString());
});

it("recognises instances of the original Date as instances of itself", function () {
var originalDateInstance = new Date();
assert(originalDateInstance instanceof this.clock.Date);
});
});

describe("stubTimers", function () {
Expand Down Expand Up @@ -4765,6 +4770,7 @@ describe("FakeTimers", function () {

/**
* Returns elements that are present in both lists.
*
* @function
* @template E
* @param {E[]} [list1]
Expand All @@ -4777,6 +4783,7 @@ describe("FakeTimers", function () {

/**
* Get property names and original values from timers module.
*
* @function
* @param {string[]} [toFake]
* @returns {{propertyName: string, originalValue: any}[]}
Expand Down Expand Up @@ -5923,6 +5930,7 @@ describe("Node Timer: ref(), unref(),hasRef()", function () {
describe("Intl API", function () {
/**
* Tester function to check if the globally hijacked Intl object is plugging into the faked Clock
*
* @param {string} ianaTimeZone - IANA time zone name
* @param {number} timestamp - UNIX timestamp
* @returns {boolean}
Expand Down
Loading