-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Check for existence of Element
when comparing DOM-nody-things (#7786)
#7791
Conversation
Thank you for first contribution to Jest! In plain node testing environment where Can you add a test to the positive [
{
nodeName: 'div',
nodeType: 1,
},
{
nodeName: 'p',
nodeType: 1,
},
] It looks like that test might not succeed (by failing :) because fall through to: return a.innerText == b.innerText && a.textContent == b.textContent; Let’s make sure if any code change is needed, it doesn’t break original test because fall through to: if (aIsDomNode || bIsDomNode) {
return false;
} |
Oops, didn't catch that :-). If you take a look at the current jasmine Judging from the comments the code path only seems to be important for IE<9 inside a browser which is probably not a supported target of Jest anyway? Just skipping the whole DOM node detection section if The latest commit leaves the duck typing in place and lets me (or other fiddlers) reimplement |
We should support ie11 (for now), so feel free to remove compat code for older browsers |
So we assume that I will make sure whether or not @mfeineis in your node testing environment without jsdom, is generic deep equality comparison of properties the desired criterion for objects which match the What do you think about cutting the special case to the minimum: if (isDomNode(a) && isDomNode(b) && typeof a.isEqualNode === 'function') {
return a.isEqualNode(b);
} |
Yes for my case these false positives should just be handled as plain objects because they are just that. I did something very similar what you suggested in my latest commit, it might be OK to leave out the |
Yes, it gave me a smile when your commit and my comment crossed paths. I think it is clearer without the second Just verified that element and text node have Oh, what do you think about adding function isDomNode(obj) {
return (
obj !== null &&
typeof obj === 'object' &&
typeof obj.nodeType === 'number' &&
typeof obj.nodeName === 'string' &&
typeof obj.isEqualNode === 'function'
);
} if (isDomNode(a) && isDomNode(b)) {
return a.isEqualNode(b);
} |
Merging the |
Yes, good point that shortcut return reduces cost of comparing. It is super for this pull request to fix the problem, delete obsolete code, and keep same semantics. |
I thought about it some more. I think |
Thank you for thinking about it some more. Two heads are better than one. Here is my short summary of the solution: for two instances of
function isDomNode(obj) {
return typeof Node !== 'undefined' && obj instanceof Node;
} because if
It doesn’t seem likely if
Before we merge (but not today) I’ll write local tests comparing |
I've added the |
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.
Looks good to me!
While we wait for #7919 conversion of I thought because if (aIsDomNode || bIsDomNode) {
return false;
} That the following tests would incorrectly pass: expect(document.createElement('div')).toEqual({});
expect({}).toEqual(document.createElement('div')); The reason that they correctly fail is an upstream short-circuit: var className = Object.prototype.toString.call(a);
if (className != Object.prototype.toString.call(b)) {
return false;
} For example:
Also because Except beware that in After this PR has been merged, you or I might add a small * @jest-environment jsdom
*/
/* eslint-env browser*/ as in for example, @SimenB @thymikee can you see any problem with the minimal code we kept for DOM elements? |
@mfeineis The After so many changes in recent days: |
## Summary Trivial. Replace "form" with "forum" in issue template. ## Test plan Skipped tests, as this is a minor typo fix in a GitHub issue template.
* Fix asymmetric equal for Number * Update CHANGELOG * Handle the case of a being a new Number * Use Object.is for Number equality * Add unit tests related to issues/7941 * Add extra units for equality * Remove unnecessary comment in eq
ahh, shoot something went wrong... |
I have a feeling I put my commits on the wrong end of the rebase-train but github shows the changes I'm expecting so hopefully I didn't break anything 😇 |
I could also spin up a new branch on top of master and cherry pick my changes on top if that is easier to manage. |
At my level of git knowledge, all’s well that ends well. Other collaborators can tell us if they see a problem. Ever since GitHub got git checkout master
git fetch upstream
git merge upstream/master
git checkout feature-branch
git merge master
# fix merge conflicts and commit
# continue with feature |
Sure, that sounds like a plan. That is the way that I have felt, when I have been you :) |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
Comparing things that look like DOM nodes now checks if
Element
is in scope before usage which would lead toReferenceError: Element is not defined
errors with'testEnvironment': 'node'
.Test plan
First contribution and signed the CLA.