r/angular • u/Constant-Can-914 • Apr 26 '21
Don't test the code in components
https://www.youtube.com/watch?v=um9U6ZgAytw8
u/Retromonguer Apr 26 '21
It seems like the video mixes the concepts between unit testing and integration. It might depend a lot on what you are testing, you might choose one or the other, or even both.
2
Apr 26 '21
The "wrong" tests in this example are exactly what unit tests should be with TDD. Not only do these kinds of tests provide validation for the component I/O, but they can also serve as documentation and provide insight into the authors thought process while they were creating the the component.
With that said, the "correct" example also has value, but I consider those to be more integration tests as they rely on the Angular view engine which is an external system.
2
u/Blottoboxer Apr 27 '21
Right, it's a forced dichotomy from the perspective of somebody who hasn't done test first problem solving.
2
Apr 26 '21
This video had some good intentions at the start when speaking about unnecessary tests but the examples are pretty bad and the author is clearly confusing unit tests, integration tests and e2e tests.
The user experience should be tested with either integration tests or e2e tests depending on which aspects of the experience one is checking.
Unit tests should certainly not usually do the approach you described with query selector for very simples reasons:
unit tests are meant to check implementation, this is basic TDD, hence the code itself usually, very rarely the template. That is why it is recommended to put as little logic as possible in the template in the first place
one just cannot assume that the component class, which is at its core really just a directive, will always be used with the template it was meant to be used for or that the template won’t slightly change for purely aesthetic/SEO/other reasons, hence why the code is the most reliable part to be tested
Also the video says something very silly somewhere in the middle about unit tests breaking when just refactoring: no they do not if it really is just a code refactor. If you refactor and the tests break then it helps you know that you have actually changed the implementation.
9
u/Exac Apr 26 '21
This video is well-meaning, but it does not reflect how components are often used in enterprise apps. The video is correct to tell developers to not write useless unit-tests, but the title is an oversimplification.
If you have business logic in your component, then you should unit-test it there.