Configure global fallbackFn for timezone-mock to handle Date subclasses#78056
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Pull request overview
Configures timezone-mock globally in the Jest unit-test setup to support Date subclasses (e.g. UTCDateMini) by providing a fallbackFn that converts object inputs via valueOf() before delegating to the underlying native Date constructor. This is intended to prevent date-fns v4’s new date.constructor(date) reconstruction path from failing under the mocked Date.
Changes:
- Add a global
timezone-mockfallbackFnin the Jest unit test setup (global-mocks.js) to handle object inputs (notablyDatesubclasses) by converting them to timestamps.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
a321d34 to
7443e12
Compare
What?
Part of #78005
This PR configures a global
fallbackFnfortimezone-mockin the Jest unit test setup. It allows theMockDateconstructor to correctly handle objects that implementvalueOf(), specifically addressing compatibility issues withDatesubclasses likeUTCDateMini.Why?
The upgrade to
date-fnsv4 introduces logic that reconstructs dates usingnew date.constructor(date). Whendate-fnsv4 is used alongsidetimezone-mock, this triggers a "Unhandled type passed to MockDate constructor: object" error because the mock does not natively support object arguments.By setting a
fallbackFnglobally, we ensure that these objects are safely converted back to timestamps before being passed to the mock constructor, unblocking the monorepo-wide upgrade todate-fnsv4.How?
A global configuration has been added to
test/unit/config/global-mocks.js. This file is loaded by Jest before every test suite.Implementation:
This intercepts any "unhandled" types in the
MockDateconstructor and uses the underlying_Date(native Date) to handle the value.Testing Instructions
npm run test:unit -- packages/components/src/date-timedate-fnsv3 tests while providing the necessary infrastructure for v4 compatibility.Testing Instructions for Keyboard
N/A - This is a change to the test infrastructure and does not affect the user interface.
Screenshots or screencast
N/A
Use of AI Tools
This pull request was authored with the assistance of Claude Opus 4.7. The AI was used to investigate the root cause of the
timezone-mockconstructor errors, propose thefallbackFnsolution based on maintainer suggestions, and verify the implementation by running the affected test suites.