Skip to content

fix(jest): mock getBoundingClientRect on host components in tests - #56477

Open
HarshitMadhav wants to merge 1 commit into
react:mainfrom
HarshitMadhav:main
Open

HarshitMadhav wants to merge 1 commit into
react:mainfrom
HarshitMadhav:main

Conversation

@HarshitMadhav

@HarshitMadhav HarshitMadhav commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Jest mocks for host components (MockNativeMethods, mockNativeComponent) exposed measure, measureInWindow, measureLayout, setNativeProps, focus, and blur, but not getBoundingClientRect. In production, ReadOnlyElement / ReactNativeElement implements getBoundingClientRect(); refs from react-test-renderer in unit tests therefore threw TypeError: getBoundingClientRect is not a function when user code called it (see #54916).

This change adds a getBoundingClientRect mock that returns a zero-valued DOMRect-like object (x, y, width, height, top, left, right, bottom), matching the fallback behavior when there is no native node. Tests were added for the mock helpers and for calling getBoundingClientRect on a mocked View ref via react-test-renderer.

Fixes #54916.

Changelog:

[INTERNAL] [FIXED] - Mock getBoundingClientRect on Jest host component refs so unit tests match ReadOnlyElement and no longer throw when calling ref.getBoundingClientRect().

Test Plan:

Ran:

yarn test packages/jest-preset/jest/__tests__/

All tests passed, including mockNativeMethods-test.js, getBoundingClientRect-test.js, and setup-test.js.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 16, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Apr 16, 2026
@react-native-bot

Copy link
Copy Markdown
Collaborator

This PR is stale because it has been open for 180 days with no activity. It will be closed in 7 days unless you comment on it or remove the "Stale" label.

@react-native-bot react-native-bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 14, 2026
@HarshitMadhav

Copy link
Copy Markdown
Contributor Author

@react-native-bot review?

@react-native-bot react-native-bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: _ref2.getBoundingClientRect is not a function when unit testing code

2 participants