I have many lines in my code that need to be covered as I nor anyone in my workplace have needed to implement unit tests, so this is a first for me. Some of it is obvious, such as just making sure a component spits out what is expected. However, the rest I can’t understand how I am supposed to cover. Some examples:
The first image is using React Styled Components. Why do these need to be covered by code? How would you go about writing a unit test for styled components?
For the second image, I am just initializing useState or items to watch. How would I create a unit test for initializing a useState?
Would these all be tested under the same file? Like I said I haven’t really done any unit testing before and while I get why it’s done, I don’t get how it’s supposed to be done.
I do not work with react myself but AFAIK Sonar will respect the code coverage reports about what lines should be covered and lack coverage. For example in the case of python, coverage.py has support to declare what code should not be covered as documented here.
We use Go and AFAIK it does not provide such a feature. The way we address it for constants is that we declare them under files named *_const.go. We ban the use of business logic from them, then our sonar properties are configured to not expect coverage from these files.
I have found there are some ways to sort of ignore being scanned, but also hearing it shouldn’t be the route to go and should instead be tested. So I’m sort of stuck on the “how” to test part. Just about every const is being marked, but not all consts are really “testable”.