Single vs Multiple queries

In the previous lesson we covered the different types of queries (getBy, findBy, queryBy).

For all the functions that use those (like getByText()), they will throw if more than one element is found.

If you are expecting more than one result, then you have to use the ...AllBy... functions, like:

  • getAllByText() / findAllByText() / queryAllByText()
  • getAllByPlaceholderText() / findAllByPlaceholderText() / queryAllByPlaceholderText()

Basically just add the All suffix in the function you call:

  • getBy... -> getAllBy...
  • queryBy... -> queryAllBy...
  • findBy... -> findAllBy...

These will return an array of elements.

This is also covered in our main React testing course. If you've taken that already, you can skip this lesson.

We'll soon move into the more advanced query APIs.

Loading full lesson content & code...

Ready to try running tests for this lesson?