리액트 테스팅 라이브러리 사용시 자주 저지르는 실수 를 읽고 핵심되는 실수만 요약했다.
Using the wrong assertion
: That toBeDisabled assertion comes from jest-dom. It's strongly recommended to use jest-dom because the error messages you get with it are much better.
Using the wrong query
- Use screen
- Use getbyrole
- Adding aria-, role, and other accessibility attributes incorrectly
- confuse screen readers and their users.
Use ‘userEvent’ instead of ‘fireEvent’
- @testing-library/user-event is a package that's built on top of fireEvent,
use find* any time you want to query for something that may not be available right away.
: find uses waitFor
under the hood. so if you want to find some element that could appear after couple of seconds, use find variant.
query* variant throws no error, but null if nothing matches
: The only reason the query* variant of the queries is exposed is for you to have a function you can call which does not throw an error if no element is found to match the query (it returns null if no element is found).
only put one assertion in a callback for ‘waitFor’.
put side-effects outside waitFor callbacks and reserve the callback for assertions only.