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

feat: Support for regular expressions in toHaveClass #563

Merged
merged 4 commits into from Jan 24, 2024

Conversation

gnapse
Copy link
Member

@gnapse gnapse commented Dec 26, 2023

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:

  • Documentation
  • Tests
  • Updated Type Definitions
  • Ready to be merged

@gnapse gnapse self-assigned this Dec 26, 2023
Copy link

codecov bot commented Dec 26, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

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

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@gnapse gnapse merged commit 9787ed5 into main Jan 24, 2024
5 checks passed
@gnapse gnapse deleted the to-have-class/regexp-support branch January 24, 2024 11:27
Copy link

🎉 This PR is included in version 6.3.0 🎉

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regex support for .toHaveClass()
2 participants