I have a JavaScript app where we generate a code coverage report using Istanbul and use SonarCloud for static analysis.
There are two ways we exclude code from the Istanbul. The first is to set exclusion paths. In jest.config.js
we have this to exclude patterns:
"coveragePathIgnorePatterns": [
"source/legacy"
]
The second way is to use Istanbul ignore comments in source files like /* istanbul ignore file */
. In either case the ignored file will not be part of the generated report file.
In our Sonar configuration we set it to use the generated lcov.info
report file with the sonar.javascript.lcov.reportPaths
property. However we then also need to set sonar.coverage.exclusions
to exclude patterns like source/legacy
because it is not treating the lcov.info
report as the source of truth. This is acceptable but duplicates configuration, which is unfortunate. The real problem is that I cannot find any way to get Sonar to handle the files excluded with /* istanbul ignore file */
.
Is there some way to make Sonar treat the lcov.info
file as its source of truth, such that any file that is not included in the file is excluded from coverage?
Alternately, is there a way with Istanbul where I can make it list ignored files but say that they are ignored? Maybe that way Sonar will see that they are ignored.
For those looking for a solution, you unfortunately need to tell sonarqube to ignore this file explicitly. Not an ideal solution, but what I ended up with.
docs: https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/
I put my filename in
sonar.coverage.exclusions
.