Skip to content

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

Open
HarshitMadhav wants to merge 1 commit intofacebook:mainfrom
HarshitMadhav:main
Open

fix(jest): mock getBoundingClientRect on host components in tests#56477
HarshitMadhav wants to merge 1 commit intofacebook:mainfrom
HarshitMadhav:main

Conversation

@HarshitMadhav
Copy link
Copy Markdown

@HarshitMadhav HarshitMadhav commented Apr 16, 2026

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
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

1 participant