Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
- `[jest-worker]` Handle `ERR_IPC_CHANNEL_CLOSED` errors properly ([#11143](https://github.com/facebook/jest/pull/11143))
- `[pretty-format]` [**BREAKING**] Convert to ES Modules ([#10515](https://github.com/facebook/jest/pull/10515))
- `[pretty-format]` Only call `hasAttribute` if it's a function ([#11000](https://github.com/facebook/jest/pull/11000))
- `[pretty-format]` Handle jsdom attributes properly ([#11189](https://github.com/facebook/jest/pull/11189))

### Chore & Maintenance

Expand Down
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