-
Notifications
You must be signed in to change notification settings - Fork 399
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
fix: Ignore comment nodes in toBeEmptyDOMElement #317
fix: Ignore comment nodes in toBeEmptyDOMElement #317
Conversation
Adding a new matcher to check if a element only contains comments and no other elements or text.
Codecov Report
@@ Coverage Diff @@
## master #317 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 23 23
Lines 534 537 +3
Branches 197 197
=========================================
+ Hits 534 537 +3
Continue to review full report at Codecov.
|
It sounds like a good idea to support comments, but I think it would be easier to just patch |
I am agreed with your point, but than it is not a empty element. An alternative solution will be to combine both logics to a new matcher with the name |
It may not technically be empty, but practically speaking there's nothing that would be displayed to the user, and it's better for tests to represent the user's perspective than implementation details. |
If we look from the user perspective, than I completely agree with your points. I will make the required adjustments. |
After a small discussion move logic from custom matcher to the existing toBeEmptyDOMElement.
@nickmccurdy should I also add this logic to the matcher |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had some questions about the regex being used, but then it hit me that I have objections with using regex in the first place. It will most likely be error prone to use a regex approach for this purpose.
I'd suggest we can use the DOM structure itself, instead of parsing anything ourselves. We can traverse the list of nodes inside the target element, and check if it has any child nodes that are not comments.
Here's a snippet that may serve as inspiration:
const nonCommentChildNodes = [...element.childNodes].filter(
node => node.nodeType !== Node.COMMENT_NODE
)
If this list turns out empty, the target element is empty, and vice-versa.
Would love your thoughts on this one.
About this
I would not bother adding this fix to the deprecated matcher.
Using the DOM structure instead of parsing it ourselves will solve this. |
I think this idea is the better solution to use the DOM structure with that the code will be more stable. I had some concerns about node elements which only contain whitespaces or text, but I tried it and seems to work. I will adjust my implementation. Thanks for your nice catch. |
My mistake, I forgot about the deprecation, I meant |
No problem I am thank full for all of your great input, so thanks to you all. |
Now I need your help again, because I am not an expert for const nonCommentChildNodes = [...element.childNodes].filter(node => node.nodeType !== Node.COMMENT_NODE); I get the following error:
After a research I found the following issue. I can not exactly identify what i have to do. I figured out that a workaround is to replace |
I suggest you declare a constant of our own and add a jsdoc comment to it explaining why we can't use |
Aren't we targeting the JSDOM environment of Jest which includes these values in the V8 context? They're not really globals, but any code in Jest DOM should be able to pretend they're globals as far as I understand. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. Requesting changes because of a few relatively minor observations. Let me know what you think.
@all-contributors please add @marcelbarner for code and test |
I've put up a pull request to add @marcelbarner! 🎉 |
🎉 This PR is included in version 5.11.9 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
@all-contributors please add @marcelbarner for code and test |
I've put up a pull request to add @marcelbarner! 🎉 |
What:
Adding a logic to the existing
toBeEmptyDOMElement
to ignore comments in the element.Why:
In some frameworks, like Angular, there is a logic to create elements on specific conditions. If this condition is not solved than the framework also adds some comments, this comment has the effect that the matcher
toBeEmptyDOMElement
fails, but the element is empty from the perspective of the user.How:
The matcher checks if the element contains child elements excluded comment nodes.
Checklist: