DEV Community

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

 
joelbonetr profile image
JoelBonetR πŸ₯‡ • Edited

Aha! That's the point I wanted to reach

The problem is, code coverage is a remarkably dumb metric.
In essence, it assumes any test is a good test. It makes no attempt to assess whether you have tested for every scenario.
As a developer, it can be very tempting to assume that good code coverage equates to good testing.

Let’s use a simple example to show what can go wrong.

const product = (a, b) => a * b;

assert product(1, 1) === 1;
Enter fullscreen mode Exit fullscreen mode

Clearly, in the above example, the test achieves 100% code coverage.
However, it isn’t a very good test. For instance, if the operator in product() was changed to divide, the test would still pass! Yet, you have been lulled into a false sense of security because you have 100% code coverage and all your tests pass.

Still, this is better than no tests at all (if the operator changes to sum or subtract it will fail) plus a regression test can simply add an assertion that product(2,3) equals 6 and we should be good from now on.

And that's independent from the context the code resides in either be backend or frontend.
Because following that logic plus the mentioned in the post, if you've a backend that serves a frontend (BFF) why to test the backend in first place then? Simply test the frontend and you'll test the entire backend as well 😁

Thread Thread
 
polterguy profile image
Thomas Hansen • Edited

Not only is coverage a good measurement of unit test quality, but it is also the only measure stick we've got. Is it perfect? No. Is your snippet an example of a test where it misses things? Yes. But it's still for all practical concerns the only measurement we've got to (statically) analyse unit test "quality". You still need to bring your brain to the party as you write unit tests. The best example of that would be your own code, that multiples 1 with 1. If one of my developers did that in code, and told me "I implemented 100% coverage", I would chase the guy out of the building with a stick when I realised what he or she had done ...

Thread Thread
 
joelbonetr profile image
JoelBonetR πŸ₯‡ • Edited

Hahahaha πŸ˜‚ I imagined it and was funny.

Just pointing out that coverage measures test quantity and not quality even it's bringed over the table when talking about code quality. It it would be a test quality measure, a 100% code coverage codebase won't need regressions and we all know that's not true.

That was a dumb example but I've found things not much cleaver than that tbh πŸ˜…

Thread Thread
 
polterguy profile image
Thomas Hansen

coverage measures test quantity and not quality

True, but one speaks about the other, at least to some extent ...

That was a dumb example but I've found things not much cleaver than that tbh

^_^

Thread Thread
 
joelbonetr profile image
JoelBonetR πŸ₯‡

Agree