testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.27k stars 467 forks source link

fix: reset regexg to prevent regexg with global flags to not match every element #1116

Closed timdeschryver closed 2 years ago

timdeschryver commented 2 years ago

What: Resolves #1115

Why:

The instance of a regex that's using the global flag hold a lastIndex state. This might give false positive results.

For example:

const g = /foo/g
g.test('foo') // |> true
g.test('foo') // |> false because lastIndex is 3
g.test('foo') // |> true because lastIndex is 0 again
g.test('foo') // |> false because lastIndex is 3

How:

Added a reproduction test case. Recreate a new regex instance from the original regex.

An alternative would be to reset the lastIndex to 0 after the check.

Checklist:

codesandbox-ci[bot] commented 2 years ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 714007e954cff8c801d9165108ed61d91f12b543:

Sandbox Source
react-testing-library-examples Configuration
react-testing-library demo Issue #1115
codecov[bot] commented 2 years ago

Codecov Report

Merging #1116 (714007e) into main (90fba31) will not change coverage. The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main     #1116   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        25           
  Lines          955       961    +6     
  Branches       311       312    +1     
=========================================
+ Hits           955       961    +6     
Flag Coverage Δ
node-12 ?
node-14 ?
node-16 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/matches.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 90fba31...714007e. Read the comment docs.

eps1lon commented 2 years ago

@timdeschryver I think you need to push an empty commit or rebase and drop the merge commit. Codecov may not like merge commits which is a result of updating the branch from the GitHub UI.

timdeschryver commented 2 years ago

@eps1lon Done. No problem, it was a team effort so we have me to blame aswell This change had a bit more catches than forseen 😅

github-actions[bot] commented 2 years ago

:tada: This PR is included in version 8.11.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket: