Skip to content

Configure global fallbackFn for timezone-mock to handle Date subclasses#78056

Merged
ciampo merged 3 commits into
WordPress:trunkfrom
vishnupprajapat:fix/timezone-mock-compatibility-v4
May 7, 2026
Merged

Configure global fallbackFn for timezone-mock to handle Date subclasses#78056
ciampo merged 3 commits into
WordPress:trunkfrom
vishnupprajapat:fix/timezone-mock-compatibility-v4

Conversation

@vishnupprajapat
Copy link
Copy Markdown
Contributor

@vishnupprajapat vishnupprajapat commented May 7, 2026

What?

Part of #78005

This PR configures a global fallbackFn for timezone-mock in the Jest unit test setup. It allows the MockDate constructor to correctly handle objects that implement valueOf(), specifically addressing compatibility issues with Date subclasses like UTCDateMini.

Why?

The upgrade to date-fns v4 introduces logic that reconstructs dates using new date.constructor(date). When date-fns v4 is used alongside timezone-mock, this triggers a "Unhandled type passed to MockDate constructor: object" error because the mock does not natively support object arguments.

By setting a fallbackFn globally, we ensure that these objects are safely converted back to timestamps before being passed to the mock constructor, unblocking the monorepo-wide upgrade to date-fns v4.

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:

timezoneMock.options( {
	fallbackFn: ( p ) =>
		new timezoneMock._Date(
			typeof p?.valueOf === 'function' ? p.valueOf() : p
		),
} );

This intercepts any "unhandled" types in the MockDate constructor and uses the underlying _Date (native Date) to handle the value.

Testing Instructions

  1. Run the existing unit tests for date-time components:
    npm run test:unit -- packages/components/src/date-time
  2. Verify that all 118 tests pass successfully.
  3. This change is intended to be transparent to existing date-fns v3 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-mock constructor errors, propose the fallbackFn solution based on maintainer suggestions, and verify the implementation by running the affected test suites.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: vishnupprajapat <vishnuprajapat@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ciampo ciampo requested review from Copilot and manzoorwanijk May 7, 2026 13:34
@ciampo ciampo added [Type] Enhancement A suggestion for improvement. [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. and removed [Type] Enhancement A suggestion for improvement. labels May 7, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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-mock fallbackFn in the Jest unit test setup (global-mocks.js) to handle object inputs (notably Date subclasses) by converting them to timestamps.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

Comment thread test/unit/config/global-mocks.js Outdated
@vishnupprajapat vishnupprajapat force-pushed the fix/timezone-mock-compatibility-v4 branch from a321d34 to 7443e12 Compare May 7, 2026 14:12
@ciampo ciampo enabled auto-merge (squash) May 7, 2026 14:53
@ciampo ciampo merged commit c0d4da8 into WordPress:trunk May 7, 2026
48 of 52 checks passed
@github-actions github-actions Bot added this to the Gutenberg 23.2 milestone May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants