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

Support anonymous custom element constructors #15138

Merged
merged 8 commits into from
Jun 30, 2024
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.
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 @@ -67,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`
Expand Down
3 changes: 3 additions & 0 deletions packages/pretty-format/src/__tests__/DOMElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
'<custom-element></custom-element>',
'<custom-extended-element></custom-extended-element>',
'<p is="custom-paragraph"></p>',
'<anonymous-element></anonymous-element>',
].join('');

expect(parent).toPrettyPrintTo(
Expand All @@ -373,6 +375,7 @@ Testing.`;
' <p',
' is="custom-paragraph"',
' />',
' <anonymous-element />',
'</div>',
].join('\n'),
);
Expand Down
17 changes: 11 additions & 6 deletions packages/pretty-format/src/plugins/DOMElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,29 @@ const testHasAttribute = (val: any) => {
}
};

const isCustomElement = (val: any) => {
const tagName = val?.tagName;
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')
);
};

export const test: NewPlugin['test'] = (val: any) =>
val?.constructor?.name && testNode(val);
(val?.constructor?.name || isCustomElement(val)) && testNode(val);

type HandledType = Element | Text | Comment | DocumentFragment;

Expand Down
Loading