This repository has been archived by the owner on Nov 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Analysis of weird test failure #2
Comments
Here is a hypothesis for y’all to evaluate:
const {JSDOM} = require('jsdom');
test('classes', () => {
const span1 = document.createElement('span');
span1.innerHTML = 'hello';
const frag = JSDOM.fragment('<span>hello</span>');
const span2 = frag.children[0];
expect(span1.constructor.name).toBe('HTMLSpanElement');
expect(span1 instanceof Node).toBe(true);
expect(span2.constructor.name).toBe('HTMLSpanElement');
expect(span2.constructor === span1.constructor).toBe(false);
expect(span2 instanceof Node).toBe(false);
}); |
What do you think of a solution that we discussed in jestjs/jest#7791 (comment) function isDomNode(obj) {
return (
obj !== null &&
typeof obj === 'object' &&
typeof obj.nodeType === 'number' &&
typeof obj.nodeName === 'string' &&
typeof obj.isEqualNode === 'function'
);
} |
Here is /* @jest-environment jsdom*/
/* eslint-env browser*/
const {JSDOM} = require('jsdom');
describe('JSDOM fragment', () => {
describe('without side effect', () => { /* pass */ });
describe('with side effect', () => {
test('isNot false', () => {
const markup = '<strong>deep</strong><span>equal</span>';
const a = JSDOM.fragment(markup);
const b = JSDOM.fragment(markup);
expect(a.children.length).toBe(2); // cause side effect
expect(a).toEqual(b);
expect(b).toEqual(a);
});
test('isNot true', () => {
const a = JSDOM.fragment('<strong>not</strong><span>deep equal</span>');
const b = JSDOM.fragment('<span>not</span><span>deep equal</span>');
expect(a).not.toEqual(b);
expect(b).not.toEqual(a);
});
});
}); |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
@SimenB /cc @thymikee The regression is caused by 2 bugs in
jsdom
package:In Check for existence of
Element
when comparing DOM-nody-things (#7786) (clean cherry pick version) jestjs/jest#7995 we changedisDomNode
function injasmineUtils.ts
file:obj !== null && typeof obj === 'object' && typeof obj.nodeType === 'number' && typeof obj.nodeName === 'string'
instanceof
test:typeof Node !== 'undefined' && obj instanceof Node
Serializing a fragment causes side effect enumerable symbol
Symbol(SameObject caches)
When you commented out
toMatchDiffSnapshot
then both fragment instances have no enumerable properties, so test passes as false negative, even if I comment outspan2.innerHTML = 'hello'
to compare<span>hello</span>
to<span>huh</span>
With
toMatchDiffSnapshot
thenfirst
has been serialized, so has bogus implementation symbol key, but because of reassignment tosecond
the new instance has no keysTo confirm, if I call
pretty-format
withDOMElement
plugin to serialize thesecond
instance, thentoEqual
passes as false negative, because both have value ofSameObject caches
The text was updated successfully, but these errors were encountered: