Skip to content
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

fix(pretty-format): handles jsdom attributes properly #11189

Merged
merged 3 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 (_) {
pikou1995 marked this conversation as resolved.
Show resolved Hide resolved
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