Sonar not showing correct Angular project test coverage

Hi George,

There are a number of reasons why your coverage might not be respected, but my suspicion in your case is that the analyzer doesn’t know about the existence of your test files. Even if the tests are mentioned in the lcov.info file, the test files must also be found an indexed by our SonarJS Coverage sensor.

Since Angular likes to co-mingle test and source files together, it’s necessary for such projects to use exclusion/inclusion properties to properly differentiate each and make sure all the right files are found.

I actually created an example exactly to demonstrate this case in one of our public repos. Have a look at sonar-project.properties, particularly these properties:

sonar.sources=src
sonar.tests=src
sonar.exclusions=**/*.spec.ts,**/*test.ts,**/*.js
sonar.test.inclusions=**/*.spec.ts,**/*test.ts
sonar.coverage.exclusions=**/*.js,src/main.ts,src/polyfills.ts,**/*environment*.ts,**/*module.ts

What this is accomplishing is to tell the scanner the sources and tests are all in the same folder, and then to make sure the test files are not seen as regular source files but are seen as tests.

Hope this helps,
Jeff

1 Like