Skip to content

Latest commit

 

History

History
31 lines (20 loc) · 809 Bytes

no-global-regexp-flag-in-query.md

File metadata and controls

31 lines (20 loc) · 809 Bytes

Disallow the use of the global RegExp flag (/g) in queries (testing-library/no-global-regexp-flag-in-query)

Ensure that there are no global RegExp flags used when using queries.

Rule Details

A RegExp instance that's using the global flag /g holds state and this might cause false-positives while querying for elements.

Examples of incorrect code for this rule:

screen.getByText(/hello/gi);
await screen.findByRole('button', { otherProp: true, name: /hello/g });

Examples of correct code for this rule:

screen.getByText(/hello/i);
await screen.findByRole('button', { otherProp: true, name: /hello/ });

Further Reading