Skip to content

fix: traverse shadow roots in toHaveFocus#723

Open
SAY-5 wants to merge 1 commit into
testing-library:mainfrom
SAY-5:pr/to-have-focus-shadow-root
Open

fix: traverse shadow roots in toHaveFocus#723
SAY-5 wants to merge 1 commit into
testing-library:mainfrom
SAY-5:pr/to-have-focus-shadow-root

Conversation

@SAY-5

@SAY-5 SAY-5 commented May 13, 2026

Copy link
Copy Markdown

What:

Traverse shadow roots when resolving the active element so toHaveFocus matches elements focused inside open shadow DOMs.

Why:

document.activeElement returns the shadow host rather than the actually focused descendant, so toHaveFocus could never pass for elements inside a shadow root. Closes #722.

How:

Added a getDeepActiveElement helper that walks shadowRoot.activeElement chains, and use it in both the pass check and the received-element message. Tests cover a single shadow root and a nested shadow root.

Checklist:

  • Documentation N/A
  • Tests
  • Updated Type Definitions N/A
  • Ready to be merged

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

toHaveFocus doesn't work with shadow roots

1 participant