Testing Library

Testing Library

  • Docs
  • Recipes
  • Help
  • Blog

›Guides

Guides

  • Recipes
  • Which query should I use?
  • Appearance and Disappearance

Examples

  • Codesandbox Examples
  • Input Event
  • Update Props
  • React Context
  • React Intl
  • React Redux
  • React Router
  • Reach Router
  • React Transition Group
  • External Examples

Help

  • Learning Material
  • Contributing
Edit

Which query should I use?

Priority

Based on the Guiding Principles, your test should resemble how users interact with your code (component, page, etc.) as much as possible. With this in mind, we recommend this order of priority:

  1. Queries Accessible to Everyone queries that reflect the experience of visual/mouse users as well as those that use assistive technology
    1. getByLabelText: Only really good for form fields, but this is the number one method a user finds those elements, so it should be your top preference.
    2. getByPlaceholderText: A placeholder is not a substitute for a label. But if that's all you have, then it's better than alternatives.
    3. getByText: Not useful for forms, but this is the number 1 method a user finds other elements (like buttons to click), so it should be your top preference for non-form elements.
    4. getByDisplayValue: The current value of a form element can be useful when navigating a page with filled-in values.
  2. Semantic Queries HTML5 and ARIA compliant selectors. Note that the user experience of interacting with these attributes varies greatly across browsers and assistive technology.
    1. getByAltText: If your element is one which supports alt text (img, area, and input), then you can use this to find that element.
    2. getByTitle: The title attribute is not consistently read by screenreaders, and is not visible by default for sighted users
    3. getByRole: This can be used to select dialog boxes and other difficult-to-capture elements in a more semantic way
  3. Test IDs
    1. getByTestId: The user cannot see (or hear) these, so this is only recommended for cases where you can't match by text or it doesn't make sense (the text is dynamic).

Manual Queries

On top of the queries provided by the testing library, you can use the regular querySelector DOM API to query elements. Note that using this as an escape hatch to query by class or id is a bad practice because users can't see or identify these attributes. Use a testid if you have to.

// @testing-library/react
const { container } = render(<MyComponent />)
const foo = container.querySelector('[data-foo="bar"]')
Last updated on 6/1/2019
← RecipesAppearance and Disappearance →
  • Priority
  • Manual Queries
Testing Library
Docs
Getting StartedExamplesAPIHelp
Community
BlogStack OverflowReactiflux on DiscordSpectrum
More
StarGitHubEdit Docs on GitHub
Copyright © 2018-2019 Kent C. Dodds and contributors