Sonar not showing correct Angular project test coverage

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube v8.1 and npm sonar-scanner v3.1.0)
  • what are you trying to achieve - Get test report for an Angular project in Sonar
  • what have you tried so far to achieve this:
  • Added sonar-scanner v.3.1.0 to package.json
  • Generate coverage report with angular ng test --code-coverage
  • Verify locally that there is a lcov.info and index.html file, and they contain valid entries
  • Execute npm run sonar from project folder
  • Sonar does not include some folders.

Angular project src folder Structure:
src/app/ -> contains other folders and files
src/other/ -> contains other folders and files
src/util -> contains other folders and files

sonar-project.properties files:
sonar.typescript.lcov.reportPath=coverage/src/app/lcov.info
sonar.sources=src
sonar.exclusions=/node_modules/
sonar.tests=src
…etc

The Sonar instance does not include all files and folders under src/app, but only one folder. The ‘app’ directory has 20+ folders, which contain different angular files. 19 of them are ignored, despite lcov.info showing correct results.

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

Hi @Jeff_Zapotoczny

I noticed you replied here - but turns out I have a similar issue that has appears only since 6th March - raised it here Sonar Typescript Coverage Failing

Even with a minor change with your sonar.exclusions and sonar.coverage.exclusions example - still nothing, yet I am also including the tests through sonar.test.inclusions

One difference is my setup here is multi-module to support a mono-repo approach with Angular.

Hi Jeff,
I’ve kind of the same configuration, as you’ve showed in the example, but the problem was elsewhere.
Long story short - due to PATH inconsistency, Jenkins could not assign the correct PATH to the appropriate Node version, and used something archaic.
Node 10.19 fixed it for me.

Great! Glad it’s sorted out.

A post was split to a new topic: Angular coverage with Jest