- About Queries | Testing Library ← understand types of queries like
getBy,findBy,queryBy,…
You should find your elements in the test the same way users will find them! For example, testing-library doesn’t have
getById for an input because users don’t care about an id of an element, they find that element by its placeholder text instead.Use
data-testid and .getByTestId().yarn test (jest --watch in package.json) then press p to filter based on file name regex.- Expect not to be in the document →
queryByinstead offindBybecausequeryByreturns a null (no error) whereasfindBythrows an error instead!
- Expect to be in the docyment → can use either
queryByorfindBy!
- Find an image →
getByAltText
- Find an input
Or using
ByPlacehoderText- Dropdown options → check this SO.
- Idea: Add
data-testidto options, then get the options bygetAllByTestId, then check the sected option. - Can use
getByDisplayValuefor selected option.
Check more in Unit Testing with JestJS
Check more in Unit Testing with JestJS