Skip to content

getAllByRole witha regex matcher is returning the wrong count #1115

@themagickoala

Description

@themagickoala
  • @testing-library/dom version: 8.11.3
  • Testing Framework and version: Jest 26.6.3
  • DOM Environment: jsdom 16.6.0

Relevant code or config:

import { getQueriesForElement } from '@testing-library/dom';

test('should use get all by role matcher', () => {
  const container = document.createElement('div');
  container.innerHTML = `<button
    type="button"
    title="Low">Low</button>
  <button
    type="button"
    title="Medium">Medium</button>
  <button
    type="button"
    title="High">High</button>`;
  const { getAllByRole } = getQueriesForElement(container);
  expect(getAllByRole('button', { name: /(low|medium|high)/gi }).length).toBe(3); // error - only found 2
});

What you did:

I was trying to match a set of elements using the getAllByRole matcher within React Testing Library, and the length of the matched results was coming back wrong.

What happened:

Instead of finding all 3 buttons, it only found 2 - the Low and High buttons

Reproduction:

The code above is a self-contained minimal reproduction of this issue within DOM Testing Library (we found this with RTL).

Problem description:

All 3 buttons should be found, rather than only finding 2.

Suggested solution:

I wasn't able to dig too far into the code for this. I imagine something is happening either in the filters for the queryAllByRole method, or within the matches method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreleased

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions