From a4411d123a2f1b7e072a3601936882d41c7aac45 Mon Sep 17 00:00:00 2001 From: Konnor Rogers Date: Wed, 19 Jun 2024 16:07:08 -0400 Subject: [PATCH 1/8] Support anonymous custom element constructors --- packages/pretty-format/src/plugins/DOMElement.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pretty-format/src/plugins/DOMElement.ts b/packages/pretty-format/src/plugins/DOMElement.ts index ca2fe9631d47..9edbf909f0fe 100644 --- a/packages/pretty-format/src/plugins/DOMElement.ts +++ b/packages/pretty-format/src/plugins/DOMElement.ts @@ -47,7 +47,7 @@ const testNode = (val: any) => { }; export const test: NewPlugin['test'] = (val: any) => - val?.constructor?.name && testNode(val); + val?.constructor && testNode(val); type HandledType = Element | Text | Comment | DocumentFragment; From ea52ca4add059a77618bc26bd2c3fc38056394a5 Mon Sep 17 00:00:00 2001 From: Konnor Rogers Date: Mon, 24 Jun 2024 03:40:36 -0400 Subject: [PATCH 2/8] Update DOMElement.ts --- packages/pretty-format/src/plugins/DOMElement.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/pretty-format/src/plugins/DOMElement.ts b/packages/pretty-format/src/plugins/DOMElement.ts index 9edbf909f0fe..262b7fbaffc2 100644 --- a/packages/pretty-format/src/plugins/DOMElement.ts +++ b/packages/pretty-format/src/plugins/DOMElement.ts @@ -30,16 +30,19 @@ const testHasAttribute = (val: any) => { } }; +const isCustomElement = (val: any) => { + const { tagName } = val + return (typeof tagName === 'string' && tagName.includes('-')) || + testHasAttribute(val); +} + const testNode = (val: any) => { const constructorName = val.constructor.name; - const {nodeType, tagName} = val; - const isCustomElement = - (typeof tagName === 'string' && tagName.includes('-')) || - testHasAttribute(val); + const {nodeType} = val; return ( (nodeType === ELEMENT_NODE && - (ELEMENT_REGEXP.test(constructorName) || isCustomElement)) || + (ELEMENT_REGEXP.test(constructorName) || isCustomElement(val))) || (nodeType === TEXT_NODE && constructorName === 'Text') || (nodeType === COMMENT_NODE && constructorName === 'Comment') || (nodeType === FRAGMENT_NODE && constructorName === 'DocumentFragment') @@ -47,7 +50,7 @@ const testNode = (val: any) => { }; export const test: NewPlugin['test'] = (val: any) => - val?.constructor && testNode(val); + (val?.constructor?.name || isCustomElement(val)) && testNode(val); type HandledType = Element | Text | Comment | DocumentFragment; From a04f2da134ac83672df8413310e527a7a5525b46 Mon Sep 17 00:00:00 2001 From: Konnor Rogers Date: Mon, 24 Jun 2024 03:41:29 -0400 Subject: [PATCH 3/8] Update DOMElement.ts --- packages/pretty-format/src/plugins/DOMElement.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/pretty-format/src/plugins/DOMElement.ts b/packages/pretty-format/src/plugins/DOMElement.ts index 262b7fbaffc2..758b0d20998a 100644 --- a/packages/pretty-format/src/plugins/DOMElement.ts +++ b/packages/pretty-format/src/plugins/DOMElement.ts @@ -32,8 +32,7 @@ const testHasAttribute = (val: any) => { const isCustomElement = (val: any) => { const { tagName } = val - return (typeof tagName === 'string' && tagName.includes('-')) || - testHasAttribute(val); + return ((typeof tagName === 'string' && tagName.includes('-')) || testHasAttribute(val)); } const testNode = (val: any) => { From 71cf82880330ae2187e8f7a16439e27c3e755cd7 Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Mon, 24 Jun 2024 12:46:47 -0400 Subject: [PATCH 4/8] add test for anonymous custom element --- packages/pretty-format/src/__tests__/DOMElement.test.ts | 3 +++ packages/pretty-format/src/plugins/DOMElement.ts | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/pretty-format/src/__tests__/DOMElement.test.ts b/packages/pretty-format/src/__tests__/DOMElement.test.ts index 0c961093ecec..de9a4811e633 100644 --- a/packages/pretty-format/src/__tests__/DOMElement.test.ts +++ b/packages/pretty-format/src/__tests__/DOMElement.test.ts @@ -357,12 +357,14 @@ Testing.`; customElements.define('custom-paragraph', CustomParagraphElement, { extends: 'p', }); + customElements.define('anonymous-element', class extends HTMLElement {}); const parent = document.createElement('div'); parent.innerHTML = [ '', '', '

', + '', ].join(''); expect(parent).toPrettyPrintTo( @@ -373,6 +375,7 @@ Testing.`; ' ', + ' ', '', ].join('\n'), ); diff --git a/packages/pretty-format/src/plugins/DOMElement.ts b/packages/pretty-format/src/plugins/DOMElement.ts index 758b0d20998a..9cf39e547786 100644 --- a/packages/pretty-format/src/plugins/DOMElement.ts +++ b/packages/pretty-format/src/plugins/DOMElement.ts @@ -31,9 +31,12 @@ const testHasAttribute = (val: any) => { }; const isCustomElement = (val: any) => { - const { tagName } = val - return ((typeof tagName === 'string' && tagName.includes('-')) || testHasAttribute(val)); -} + const {tagName} = val; + return ( + (typeof tagName === 'string' && tagName.includes('-')) || + testHasAttribute(val) + ); +}; const testNode = (val: any) => { const constructorName = val.constructor.name; From b669ed16a6558d268bae38bf4c69bc821ed50aeb Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Mon, 24 Jun 2024 12:49:09 -0400 Subject: [PATCH 5/8] Add changelog note --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c0004c09728..275c5ffefffe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[pretty-format]` Fix bug with "anonymous custom elements" not being printed as expected. ([#15138](https://github.com/jestjs/jest/pull/15138)) - `[jest-circus, jest-cli, jest-config]` Add `waitNextEventLoopTurnForUnhandledRejectionEvents` flag to minimise performance impact of correct detection of unhandled promise rejections introduced in [#14315](https://github.com/jestjs/jest/pull/14315) ([#14681](https://github.com/jestjs/jest/pull/14681)) - `[jest-circus]` Add a `waitBeforeRetry` option to `jest.retryTimes` ([#14738](https://github.com/jestjs/jest/pull/14738)) - `[jest-circus]` Add a `retryImmediately` option to `jest.retryTimes` ([#14696](https://github.com/jestjs/jest/pull/14696)) From 67bf11eb8a96a69b273626a95719cf9b3f41647a Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Mon, 24 Jun 2024 12:49:41 -0400 Subject: [PATCH 6/8] Add changelog note --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 275c5ffefffe..0be44fa5b2e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ ### Features -- `[pretty-format]` Fix bug with "anonymous custom elements" not being printed as expected. ([#15138](https://github.com/jestjs/jest/pull/15138)) - `[jest-circus, jest-cli, jest-config]` Add `waitNextEventLoopTurnForUnhandledRejectionEvents` flag to minimise performance impact of correct detection of unhandled promise rejections introduced in [#14315](https://github.com/jestjs/jest/pull/14315) ([#14681](https://github.com/jestjs/jest/pull/14681)) - `[jest-circus]` Add a `waitBeforeRetry` option to `jest.retryTimes` ([#14738](https://github.com/jestjs/jest/pull/14738)) - `[jest-circus]` Add a `retryImmediately` option to `jest.retryTimes` ([#14696](https://github.com/jestjs/jest/pull/14696)) @@ -41,6 +40,7 @@ ### Fixes +- `[pretty-format]` Fixed a bug where "anonymous custom elements" were not being printed as expected. ([#15138](https://github.com/jestjs/jest/pull/15138)) - `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109)) - `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576)) - `[expect]` Improve diff for failing `expect.objectContaining` ([#15038](https://github.com/jestjs/jest/pull/15038)) From 069eb04d55d7b0199f536877a4dba125a157da40 Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Mon, 24 Jun 2024 13:46:47 -0400 Subject: [PATCH 7/8] tagName may not always exist --- packages/pretty-format/src/plugins/DOMElement.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pretty-format/src/plugins/DOMElement.ts b/packages/pretty-format/src/plugins/DOMElement.ts index 9cf39e547786..885b30e517b3 100644 --- a/packages/pretty-format/src/plugins/DOMElement.ts +++ b/packages/pretty-format/src/plugins/DOMElement.ts @@ -31,7 +31,7 @@ const testHasAttribute = (val: any) => { }; const isCustomElement = (val: any) => { - const {tagName} = val; + const tagName = val?.tagName; return ( (typeof tagName === 'string' && tagName.includes('-')) || testHasAttribute(val) From dfd09d61dcc14f2d140e0421e8773cde6d9978fc Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Tue, 25 Jun 2024 15:16:47 -0400 Subject: [PATCH 8/8] move position in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0be44fa5b2e6..1066a3b77bf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,7 +40,6 @@ ### Fixes -- `[pretty-format]` Fixed a bug where "anonymous custom elements" were not being printed as expected. ([#15138](https://github.com/jestjs/jest/pull/15138)) - `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109)) - `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576)) - `[expect]` Improve diff for failing `expect.objectContaining` ([#15038](https://github.com/jestjs/jest/pull/15038)) @@ -68,6 +67,7 @@ - `[jest-runtime]` Properly handle re-exported native modules in ESM via CJS ([#14589](https://github.com/jestjs/jest/pull/14589)) - `[jest-util]` Make sure `isInteractive` works in a browser ([#14552](https://github.com/jestjs/jest/pull/14552)) - `[pretty-format]` [**BREAKING**] Print `ArrayBuffer` and `DataView` correctly ([#14290](https://github.com/jestjs/jest/pull/14290)) +- `[pretty-format]` Fixed a bug where "anonymous custom elements" were not being printed as expected. ([#15138](https://github.com/jestjs/jest/pull/15138)) - `[jest-cli]` When specifying paths on the command line, only match against the relative paths of the test files ([#12519](https://github.com/jestjs/jest/pull/12519)) - [**BREAKING**] Changes `testPathPattern` configuration option to `testPathPatterns`, which now takes a list of patterns instead of the regex. - [**BREAKING**] `--testPathPattern` is now `--testPathPatterns`