Closed
Description
Environment
@testing-library/dom
version: 7.22.1jest
version: 22.4.4jest-environment-jsdom
version: 22.4.3yarn
version: 1.22.4node
version: 10.15.3
Relevant code or config:
export const App = () => (
<div>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
);
import "@testing-library/jest-dom/extend-expect";
import React from "react";
import { render } from "@testing-library/react";
import { App } from "../index";
test("React Testing Library works!", () => {
const { getByRole } = render(<App />);
const passwordField = getByRole("textbox");
expect(passwordField).toBeInTheDocument();
});
What you did:
I tried to get a password input using getByRole("textbox")
. I've also tried getByLabelText("Password")
.
What happened:
No element is found, and an error message such as Unable to find an element with the role "textbox"
is shown.
Reproduction:
https://codesandbox.io/s/react-testing-library-getbyrole-password-issue-roy0l
Problem description:
It makes it difficult to test a login form without falling back to query selectors or test IDs, something that the library guidelines say should only be used if there is no other choice.
Suggested solution:
Either allow password fields to be found using getByRole("textbox")
or support another role if appropriate.
Supporting password fields in getByLabelText
would also solve the problem for me.