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): handle tagName not being a string #10397

Merged
merged 2 commits into from
Aug 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[pretty-format]` Handle `tagName` not being a string ([#10397](https://github.com/facebook/jest/pull/10397))

### Chore & Maintenance

### Performance
Expand Down
18 changes: 18 additions & 0 deletions packages/pretty-format/src/__tests__/DOMElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,22 @@ Testing.`;
{maxDepth: 2},
);
});

it('handles `tagName` not being a string', () => {
expect({
name: 'value',
tagName: {text: 'param'},
type: 'string',
}).toPrettyPrintTo(
[
'Object {',
' "name": "value",',
' "tagName": Object {',
' "text": "param",',
' },',
' "type": "string",',
'}',
].join('\n'),
);
});
});
6 changes: 4 additions & 2 deletions packages/pretty-format/src/plugins/DOMElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/;

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

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