We have a microservice Architecture, multiple UIs, multiple Spring Boot Backends etc. And as a result our UI Projects are very sleek, contains just the frontend logic and consist of maximum 2-3 functional html pages.
Still, we have 100% Code coverage in unittest, and also we test every functional scenario also with Protractor, which are the most important tests for us, since they test the whole flow, and test if the whole application really works. So mostly for a regular UI with 2 pages, we have sth like 40-50 different protractor tests, for different use cases regardigng validations, success/error cases, display options etc.
So we have this debate again and again in team. Is unittesting Angular really worth for us?
My thought: When i compare my UI Code with my Backend code, Angular code is mostly more complex in terms of data flow than a backend. So in backend mostly the methods have input and output parameter, and you can really isolate them while testing. You must of course mock some service calls, but mostly the main task of a method is really imaginable and makes mostly sense also on its own.
In Angular world (or in Javasscript world actually) it seems a bit different. Mostly we have subscriptions, and events and some input and outputs from components and hostlisteners etc. So A main task of a function makes mostly no sense on its own. Mostly you need to check multiple components/services/DOM to see what the code really does at the end.
But other than that, Angular unittesting is for most of us really pain mostly (although we have 3-4 years of experience). Image you have a @ViewChild in some component, and you have to test the function which uses it. You need to simulate the whole DOM in order you have a HTMLElement at the end which you can test. Other than that you have mostly some timing sensitivity, in come cases you must wait for the next angular lifecycle in your tests etc. At the end the unitttest which you write is very technical in comparison to backend tests, and you write your tests mostly against your assumptions/mocks. And you cover with some unittest of a function mostly just a tiny part of your flow.
But most importantly, the whole this code runs in Protractor Tests anyhow. For example, we have a service call, and have 2 functional branches there, success and error case. We test both these cases with our protractor tests from an whole other level. For this simple functional use case you only need 2 protractor tests. But when you want to unittest this exact flow/case, you need mostly tests in X separate places in order to cover the whole flow, and at the end this is still not guaranteed that your code will really work on a browser since you cannot really test the interactions between those multiple modules/components which run together. It is how it sounds: "unittest".
We also cannot use this old argument for unittest like "it is the first tests in your test hierarchy, so until you see your error in a system tests" etc. We have a CI/CD Pipeline which let the both tests run always with every commit.
I want to emphasize that the discussion is not about whether we should write unittest with 0% or 100% coverage. I believe, we should write unittest when we need. For example we have a function which use some regex. There you have really many many cases, which you may not want to cover all seperate with 50 protractor tests (time consuming). But you can really test this with your 50+ unittests, faster, cleaner. and also you can develop "test-driven". So can beautifully write your unittests first, and then your regex.
Also when you have some complex if/else logic; from a user view, regarding UI when there is not any change between some cases which you can distinguish with your protractor, you also have to write unittest.
But all in all, what we currently do, trying to have 100% Angular code coverage is, i believe really an overengineering. At the end our main aim is to guarantee that the code works. If our protractor tests the 90% of the whole application anyway, it sould be sufficient when we just unittest the rest and the logical places selectively.
What do you think?