Yesterday, I practiced writing unit tests while following the Thinking in React – React documentation. I initially wrote layout specs in the tests as well, but removed them after a couple of commits for the reasons explained below.
Remove unnecessary Test suit · mu-hun/thinking-in-react@b004b87 · GitHub
How far should tests go?
- Verifying the text of fixed
<td/>elements - Checking if styles change based on
props - Whether
onChangeevents fire correctly
None of the above. I believe these three test suites imply a lack of trust in React itself.
So what should we test?
Verify that the expected view is rendered for a given combination of props and state.
We should use tests to verify that the design we are responsible for writing produces the desired results. Conversely, we have no responsibility to write tests for levels of design we didn’t write or for static elements with no side effects. The tools we use already handle those well enough.