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.

I am trying to get the test execution report on sonarqube the coverage is successfull but not the unit test.
My sonarqube is running inside docker container. I have generated the test execution data using “mocha-sonarqube-reporter” and recived the data in xml format.
used nyc for coverage report.
the sonarqube cmd is as follows:

The sonar logs are as follows:
INFO: Scanner configuration file: /var/jenkins_home/tools/hudson.plugins.sonar.SonarRunnerInstallation/extensure-sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.5.0.2216
INFO: Java 1.8.0_272 AdoptOpenJDK (64-bit)
INFO: Linux 4.19.76-linuxkit amd64
INFO: User cache: /root/.sonar/cache
INFO: Scanner configuration file: /var/jenkins_home/tools/hudson.plugins.sonar.SonarRunnerInstallation/extensure-sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: Analyzing on SonarQube server 8.4.1
INFO: Default locale: “en_US”, source code encoding: “UTF-8” (analysis is platform dependent)
WARN: SonarScanner will require Java 11 to run starting in SonarQube 8.x
INFO: Load global settings
INFO: Load global settings (done) | time=64ms
INFO: Server id: BF41A1F2-AXZHlvEOpdl3jkIdcHbq
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=35ms
INFO: Load/download plugins (done) | time=62ms
INFO: Process project properties
INFO: Process project properties (done) | time=7ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=3ms
INFO: Project key: Sonar_Extensure_test_11
INFO: Base dir: /var/jenkins_home/workspace/sonar_result_test
INFO: Working dir: /var/jenkins_home/workspace/sonar_result_test/.scannerwork
INFO: Load project settings for component key: ‘Sonar_Extensure_test_11’
INFO: Load project settings for component key: ‘Sonar_Extensure_test_11’ (done) | time=19ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=52ms
INFO: Auto-configuring with CI ‘Jenkins’
INFO: Load active rules
INFO: Load active rules (done) | time=985ms
INFO: Indexing files…
INFO: Project configuration:
INFO: 41 files indexed
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for ts: Sonar way
INFO: ------------- Run sensors on module Sonar_Extensure_test_11
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=21ms
INFO: Sensor SonarCSS Rules [cssfamily]
INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
INFO: Sensor SonarCSS Rules [cssfamily] (done) | time=1ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: ‘sonar.coverage.jacoco.xmlReportPaths’ is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=3ms
INFO: Sensor SonarJS Coverage [javascript]
INFO: Analysing [/var/jenkins_home/workspace/sonar_result_test/./coverage/lcov.info]
INFO: Load project repositories
INFO: Load project repositories (done) | time=18ms
WARN: Could not resolve 3 file paths in [/var/jenkins_home/workspace/sonar_result_test/./coverage/lcov.info]
WARN: First unresolved path: coverage/lcov-report/block-navigation.js (Run in DEBUG mode to get full list of unresolved paths)
INFO: Sensor SonarJS Coverage [javascript] (done) | time=76ms
INFO: Sensor TypeScript analysis [javascript]
INFO: Using TypeScript at: ‘/var/jenkins_home/workspace/sonar_result_test/node_modules’
INFO: Found 1 tsconfig.json file(s): [/var/jenkins_home/workspace/sonar_result_test/tsconfig.json]
INFO: 40 source files to be analyzed
INFO: Analyzing 40 files using tsconfig: /var/jenkins_home/workspace/sonar_result_test/tsconfig.json
WARN: You are using version of TypeScript 3.9.9 which is not officially supported; supported versions >=3.2.1 <3.8.0
INFO: 40/40 source files have been analyzed
INFO: Sensor TypeScript analysis [javascript] (done) | time=5156ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=1ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=2ms
INFO: Sensor SonarTS [typescript]
INFO: Since SonarTS v2.0, TypeScript analysis is performed by SonarJS analyzer v6.0 or later. No TypeScript analysis is performed by SonarTS.
INFO: Sensor SonarTS [typescript] (done) | time=0ms
INFO: Sensor Generic Test Executions Report
INFO: Parsing /var/jenkins_home/workspace/sonar_result_test/xunit.xml
INFO: Imported test execution data for 0 files
INFO: Test execution data ignored for 9 unknown files, including:
/var/jenkins_home/workspace/sonar_result_test/lib/tests/bots/dialogAndWelcomeBot.test.js
/var/jenkins_home/workspace/sonar_result_test/lib/tests/dialogs/bookingDialog.test.js
/var/jenkins_home/workspace/sonar_result_test/lib/tests/dialogs/cancelAndHelpDialog.test.js
/var/jenkins_home/workspace/sonar_result_test/lib/tests/dialogs/dateResolverDialog.test.js
/var/jenkins_home/workspace/sonar_result_test/lib/tests/dialogs/emailValidator.test.js
INFO: Sensor Generic Test Executions Report (done) | time=43ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=4ms
INFO: CPD Executor 3 files had no CPD blocks
INFO: CPD Executor Calculating CPD for 37 files
INFO: CPD Executor CPD calculation finished (done) | time=32ms
INFO: Analysis report generated in 62ms, dir size=286 KB
INFO: Analysis report compressed in 88ms, zip size=131 KB
INFO: Analysis report uploaded in 22ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://192.168.157.161:9000/dashboard?id=Sonar_Extensure_test_11
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://192.168.157.161:9000/api/ce/task?id=AXjGF56xh6OZzIOB-uWs
INFO: Analysis total time: 8.455 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 9.134s
INFO: Final Memory: 17M/253M
INFO: ------------------------------------------------------------------------

Not able to understand why this is happening:
INFO: Test execution data ignored for 9 unknown files, including:
/var/jenkins_home/workspace/sonar_result_test/lib/tests/bots/dialogAndWelcomeBot.test.js
/var/jenkins_home/workspace/sonar_result_test/lib/tests/dialogs/bookingDialog.test.js
/var/jenkins_home/workspace/sonar_result_test/lib/tests/dialogs/cancelAndHelpDialog.test.js
/var/jenkins_home/workspace/sonar_result_test/lib/tests/dialogs/dateResolverDialog.test.js
/var/jenkins_home/workspace/sonar_result_test/lib/tests/dialogs/emailValidator.test.js

The coverage is visible but the unit test are not…

How do i get the unit test result on sonarqube
Please let me know what is missing here

Because those test.js files are not present at location specific in xunit.xml file which you have passed as testExecutionReportPaths. I am also facing same challenge with AzureDevOps. Don’t know how to fix it.

@Rahul_Mahulkar
I also face this error ( get a dash symbol in Unit Tests)
The scanner run result is below

INFO: Test execution data ignored for 1 unknown files, including:
test/index.test.ts

I am sure the ignored file (test/index.test.ts) is in my folder

ps. the sonar-project.properties is

sonar.sources=src
sonor.tests=test
sonar.test.inclusions=**/*.test.ts

ps2. the xunit.xml is

<testExecutions version="1">
<file path="test/index.test.ts">
<testCase name="test1" duration="88"/>
<testCase name="test2" duration="102"/>
</file>
</testExecutions>

Scanner runs from specific working directory. Let’s assume it’s being executed from D:\BuildDirectory\source\main. Let’s assume index.test.ts is located at path D:\BuildDirectory\source\main\somemodule\src\test\index.text.ts. In this case based on xunit.xml you have specified scanner expects index.test.ts to be available at D:\BuildDirectory\source\main\test\index.test.ts and it fails to find the file. This results into following

INFO: Test execution data ignored for 1 unknown files, including:
test/index.test.ts

In my case I have written powershell script to read and update xunit.xml to reflect correct relative path. In the example I have given it should be as follow

<testExecutions version="1">
<file path="somemodule/src/test/index.text.ts">
<testCase name="test1" duration="88"/>
<testCase name="test2" duration="102"/>
</file>
</testExecutions>

Please note you can also control value of path in file tag above by setting sonor.tests=somemodule/src/test in stead of sonor.tests=test.

What are u using to get this kind xunit.xml file? My xunit fails is missing row, instead it has