Skip to content

Commit 33c3b07

Browse files
authored
fix(pretty-format): handles jsdom attributes properly (#11189)
1 parent 3caef7d commit 33c3b07

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
- `[jest-worker]` Handle `ERR_IPC_CHANNEL_CLOSED` errors properly ([#11143](https://github.com/facebook/jest/pull/11143))
6969
- `[pretty-format]` [**BREAKING**] Convert to ES Modules ([#10515](https://github.com/facebook/jest/pull/10515))
7070
- `[pretty-format]` Only call `hasAttribute` if it's a function ([#11000](https://github.com/facebook/jest/pull/11000))
71+
- `[pretty-format]` Handle jsdom attributes properly ([#11189](https://github.com/facebook/jest/pull/11189))
7172

7273
### Chore & Maintenance
7374

packages/pretty-format/src/__tests__/DOMElement.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,9 @@ Testing.`;
582582
].join('\n'),
583583
);
584584
});
585+
586+
it('handles jsdom attributes properly', () => {
587+
const attributes = require('jsdom/lib/jsdom/living/attributes');
588+
expect(DOMElement.test(attributes)).toBe(false);
589+
});
585590
});

packages/pretty-format/src/plugins/DOMElement.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ const FRAGMENT_NODE = 11;
2222

2323
const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/;
2424

25+
const testHasAttribute = (val: any) => {
26+
try {
27+
return typeof val.hasAttribute === 'function' && val.hasAttribute('is');
28+
} catch {
29+
return false;
30+
}
31+
};
32+
2533
const testNode = (val: any) => {
2634
const constructorName = val.constructor.name;
2735
const {nodeType, tagName} = val;
2836
const isCustomElement =
2937
(typeof tagName === 'string' && tagName.includes('-')) ||
30-
(typeof val.hasAttribute === 'function' && val.hasAttribute('is'));
38+
testHasAttribute(val);
3139

3240
return (
3341
(nodeType === ELEMENT_NODE &&

0 commit comments

Comments
 (0)