diff --git a/src/__node_tests__/index.js b/src/__node_tests__/index.js index fabe3534..d1b08a12 100644 --- a/src/__node_tests__/index.js +++ b/src/__node_tests__/index.js @@ -76,3 +76,23 @@ test('works without a browser context on a dom node (JSDOM Fragment)', () => { /> `) }) + +test('byRole works without a global DOM', () => { + const { + window: { + document: {body: container}, + }, + } = new JSDOM(` + + + + + + `) + + expect(dtl.getByRole(container, 'button')).toMatchInlineSnapshot(` + + `) +}) diff --git a/src/role-helpers.js b/src/role-helpers.js index c7ff74c0..7948f4e2 100644 --- a/src/role-helpers.js +++ b/src/role-helpers.js @@ -15,6 +15,7 @@ const elementRoleList = buildElementRoleList(elementRoles) * @returns {boolean} true if excluded, otherwise false */ function shouldExcludeFromA11yTree(element) { + const window = element.ownerDocument.defaultView const computedStyle = window.getComputedStyle(element) // since visibility is inherited we can exit early if (computedStyle.visibility === 'hidden') {