Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 15, 2021 09:54 am GMT

How our team writes React tests and other stuff too 5 June 2021 8 minute read

Have you ever worked in a codebase where each test file was different to the next? Different terminology and grouping of test scenarios; different usages of snapshot testing and react-testing-library.

The effect of this meant wasted time reading a full test file to understand its structure before adding another test case. Creating new tests involved making multiple decisions; should I copy the structure of an existing test or create a new one? Should I use snapshot tests?

We solved this problem by defining guidelines to follow, that make writing tests that are easy to read and write, and would reduced wasted time on decisions.

Our guidelines are as follows:

  • Group tests by render and user behaviours
  • Use a snapshot test to validate render
  • Use GIVEN-WHEN-THEN to describe branching logic
  • Don't be afraid to stub child components
  • Don't re-test functionality of other components
  • Use a single expect statement per test
  • Accept unit testing UI components can be difficult
  • Try use screen.getByRole
  • Isolate complex component logic, and test separately

For full detail on our approach and each guideline for writing tests see https://whassup.github.io/blog/article/how-our-team-writes-tests


Original Link: https://dev.to/whassup/how-our-team-writes-react-tests-and-other-stuff-too-5-june-2021-8-minute-read-29c

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To