Skip to content

Prefer findByRole query over using waitFor + getByRole #1186

@shawnesquivel

Description

@shawnesquivel

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

  1. Following the example https://github.com/testing-library/testing-library-docs/blob/main/docs/react-testing-library/example-intro.mdx
  2. On Line 96
  await waitFor(() => screen.getByRole('heading'))
  1. Throws the eslint error
Prefer `findByRole` query over using `waitFor` + `getByRole`

Expected behavior Expect to wait until "heading" element is rendered by the Fetch.js component (see step 1 example for file)

  • OS: Windows 10

Additional context
When using the CLI fix recommendation, it changes the line to read

  await screen.findByRole("heading");

However, the same error is thrown.

Additional Resources
https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/prefer-find-by.md
https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#using-waitfor-to-wait-for-elements-that-can-be-queried-with-find

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions