DEV Community

Discussion on: Unit testing frontend code is (probably) useless

Collapse
 
framirezsandino profile image
framirezsandino

I think this is a very controversial statement as previous posted comments have proven so far, but still, want to add my two cents. Before thinking on making UI Unit Testing you should think about if it is worth doing it, or, it is made to cover a design flaw in the app overall.

Let me use an example: let's say you have to provide a presentational message in UI to convert an input entered number by use into words, something like libraries such as npmjs.com/package/number-to-words do. Let's suppose you have to implement it from scratch and incorporate this functionality on UI only, as this value doesn't have neither to be persisted or used on BE services. In that case, you would need to ensure the UI implementation for a theoretically called numberToWordsInEnglish being called this way:

numberToWordsInEnglish(1000)

Will always produce as output:

"one thousand"

Instead of "one zero zero zero", "ten zero zero" or whatever other combination considered wrong under this context.

Also, let's say your initial implementation doesn't support decimals and what them to be added later, or maybe extended maximum number support. You may want to have a mechanism to validate any future change doesn't break the current implementation that, if made in smaller funcional pieces, can be tested only once using unit testing.

Now, for a different example, if you need to test the place you're placing this" number to words" message is correct, or, using enough contrast, or, even if the message is present at all in the app requires different testing techniques, such as E2E testing, integration, components, etc, that may not be considered unit testing at all, which is something several detractors of your post doesn't seem to differentiate correctly, as they seem to extrapolate the term "unit" testing to "any kind of" testing.

TL;DR correct testing depends on context, if you forcibly have to test logic in UI for whatever reason it is, you should go with unit testing, otherwise, you should apply other testing techniques and not to mix them up with what "unit testing" is actually intended for

Collapse
 
polterguy profile image
Thomas Hansen

TL;DR correct testing depends on context, if you forcibly have to test logic in UI for whatever reason it is, you should go with unit testing, otherwise, you should apply other testing techniques and not to mix them up with what "unit testing" is actually intended for

Thank you!

For the record, there are edge cases where unit testing frontend code makes sense. I even give an example myself.