How to get code coverage in SonarQube report Node.js+SonarQube+Azure DevOps SQ widgets

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)

SonarQube 7.1 Community Edition (running in docker in Azure Web App Linux for containers), In my Azure Build Pipeline I’m using following extension (https://marketplace.visualstudio.com/items?itemName=SonarSource.sonarqube), Node.js.

  • what are you trying to achieve

I want to have code coverage non zero (0) in my SonarQube report and also if it possible I want to have content of junit.xml test result file which generated by jest-junit in my SQ report. In my build pipeline in Azure Devops I’m using cobertura-coverage.xml and junit.xml without any issue both reports works fine.

  • what have you tried so far to achieve this

Honestly I think I tried everything. I spent almost three days without any success.

In run code analysis task I’m using sonar-project.properties file.I tried following and some other parametes combinations of parameters (none of this parameters didnt help me):
sonar.cobertura.reportPath
sonar.javascript.lcov.reportPaths
sonar.coverageReportPaths

In my package.json I have following part:
“scripts”: {
“testci”: “react-scripts test --ci --reporters=jest-junit --reporters=default --coverage --coverageReporters=cobertura --coverageReporters=lcov --coverageReporters=html --transformIgnorePatterns “node_modules/(?!@amcharts)/””,
}

I executing “npm run testci” as part of my pipeline and got following bunch of reports:
photo_2019-08-15_19-01-23
and also client/junit.xml.

Could you please help me to figure out how to get code coverage and test results in SonarQube report.

What happens if you pass the coverage/lcov.info file to sonar.javascript.lcov.reportPaths? Is there anything in your analysis logs about the parsing of coverage reports?

Hello Colin!

Here is my sonar.properties file:

sonar.projectKey=test
sonar.projectName=testkey
sonar.projectVersion=1.0.0

sonar.sources=client/src
sonar.exclusions=client/node_modules/*,client/Docker*,client/init_container.sh,client/**/*.test.*,client /coverage/lcov.info,client/coverage/lcov-report/*

sonar.tests=client/src
sonar.test.inclusions=**/*test*

sonar.dynamicAnalysis=reuseReports
sonar.log.level=DEBUG
sonar.verbose=true

#sonar.cobertura.reportPath=
#sonar.javascript.lcov.reportPaths=client/coverage/lcov.info
#sonar.coverageReportPaths=client/coverage
sonar.javascript.lcov.reportPaths=client/coverage/lcov.info

I checked all logs in build agents and there is nothing regarding sonar.javascript.lcov.reportPaths or client/coverage/lcov.info or both.

Also I checked all logs on server access.log, es.log,sonar.log,web.log. Only in access.log I found one single mention:
172.16.4.1 - - [14/Aug/2019:21:57:14 +0000] "GET /api/settings/values?keys=sonar.javascript.file.suffixes%2Csonar.javascript.globals%2Csonar.javascript.ignoreHeaderComments%2Csonar.javascript.exclusions%2C
sonar.javascript.environments%2Csonar.javascript.jQueryObjectAliases%2Csonar.javascript.lcov.reportPaths&component

The only one setting which was not ignored according logs was sonar.coverageReportPaths but all reports which I set as value was in wrong XML format (according logs)

Any ideas?

Could you still share the logs?
Are JS files analyzed at all? Do you see them with syntax highlighting in SQ UI?

Hi Elena,

13.txt (8.2 KB)

I deleted some sensitive info from log. Both cobertura-coverage.xml files exists.

sonar.projectKey=Promotions_test
sonar.projectName=Promotions_test
sonar.projectVersion=1.0.0

# =====================================================
#   Definition of the modules
# =====================================================

sonar.modules=frontend,backend

frontend.sonar.projectName=Promotions_frontend
backend.sonar.projectName=Promotions_backend

# =====================================================
#   Frontend
# =====================================================

frontend.sonar.projectBaseDir=client

frontend.sonar.sources=src
frontend.sonar.exclusions=**/*test*

frontend.sonar.tests=src
frontend.sonar.test.inclusions=**/*test*
    
# =====================================================
#   Backend
# =====================================================

backend.sonar.projectBaseDir=api

backend.sonar.sources=src
backend.sonar.exclusions=**/*test*

backend.sonar.tests=src
backend.sonar.test.inclusions=**/*test*

# =====================================================
#   Reports
# =====================================================

frontend.sonar.cobertura.reportsPath=coverage/cobertura-coverage.xml
backend.sonar.cobertura.reportsPath=coverage/cobertura-coverage.xml

Why do you think you SonarQube should read Cobertura report? Did you install the community plugin?

Also from the logs I see that there is no a single JS file in your project, only TS. For TS to import coverage natively (without any additional plugins) you should generate coverage report in LCOV format and use property sonar.typescript.lcov.reportPaths

2 Likes

Hello Elena,

typescript.lcov.reportPaths=coverage/lcov.info is working!! Thank you for advice!!

@Lena Been trying to solve the same problem all day, and finally found the solution in your comment.

Can you or someone else from your team please update the Test Coverage & Execution doc for both SonarQube (link) and SonarSource (link) to include sonar.typescript.lcov.reportPaths for TypeScript? It currently lists sonar.javascript.lcov.reportPaths for both JS and TS files, which is highly misleading, and is what I had been using and wondering wth is going on.

Thank you!

Update: Nevermind. Got a warning: WARN: The use of sonar.typescript.lcov.reportPaths for coverage import is deprecated, use sonar.javascript.lcov.reportPaths instead.. Will create a new ticket to figure out why the coverage info is not being reported.