Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With the help of @jcamposmk, consulting the documentation of the react testing library and analyzing some articles about the author of the library, we found some recommendations on tests, which may be useful. We would like to know your opinions on this.
There is no need to call the cleanup function as it is called automatically for Jest and other test frameworks.
The use of screen is recommended:
The benefit of using screen is you no longer need to keep the render call destructure up-to-date as you add/remove the queries you need. You only need to type screen. and let your editor's magic autocomplete take care of the rest.
*It seems like the returned object from the render function could be framework specific but the screen object will be framework agnostic, making your tests more resilient and consistent across projects.
Wrapping things in
act
unnecessarily. Some queries and functions are already wrapped inact
Use
*byRole
most of the time:There's not much you can't get with this (if you can't, it's possible your UI is inaccessible).
Queries priority
Do not use
waitFor
to wait for items that can be queried withfind*
(
find*
queries usewaitFor
under the hood)Do not put multiple assertions in a single
waitFor
callbackBy putting a single assertion, we can wait for the UI to settle to the state we want to assert on, and also fail faster if one of the assertions do end up failing.
Use @testing-library/user-event
Is a package that's built on top of
fireEvent
, but it provides several methods that resemble the user interactions more closely. It provides more advanced simulation of browser interactions than the built-infireEvent
method.Common mistakes
Cheatsheet
Other changes:
The
data-testid
attribute was added to theLoading
component to obtain the component by this attribute and not by the name of the class. This way we make sure that the test keeps working no matter if the class name changes.In production: You can compile this attribute away with
babel-plugin-react-remove-properties
.The aria-label attribute was added to the form and the inputs fields to differentiate them by their role and accessibility name
Using aria-label attribute