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

bug: Error when using testing-library/dom getByRole #5252

Closed
3 tasks done
angelo-v opened this issue Jan 13, 2024 · 4 comments · Fixed by #5336
Closed
3 tasks done

bug: Error when using testing-library/dom getByRole #5252

angelo-v opened this issue Jan 13, 2024 · 4 comments · Fixed by #5336
Assignees
Labels
Bug: Validated This PR or Issue is verified to be a bug within Stencil Has Workaround This PR or Issue has a work around detailed within it.

Comments

@angelo-v
Copy link

angelo-v commented Jan 13, 2024

Prerequisites

Stencil Version

4.9.1

Current Behavior

When using testing-library/dom with stencil an error occurs when using getByRole:

Cannot read properties of null (reading 'defaultView')

Expected Behavior

getByRole should find elements with the given role, or say it could not be found, but it should not lead to the above error.

System Info

@testing-library/dom: 9.3.4
@testing-library/jest-dom: 6.2.0
System: node 20.10.0
    Compiler: [...]/node_modules/@stencil/core/compiler/stencil.js
       Build: 1704730339
     Stencil: 4.9.1 🍬
  TypeScript: 5.2.2
      Rollup: 2.42.3
      Parse5: 7.1.2
      jQuery: 4.0.0-pre
      Terser: 5.26.0

Steps to Reproduce

Component under test:

import { Component, h } from '@stencil/core';

@Component({
  tag: 'test-component',
  shadow: false,
})
export class TestComponent {
  render() {
    return <button>test</button>;
  }
}

test:

import { newSpecPage } from '@stencil/core/testing';
import { screen } from '@testing-library/dom';

import '@testing-library/jest-dom';
import { TestComponent } from './test-component';

describe('test component', () => {
  it('can find button using getByRole', async () => {
    await newSpecPage({
      components: [TestComponent],
      html: `<test-component/>`,
    });
    const button = screen.getByRole('button');
    expect(button).toEqualHtml('<button>test</button>');
  });
});

Code Reproduction URL

https://github.com/angelo-v/stencil-testing-library-dom-error/blob/main/src/components/my-component/my-component.spec.ts

Additional Information

It works when setting the hidden option:

screen.getByRole('button', {hidden: true});
@rwaskiewicz
Copy link
Member

Thanks @angelo-v!

I was able to confirm this is a bug with how Stencil represents the relationship between Nodes and their owning Document. I'm going to tag to this to get it ingested into our backlog - thanks again!

@rwaskiewicz rwaskiewicz removed their assignment Jan 16, 2024
@rwaskiewicz rwaskiewicz added the Bug: Validated This PR or Issue is verified to be a bug within Stencil label Jan 16, 2024
@ionitron-bot ionitron-bot bot removed the triage label Jan 16, 2024
@rwaskiewicz rwaskiewicz added the Has Workaround This PR or Issue has a work around detailed within it. label Jan 16, 2024
@christian-bromann christian-bromann self-assigned this Feb 7, 2024
github-merge-queue bot pushed a commit that referenced this issue Feb 8, 2024
…ull (#5336)

* fix(mock-doc): overwrite parentElement in MockHTMLElement to return null

fixes #5252
STENCIL-1104

* add test

* only return null for html elements

* fix unit tests

* tweak

* another tweak

* revert tests and correct implementation

* update tests

* prettier

* minor comment tweak

* fix assertion
@tanner-reits
Copy link
Member

A fix for this was included in today's v4.12.2 release!

@angelo-v
Copy link
Author

getByRole now seems to work, thank you!

But I am getting follow up errors with toBeInTheDocument matcher:

const list = await getByRole(page.root, 'list');
expect(list).toBeInTheDocument();

Right-hand side of 'instanceof' is not an object

Should I open a seperate issue for this?

@christian-bromann
Copy link
Member

Should I open a seperate issue for this?

Yes please! Can you also provide a minimal reproducible example again? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug: Validated This PR or Issue is verified to be a bug within Stencil Has Workaround This PR or Issue has a work around detailed within it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants