testing-library / jest-dom

:owl: Custom jest matchers to test the state of the DOM
https://testing-library.com/docs/ecosystem-jest-dom
MIT License
4.45k stars 401 forks source link

feat: Support for regular expressions in toHaveClass #563

Closed gnapse closed 10 months ago

gnapse commented 11 months ago

What:

Enhances .toHaveClass to also support receiving regular expressions in its params, to be matched against the target element's class names.

Since this is a newly supported argument type, this is not a breaking change. Any code calling .toHaveClass with strings only (the only supported use so far) that code will continue to work as before.

Why:

Closes #556

It enables more freedom when checking an element's class names. For instance, in some preprocessed CSS scenarios, class names are generated programmatically, and the writer of the test cannot know in advance the exact class name, though they may know a substring or pattern in it.

How:

By checking, in a handful of places of the relevant code, whether a value is a string or a regular expression. If it is a regular expression, then we do a different check.

Also, in the particular case of passing {exact:true} options to .toHaveClass, we disable regular expressions support, and throw an error if regular expressions are found in the arguments.

Checklist:

codecov[bot] commented 11 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (1fb156c) 100.00% compared to head (a2cdb76) 100.00%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #563 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 27 27 Lines 668 676 +8 Branches 254 261 +7 ========================================= + Hits 668 676 +8 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

github-actions[bot] commented 10 months ago

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

The release is available on:

Your semantic-release bot :package::rocket: