Skip to content

Using @testing-library/react and @testing-library/user-event with vitest's browser mode? #6585

Answered by sheremet-va
gaspardip asked this question in Q&A
Discussion options

You must be logged in to vote

screen.getByRole returns a Locator, not the HTML Element. You can call .element() or .query() to return the HTML element itself to pass it down to @testing-library/user-event:

await userEvent.hover(screen.getByRole('button').element());

This works using jsdom but fails miserably on browser mode because of playwright's actionability checks (the element is never actually clicked because it cannot be clicked as far as I can tell, which makes sense)

You can disable actionability checks by providing force: true as an option to .click:

await userEvent.click(screen.getByRole('button'), { force: true });

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by gaspardip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants