Skip to content

toHaveFocus() never match for shadow root descendants #11311

Description

@PolariTOON

Describe the bug

I want to assert a given element inside a custom element has the focus. But calling await expect.element(...).toHaveFocus() on the focused descendant fails, telling the received element with focus is the custom element, not the descendant.

Looking at the codebase, the check appears to be htmlElement.ownerDocument.activeElement === htmlElement but maybe should be something like htmlElement.getRootNode().activeElement === htmlElement instead.

Reproduction

https://github.com/PolariTOON/no-focus-in-shadow-root-in-vitest (beware there is an "install" script to install a browser first)

System Info

Binaries:
    Node: 26.9.0
    npm: 11.19.1
  Browsers:
    Chrome for Testing 153.0.8010.12 (playwright chromium v1243)
  npmPackages:
    @vitest/browser-playwright: ^5.0.1 => 5.0.1
    playwright: ^1.63.0 => 1.63.0
    vitest: ^5.0.1 => 5.0.1

Used Package Manager

npm

Validations

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions