Sonarcloud report 0% coverage for things excluded from the report

I’m struggling with a coverage exclusions, please help.
Sonarcloud + typescript project + unit tests in Jest

I can’t exclude specific lines using istanbul annotations: istanbul/ at master · gotwarlost/istanbul · GitHub

Excluded lines DOES NOT appear in a lcov report imported to the sonarcloud, but still, sonarcloud show them as not covered. My investigation lead me here: Executable Lines | SonarQube Docs which is really not a good idea if it can’t be turned off. I want sonar to import a coverage report and do not modify it. In the current state, the coverage analysis is just worthless, as it show things that are excluded from the coverage on purpose.
So, how can I exclude a specific method from the code coverage analysis?

Hey there.

When a coverage report is provided to SonarCloud for a file, it becomes the authority for what lines are considered executable and which aren’t. This comes with one annoying side effect – if a file is excluded in such a way that no data is provided for that file, SoanrCloud will make an (very) educated “guess”.

Are the files you’re excluding methods from still referenced in the coverage report? Are other lines from those files showing as covered in the UI (the ones that are actually covered, if there are any)?

That’s my point - I don’t want sonar to make any guess. I provide the report that should be taken and displayed.
If the file is referenced in a coverage report - in works just fine.
If it’s not, sonar show it as 0% coverage.

The reason Sonar behaves this way might make sense when you learn a bit more about its intention.

Imagine you have a repository with two identically sized backend/frontend project with two different technologies (Java vs. JavaScript).

If a coverage report is provided for the backend code (where 100% of the code is covered) and not the frontend code — what should the coverage be? 100% or 50%.

We think the more accurate representation in 50%. That’s why if there’s no information for a file in the coverage report, we still assume it can be covered.

In your original post, however, you said you’re trying to only exclude specific lines, not files. Are you excluding all the lines of a file?

I understand the intention, but if something like that is introduced, it should be configurable.

Sometimes, I need to exclude all the lines of a file. For example, there is a file containing method that should not be covered. I can’t exclude a whole file because in the future an other method added to the same file, may need to be covered. Also, I want the decission about the exclusion to be close to the code, not in some global configuration in a project’s root or tool’s gui.
Or another example - for some part of the code, I don’t even calculate coverage (configured in jest) and I see those files with 0% coverage, which is not correct.

Please, guide me how to turn it off.

Hi, any update on that?

Hey there.

Since right now, it isn’t possible to turn off – you’ll have to set coverage exclusions that match the exclusions in your file.

Any plan on changing that? My excluions are already over 350 characters, that shouldn’t be like that.