Skip to content

*ByLabelText can't find password inputs #737

Closed
@DanielGibbsNZ

Description

@DanielGibbsNZ

Environment

  • @testing-library/dom version: 7.22.1
  • jest version: 22.4.4
  • jest-environment-jsdom version: 22.4.3
  • yarn version: 1.22.4
  • node 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsdomIssue with JSDOM environment

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions