happy-dom icon indicating copy to clipboard operation
happy-dom copied to clipboard

getByRole does not work in some cases

Open roman-gula opened this issue 1 year ago • 1 comments

Describe the bug Hi! I use Vite with MUI and noticed that with happy-dom my test fails. With jsdom it works. I can't find what is the problem. Please see code below

To Reproduce

it('with Accordion', () => {
  render(
    <Accordion>
      <AccordionSummary>aaa</AccordionSummary>
    </Accordion>
  );

  expect(screen.getByRole('button')).toBeInTheDocument();
});

Example on Stackblitz

roman-gula avatar Oct 06 '23 14:10 roman-gula

I also have this issue when using different component libraries. The issue seems to be that HappyDOM may not be looking at the role attribute, but rather its only looking at the actual element type? The component library is also using a div with role="button" and HappyDOM does not pick it up properly.

DavidRNogueira avatar Oct 23 '23 15:10 DavidRNogueira

Thank you for reporting @roman-gula and @DavidRNogueira! :slightly_smiling_face:

A fix has been released: https://github.com/capricorn86/happy-dom/releases/tag/v13.8.5

The reason for why it failed to match the element is because Testing Library checks if the element is visible by using Window.getComputedStyle(), which returned display: none for the button element. The button element had display: none as a CSS rule with the pseudo selector ":before" matched the actual element (and not the pseudo element), but shouldn't have.

capricorn86 avatar Mar 14 '24 18:03 capricorn86