DEV Community

[Comment from a deleted post]
Collapse
 
stemmlerjs profile image
Khalil Stemmler

In Uncle Bob's book, "The Clean Coder", he says that everything should be tested.

Although, he goes on to say that writing tests for front-end presentational code like the stuff we do in React are non-ideal because it's the most susceptible for change.

I used to test all my front end code with Cypress and other E2E tools, but those tests are incredibly flimsy because as soon as the selectors are altered in any way, the tests break. You spend a lot of time updating tests for front-end code. Generally speaking, my opinion is that it isn't worth it.

Instead, you want to test the things that don't change that often. Things that aren't that volatile, like the features.

For example, if you were to test a Login Page, how would you go about doing that? It's a pretty important page to ensure that it works. What would you do?

You could test against the input fields and whatnot, but as soon as we want to change the style, there's a chance we might break those integration tests.

What's a better way to test the feature or Logging in?

Through the API. Because it's a lot less likely to change.

In "The Clean Coder", Uncle Bob recommends us to refrain from testing volatile things like the views, and instead test that the features work through the API calls.

If you have time, writing Cypress tests and testing the view is nice, but your entire goal is to reduce the amount of testing "implementation details" (a div was used here, and a span with color: green was used here), and instead test the "business rules".

The testing of backend code is a lot easier to understand the principles of writing testable code in my opinion.