feat: Support for regular expressions in toHaveClass #563
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: