Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getAllByRole witha regex matcher is returning the wrong count #1115

Closed
themagickoala opened this issue Mar 22, 2022 · 3 comments · Fixed by #1116
Closed

getAllByRole witha regex matcher is returning the wrong count #1115

themagickoala opened this issue Mar 22, 2022 · 3 comments · Fixed by #1116
Labels
bug Something isn't working released

Comments

@themagickoala
Copy link

  • @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.

@themagickoala
Copy link
Author

We've managed to resolve this locally by removing the global flag from the Regular Expression, but I don't understand why this would cause the test to fail?

@timdeschryver
Copy link
Member

timdeschryver commented Mar 22, 2022

It seems like it somehow holds some state on a global RegExp, I don't know why this happens but here's what I found:

const g = /foo/g
g.test('foo') // |> true
g.test('foo') // |> false

const n = /foo/
n.test('foo') // |> true
n.test('foo') // |> true

g.test('bar') // |> false
g.test('bar') // |> false

TIL: A RegExp object with the g flag keeps track of the lastIndex where a match occurred, so on subsequent matches it will start from the last used index

Source: https://stackoverflow.com/a/1520853/10112124

@github-actions
Copy link

🎉 This issue has been resolved in version 8.11.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
3 participants