Skip to content

Commit

Permalink
fix(pretty-format): handles jsdom attributes properly (#11189)
Browse files Browse the repository at this point in the history
  • Loading branch information
pikou1995 authored Mar 14, 2021
1 parent 3caef7d commit 33c3b07
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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

0 comments on commit 33c3b07

Please sign in to comment.