From d134a05f89c199321fe3166a175a5b8f13179dff Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 12 Aug 2020 17:34:59 +0200 Subject: [PATCH 1/2] fix(pretty-format): handle `tagName` not being a string --- CHANGELOG.md | 2 ++ .../src/__tests__/DOMElement.test.ts | 18 ++++++++++++++++++ .../pretty-format/src/plugins/DOMElement.ts | 6 ++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 093d3cc6a965..4c68cb64bea0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[pretty-format]` Handle `tagName` not being a string + ### Chore & Maintenance ### Performance diff --git a/packages/pretty-format/src/__tests__/DOMElement.test.ts b/packages/pretty-format/src/__tests__/DOMElement.test.ts index 6fb25f764095..6ad40955532e 100644 --- a/packages/pretty-format/src/__tests__/DOMElement.test.ts +++ b/packages/pretty-format/src/__tests__/DOMElement.test.ts @@ -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'), + ); + }); }); diff --git a/packages/pretty-format/src/plugins/DOMElement.ts b/packages/pretty-format/src/plugins/DOMElement.ts index 75c47737af21..cb855856883e 100644 --- a/packages/pretty-format/src/plugins/DOMElement.ts +++ b/packages/pretty-format/src/plugins/DOMElement.ts @@ -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 && From 4322ed7a347c1092e8386e2cfd5c9d0483505cc8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 12 Aug 2020 17:37:53 +0200 Subject: [PATCH 2/2] link to PR --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c68cb64bea0..f589a87d9217 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- `[pretty-format]` Handle `tagName` not being a string +- `[pretty-format]` Handle `tagName` not being a string ([#10397](https://github.com/facebook/jest/pull/10397)) ### Chore & Maintenance