An extension of DOM-testing-library to provide hooks into the shadow dom
npm install shadow-dom-testing-libraryCurrently, DOM-testing-library does not support checking
shadow roots for elements. This can be troublesome when
you're looking for something with a "button" that's
nested inside a shadowRoot.
testing-library__dom is a hard fork of DOM testing
library which presents its own set of challenges.
shadow-dom-testing-library looks to augment the existing
functionality.
Make sure you are using a library which supports rendering
shadow roots. For Jest users, this means ensuring you have
JSDOM >= 16.2 and Jest >= 26.2
``bash`
npm install -D shadow-dom-testing-library
`js
// my-button.jsx
export default () => (
);
// my-button.test.jsx
import { render } from "@testing-library/react";
import { screen } from "shadow-dom-testing-library";
import Button from "./my-button";
test("Find the button in the shadow root", async () => {
render();
const btn = await screen.findByShadowRole("button");
expect(btn).toBeInTheDocument();
});
`
All queries found here:
are implemented with a "Shadow" prefix prior to the query
type.
`js
import { render } from "@testing-library/react"
import { getByShadowRole, findByShadowLabelText, queryAllByShadowTitle } from "shadow-dom-testing-library"
test("Find the button", () => {
const { container } = render()
getByShadowRole(container, "button")
await findByShadowLabelText(container, /Car Manufacturer/i)
queryAllByShadowTitle(container, "delete")
})
`
Shadow dom testing library ships its own "screen" that
you're familiar with. It has all the functions prebound
to the document.
`jsx
import { render } from "@testing-library/react"
import { screen } from "shadow-dom-testing-library"
test("Lets test some rendering", () => {
render()
screen.getByShadowRole("button")
await screen.findByShadowLabelText(/Car Manufacturer/i)
screen.queryAllByShadowTitle("delete")
})
`
In addition, every function also accepts a
"shallow" option. The shallow option means to only go 1
shadowRoot deep. Perhaps in the future a "recurseDepth"
will be implemented to specify shadowRoot depth recursion.
`jsx
import { render } from "@testing-library/react"
import { screen, getByShadowRole } from "shadow-dom-testing-library"
test("Lets test some rendering", () => {
render()
getByShadowRole(document, "button", { depth: 1 })
await screen.findByShadowLabelText(/Car Manufacturer/i, { depth: 1 })
screen.queryAllByShadowTitle("delete", { depth: 1 })
})
`
Shadow DOM testing library also ships its own
"deepQuerySelector" and "deepQuerySelectorAll" functions
for if you need more fine-grained access to the DOM.
`js
import {
deepQuerySelector,
deepQuerySelectorAll,
} from "shadow-dom-testing-library";
const elements = deepQuerySelectorAll(document, "my-button");
const element = deepQuerySelector(document, "my-button", { depth: 1 });
`
A within function is exported to provide the queries
bound to a particular container element.
`jsx
import { render } from "@testing-library/react";
import { screen, within } from "shadow-dom-testing-library";
test("Lets test some rendering", () => {
render(
const fieldGroup = screen.getByShadowRole("group");
const nameInput = within(fieldGroup).getByShadowRole("textbox", {
name: "foobar",
});
});
`
Be careful with the shadowQueries and deepQueries. These
functions recurse through every shadow root which can
easily lead to unintended elements being found in your
tests.
Also, this library is very new, use with caution. Feel free
to report any issues.
Recursing through the Shadow DOM can be expensive if you
render a large number of elements in an element. Benchmarks
have not been measured, but it will easily be much worse
than a regular querySelector call.
Shadow queries will work for both Light DOM and for
Shadow DOM elements. For example you can search for a
"button" in the Light DOM.
`jsx
function SimpleButton() {
const [count, setCount] = React.useState(0);
return ;
}
import { screen } from "shadow-dom-testing-library";
test("Regular buttons should also work with shadow query", async () => {
render(
fireEvent.click(await screen.findByRole("button"));
const el = await screen.findByText(/1/);
expect(el).toBeInTheDocument();
});
`
Shadow-dom-testing-library supports serializing shadow
doms. To do so, it ships its own "debug" function attached
to the screen.
`js
import { screen, render } from "shadow-dom-testing-library";
test("Debug", () => {
render(
screen.debug();
});
// Calling debug directly
import { debug, render } from "shadow-dom-testing-library";
test("Debug", () => {
render(
debug();
});
// Changing indentation
import { screen, render } from "shadow-dom-testing-library";
test("Debug", () => {
render(
debug(document.documentElement, undefined, { indent: 4 });
});
// Changing max depth
import { screen, render } from "shadow-dom-testing-library";
test("Debug", () => {
render(
debug(document.documentElement, undefined, {
// default is 7000.
maxDepth: 100,
});
});
`
This is the equivalent of logDOM from "@testing-library/dom"
logShadowDOM will log to the console the state of the DOMprettyShadowDOM
and internally calls . This is called via screen.debug()
`js
import { logShadowDOM } from "shadow-dom-testing-library";
logShadowDOM(element, maxLength, options); // void; calls console.log()
`
This is the equivalent of prettyDOM from "@testing-library/dom". This is called by logShadowDOM.prettyShadowDOM returns string | false and does not automatically log to the console.
This is useful for custom error messages for elements.
`js
import { prettyShadowDOM } from "shadow-dom-testing-library";
prettyShadowDOM(element, maxLength, options); // => string | false
`
Perhaps you don't want the extended screen. That's fine. To
import just the shadowQueries you can do so like this:
`js
import { shadowQueries } from "shadow-dom-testing-library";
test("findByShadowRole", async () => {
render();
const btn = await shadowQueries.findByShadowRole("button");
expect(btn).toBeInTheDocument();
});
`
You could also use this method to extend your own screen.
`js
import { shadowQueries } from "shadow-dom-testing-library";
import { screen as DOMScreen } from "@testing-library/dom";
const screen = {
...DOMScreen,
...shadowQueries,
};
screen.getByShadowRole("button");
`
`ts
import { screen, shadowQueries } from "shadow-dom-testing-library";
const btn = await screen.findByShadowRole
const btn = shadowQueries.getByShadowRole
``