What format does Sonar Cloud code coverage accept for Node.Js project?

I’m working on a frontend project, in Node.js
with continuous integration on Azure DevOps, and with a custom agent, just with windows server.

My problem is that I can’t generate the coverage report but the unit tests do.

I manage to run the sonar scanner in my pipeline, and the result is displayed fine in Sonar Cloud, but I am missing the coverage.

How could I do it?

Which framework should I use, because I have tried a lot of them:

        "karma": "^3.1.4",
        "karma-coverage": "^2.0.3",
        "karma-coverage-istanbul-reporter": "^3.0.3",
        "karma-jasmine": "~1.1.2",
        "karma-jasmine-html-reporter": "^0.2.2",
        "karma-junit-reporter": "^1.2.0",
        "puppeteer": "^10.0.0",

My problem is that my local coverage report is at 0

For those who will have the same problem here is the solution I found:
sonarCloud accepts type: xml code coverage file, with the tool cobertura.
Like code-coverage.xml output.

I had to use the frameworks from my first message, defined in my package.json

1. So I use the following tasks on my scan pipeline:

          - script: |
              npx ng test --code-coverage --progress --karma-config src/karma-ci.conf.js --prod
            displayName: Generate unit tests and report coverage

This task can be replaced by this one below.

          - task: Npm@1
              command: custom
              customCommand: run test:ci
            displayName: Launch unit tests

But you will need to define in your package.json the script that can call the Npm@1 command custom run test:ci. By adding this to the script:

 "scripts": {
        "test:ci": "ng test --watch=false --karma-config src/karma-ci.conf.js",

2. And the other tasks of my pipline for publish unit tests and run analysis, and publish quality gate.

          - task: PublishTestResults@2
            condition: succeededOrFailed()
              testResultsFormat: "JUnit"
              testResultsFiles: "**/TestResults/report-junit/unit-test-results.xml"
              searchFolder: $(System.DefaultWorkingDirectory)
              failTaskOnFailedTests: false
            displayName: Publish unit test results

          - task: PublishCodeCoverageResults@1
            condition: succeededOrFailed()
              codeCoverageTool: Cobertura
              summaryFileLocation: "**/TestResults/report-cobertura/code-coverage.xml"
              failIfCoverageEmpty: true
            displayName: Publish code coverage report

          - task: SonarCloudAnalyze@1
            displayName: "Run Code Analysis"

          - task: SonarCloudPublish@1
            displayName: "Publish Quality Gate Result"

2. The final result


I still have a doubt about the result of code coverage that
my report shows on my last capture 0.0% <85.0%.

It is not consistent to get 0.0% if my code coverage of my 2nd capture shows the following percentage:

  • 33% of line covered and
  • 29% branch coverage

So I wonder if my report has been sent to sonarCloud, can you confirm it or explain it to me or am I wrong?
if you need my pipeline logs let me know.

Hello Jérémie,

First thing first, I invite you to read SonarQube documentation about importing coverage data.

As you will see, you can either use SonarQube generic format for test coverage or the LCOV format supported by the JavaScript/TypeScript analyzer, which is quite common in the JavaScript community. For the latter, it’s not really my place to tell you which test framework you should use, but any framework that generates a .lcov file will do, e.g. Jest developed by Facebook seems to be pretty popular.

In any case, in order to import your code coverage report in SonarQube, your pipeline will need to run your unit tests and generate the coverage report before running an analysis as the scanner won’t do it for you. Furthermore, you will need to tell the scanner where to find the coverage report file by setting the appropriate property, e.g. sonar.javascript.lcov.reportPaths for .lcov files.

Hope this helps,

Thank you for your reply Yassine,the problem was with the format of my code coverage of my sonar-project.properties file.

you had to define the properties like this:

# Path to sources
# Path to report coverage and unit tests
sonar.javascript.lcov.reportPaths= TestResults/report-lcov/lcov.info
sonar.junit.reportsPath= TestResults/report-junit/unit-tests-results.xml
# Log

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.