I’m working on a Vue component that contains a custom element with a shadow DOM. I want to test the content inside the shadow root of this custom element using Vue Test Utils. However, when I use wrapper.find("custom-element").html(), it only returns the outer HTML of the wrapper element, not the content inside the shadow DOM.
What I’ve Tried: I’ve tried using
wrapper.find("custom-element").shadowRoot
and
wrapper.find("custom-element").element.shadowRoot
, but I’m still unable to access the HTML inside the shadow DOM. Meaning this below code give null value,
const root = await wrapper.find("custom-header").element.shadowRoot;
console.log('***** : ', root.querySelector("nav"))
Note: the web component shadowRoot is open only.
Expected Behavior: I want to be able to access and test the content inside the shadow DOM of the custom element.
What I Need Help With: How can I correctly access the shadow DOM of a custom element in Vue Test Utils and retrieve its HTML content for testing purposes? Are there any additional steps or considerations I need to keep in mind when working with shadow DOMs in unit tests?
Any help would be greatly appreciated! Thank you in advance!
root.querySelector("nav")orrootthat is null? What about "What is source code for the tested component"? The question currently lacks clear problem statement. Jsdom supports shadow dom in general afaik, but it may have problems with web components in case you use them. The problem is specific to your case. It's possible that the element really doesn't exist at the time it's accessed. The question needs stackoverflow.com/help/mcve to rule this out. I'd suggest to check first if something changes if you use happy-dom instead of jsdom, which vitest supports too