The other day I needed to test a React hook with Ava, which lead me to try to use React Testing Library, and it took me a few minutes to figure out the best way to do so because the documentation to do so was spread over several pages.
I’m working with npm, so adjust commands as needed. First, set up some packages:
|
|
Now we can write a basic test. This is a trivial example, but shows how everything comes together.
|
|
The crucial bits for getting this all working:
import "global-dom/register";
sets up jsdom globally in our tests.test.afterEach(cleanup);
makes sure that React Testing Library gets cleaned up after each test.
The rest is just normal React Testing Library usage.
Love it? Hate it? Have something to say? Let me know at comments@nalanj.dev.