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

Queries can not find elements in open shadow dom (& weird prettyDOM output) #742

Closed
aryzing opened this issue Aug 17, 2020 · 11 comments
Closed

Comments

@aryzing
Copy link

aryzing commented Aug 17, 2020

  • @testing-library/dom version: "7.22.0"
  • Testing Framework and version: Jest "26.2.2"
  • DOM Environment: "16.3.0"

Relevant code or config:

After appending a custom element to the DOM,

test("Renders content when attached to DOM", async (): Promise<void> => {
  const testComponent = new TestComponent({ label: "counter" }, { count: 0 });

  if (!testComponent.shadowRoot) {
    throw new Error("Expected an open shadow DOM to be attached.");
  }

  document.body.append(testComponent);

  // Works! But Typescript complains about the type of shadowRoot.
  // Recommendation taken from https://github.com/testing-library/dom-testing-library/issues/413#issuecomment-660039128
  // Unsure if this is even correct usage of testing-library (this is not documented).
  expect(queryAllByText(testComponent.shadowRoot, "counter")).toHaveLength(1);

  // Fails... :(
  expect(screen.queryAllByText("counter")).toHaveLength(1);
});

What you did:

Queried for text that is in the DOM.

What happened:

Assertion fails b/c query is unable to find element in shadow DOM. Also, interestingly when using prettyDOM, the custom element is shown as an object rather than with its tag, <test-component>:

      <body>
        TestComponent {
          "props": Object {
            "label": "counter",
          },
          "setProps": [Function],
          "setState": [Function],
          "state": Object {
            "count": 0,
          },
        }
      </body>

Reproduction:

(do you have a non-React Codesandbox I could use?)

Managed to pull this together in the one suggested, but even the expect that is working on my local fails in the codesandbox. https://codesandbox.io/s/react-testing-library-demo-forked-126wx?file=/src/__tests__/hello.js

Problem description:

The queries are described as being able to find relevant nodes in DOM, yet are unable to find elements when they are in a custom element's shadow DOM.

Suggested solution:

I'm sorry, i am unable to provide any suggestions beyond having queries meet the expectation that they find elements in the DOM, including when they are inside the shadow DOM of a custom element.

@kentcdodds
Copy link
Member

Hi @aryzing,

Check out this thread: #413

TL;DR, the only way to get this support today is to use this: https://www.npmjs.com/package/testing-library__dom

It works exactly the same as @testing-library/dom except it also supports the shadow DOM.

@Stolle95
Copy link

Hi @aryzing,

Check out this thread: #413

TL;DR, the only way to get this support today is to use this: https://www.npmjs.com/package/testing-library__dom

It works exactly the same as @testing-library/dom except it also supports the shadow DOM.

Hello!
@testing-library__dom  seems amazing and I'm eager to try it out.
I'm having difficulty understanding how to replace @testing-library/dom to @testing-library__dom in order to be able to access elements from shadow DOM.

Has anyone made this change and successfully accessed elements from shadow dom?

@aryzing
Copy link
Author

aryzing commented Jan 13, 2021

@Stolle95 have a look at this repo.

@Stolle95
Copy link

Stolle95 commented Jan 13, 2021

@aryzing Thanks for the quick answer. I can see you are testing with a click-event. Did you try to change the value in an input field?
Also, is it supposed to use the render function from @testing-library/react?

@bigfish
Copy link

bigfish commented May 12, 2021

I too am having difficulty using the proposed replacement 'testing-library__dom', even after looking at aryzing's repo. Can this solution be documentend properly by the maintainer, as I didn't find any documentation relating to it, and the repo link in npm just points to the spectrum components repo. I tried using it, but no queries executed on the 'screen' exported by the library are able to find elements within the shadow dom. So it seems like the statement that 'It works exactly the same as @testing-library/dom except it also supports the shadow DOM' is false. Unless I'm missing something ??? Again, documentation would be nice. I'm frustrated that shadow DOM is not something supported out of the box as it is a standard part of the web platform now for a couple of years and jsdom supports it apparently. If it is not going to be supported, I (& many others!) will have to abandon @testing-library/* and use something else like Playwright, which does support shadow dom queries transparently.

@gabrielbeauchemin
Copy link

Please reopen this issue. I think shadow dom should be supported.

@mkjoc
Copy link

mkjoc commented Sep 19, 2021

Anyone know if this issue got resolved ? I have same issue of shadow dom as above mentioned.

@gabrielbeauchemin
Copy link

No this is not resolved on the testing-library app. If you have your own components wrapping imported web components, you should still be able to test your them (no need to have shadow dom supported to test that in most cases).

If you have implemented your own web components and would like to test them with the testing-library app, it will not work because shadow dom is not supported for now. In htis case you can look at testing-library__dom, Stencil or Puppeteer.

@alineDiesel
Copy link

Im with the same problem. Any more suggestions?

@eriklharper
Copy link

Please reopen this issue. I think shadow dom should be supported.

I agree. If this is a DOM testing library then it should be able to test all forms of DOM.

@ThijSlim
Copy link

ThijSlim commented Aug 2, 2022

Just recently a package appeared which makes it really easy to interact with the shadow Dom using Testing Library methods:
https://www.npmjs.com/package/shadow-dom-testing-library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants