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 constructor-based checks for fake Date no longer pass after installing #512

Merged
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
2 changes: 1 addition & 1 deletion src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +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 @@ -52,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 @@ -105,7 +105,7 @@
/* 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)
Expand All @@ -119,7 +119,7 @@

/* 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
Expand All @@ -133,7 +133,7 @@
*/

/**
* 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
Expand All @@ -145,7 +145,7 @@
/* 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 @@ -275,7 +275,7 @@
/**
* 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 @@ -311,7 +311,7 @@
}

/**
* 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 Down Expand Up @@ -450,7 +450,7 @@

// ensures identity checks using the constructor prop still works
// this should have no other functional effect
this.constructor = Date;
this.constructor = NativeDate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, this was naively working in the tests due to it being the same when not overwritten. This is what I meant to have written 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I was initially very surprised, but makes sense now — since this happens every time a fake Date is created.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patch version out shortly

}

static [Symbol.hasInstance](instance) {
Expand Down
3 changes: 3 additions & 0 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3203,6 +3203,9 @@ describe("FakeTimers", function () {
// issue #510
it("creates Date objects where the constructor prop matches the original", function () {
const realDate = new Date();
Date = NOOP; // eslint-disable-line no-global-assign
global.Date = NOOP;

const date = new this.clock.Date();

assert.equals(date.constructor.name, realDate.constructor.name);
Expand Down
Loading