testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.26k stars 467 forks source link

logRoles doesn't support RenderResult #1165

Closed amaschas closed 1 year ago

amaschas commented 2 years ago

Relevant code or config:

test("A sample test.", () => {
    const component = render(() => (<div>Testing</div>));
    logRoles(component);
});

What you did:

I'm trying to log the roles available in a rendered component.

What happened:

I get an error telling me that logRoles only works with HTMLElement and not RenderResult.

Problem description:

I'm not really sure how logRoles is even supposed to be used, given that it can't show me the roles available in a RenderResult. It's actually easier to just to screen.getByRole('foo') at this point to find out what roles are available within your component. I think that logRoles should either just take a RenderResult, or the documentation should explain how to use logRoles with a RenderResult, since most users are probably just going to render their test component, and then want to see what roles they can test for.

cupojoe commented 1 year ago

You can log roles by

test("A sample test.", () => {
    const { container } = render(() => (<div>Testing</div>));
    logRoles(container);
});

The result object includes other things besides the DOM.

eps1lon commented 1 year ago

This is working as intended. You probably want to do what Joel explained in https://github.com/testing-library/dom-testing-library/issues/1165#issuecomment-1509356454:

test("A sample test.", () => {
    const { container } = render(() => (<div>Testing</div>));
    logRoles(container);
});