- 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 β
queryBy
instead offindBy
becausequeryBy
returns a null (no error) whereasfindBy
throws an error instead!
- Expect to be in the docyment β can use either
queryBy
orfindBy
!
- Find an image β
getByAltText
- Find an input
Or using
ByPlacehoderText
- Dropdown options β check this SO.
- Idea: Add
data-testid
to options, then get the options bygetAllByTestId
, then check the sected option. - Can use
getByDisplayValue
for selected option.
Check more in Unit Testing with JestJS
Β
Check more in Unit Testing with JestJS