Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(pretty-format): handles jsdom attributes properly
  • Loading branch information
pikou1995 committed Mar 13, 2021
commit f8365c63cf023f41e0a7fe70e5305e219648cd5f
5 changes: 5 additions & 0 deletions packages/pretty-format/src/__tests__/DOMElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,9 @@ Testing.`;
].join('\n'),
);
});

it('handles jsdom attributes properly', () => {
const attributes = require('jsdom/lib/jsdom/living/attributes');
expect(DOMElement.test(attributes)).toBe(false);
});
});
10 changes: 9 additions & 1 deletion packages/pretty-format/src/plugins/DOMElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ const FRAGMENT_NODE = 11;

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

const testHasAttribute = (val: any) => {
try {
return typeof val.hasAttribute === 'function' && val.hasAttribute('is');
} catch (_) {
return false;
}
};

const testNode = (val: any) => {
const constructorName = val.constructor.name;
const {nodeType, tagName} = val;
const isCustomElement =
(typeof tagName === 'string' && tagName.includes('-')) ||
(typeof val.hasAttribute === 'function' && val.hasAttribute('is'));
testHasAttribute(val);

return (
(nodeType === ELEMENT_NODE &&
Expand Down