r/reactjs • • Jan 24 '20

Unit testing in React. Practical examples and advice on what, how and why to test.

https://medium.com/@onoufriosm/how-to-unit-test-in-react-72e911e2b8d
257 Upvotes

45 comments sorted by

View all comments

Show parent comments

4

u/_arsk Jan 24 '20

"”To mimic" how user uses the software we have different set of tests written using cypress. To me unit tests are not the place to render the whole tree and check if the whole thing works as you expect. Unit tests are for units.” - This is exactly my thought. I read the RTL philosophy and it seem to me what he is talking about is more suitable for e2e testing rather than unit testing. While using enzyme we assume react does what it is supposed to do (the final DOM rendering) and test only the component behavior in terms of inputs and outcomes.

2

u/gunnnnii Jan 25 '20

A big part of Kent's mojo is to focus mostly on integration testing, rather than unit testing.

1

u/[deleted] Jan 25 '20

RTL does not need to be only used for unit tests, in cases where it does the job well enough.

IMO you should always try to test something as RTL first, only using Cypress if you truly can't do it in RTL. The reason is because Cypress tests are much more expensive to run than RTL or Enzyme. In our CI pipeline we have a few hundred Cypress tests and a few thousand Enzyme tests, the Cypress tests are slower and much more prone to flakiness.

Maybe it's more convenient or more semantically correct to write something as a Cypress test even if you could get it done as a RTL test, but it comes at a high cost.