Sonar cloud does not pick up from the code coverage

Hi team,
We are using Jest unit results publishing Cobetura.xml, which shows the Azure pipeline extension, This code coverage needs to show Sonar Cloud, but sonar does not pick it. Please help

YMAL

  • task: Npm@1
    displayName: npm install
    inputs:
    command: “custom”
    customCommand: “install --force”
    enabled: true

  • task: Npm@1
    displayName: Unit Test
    inputs:
    command: ‘custom’
    customCommand: ‘run test:unitcoverage’

  • task: PublishTestResults@2
    displayName: ‘Publish Test Results’
    inputs:
    testRunner: JUnit
    testResultsFiles: ‘**/junit.xml’

  • task: PublishCodeCoverageResults@1
    displayName: Publish coverage
    inputs:
    codeCoverageTool: ‘Cobertura’
    summaryFileLocation: ‘$(System.DefaultWorkingDirectory)**/coverage/cobertura-coverage.xml’
    pathToSources: ‘$(System.DefaultWorkingDirectory)/src’
    reportDirectory: ‘$(System.DefaultWorkingDirectory)/coverage/cobertura’
    enabled: true

  • task: SonarCloudPrepare@1
    inputs:
    SonarCloud: ‘xx’
    organization: ‘xx’
    scannerMode: ‘CLI’
    configMode: ‘file’
    configFile: ‘$(Build.SourcesDirectory)/sonar-project.properties’
    enabled: true

  • task: SonarCloudAnalyze@1
    displayName: Run Sonar analysis
    enabled: true

  • task: SonarCloudPublish@1
    displayName: Publish quality gate
    inputs:
    pollingTimeoutSec: ‘300’
    enabled: true

sonar-project.properties

sonar.projectKey=xx

sonar.projectName=xx

sonar.sourceEncoding=UTF-8

sonar.sources=src

sonar.exclusions=/index.js,//index.js,/*.test.js

sonar.token=xxxx

sonar.cobertura.reportPaths=./coverage-cobertura.xml

sonar.javascript.lcov.reportPaths=./coverage/cobertura/lcov.info

Package.json

},
“scripts”: {
“start”: “react-scripts start”,
“build”: “react-scripts build”,
“test”: “react-scripts test”,
“test:unitcoverage”: “react-scripts test --env=jsdom --watch --coverage --watchAll=false --coverageReporters=cobertura --coverageReporters=html --reporters=default --reporters=jest-junit”,
“eject”: “react-scripts eject”,
“coverage”: “jest --coverage --coverageReporters=cobertura”
},

Please suggest any configuration changes or any other changes need.

Hi @nagarjuna183

Can you please share the sonar-scanner logs? Feel free to redact any sensitive information. I’m more interested in seeing any error messages related to the coverage file.

Regards,
Ismail

INFO: No LCOV files were found using ./coverage/cobertura/lcov.info

WARN: No coverage information will be saved because all LCOV files cannot be found.

INFO: Sensor JavaScript/TypeScript Coverage [javascript] (done) | time=1939ms

INFO: Sensor CSS Metrics [javascript]

INFO: Sensor CSS Metrics [javascript] (done) | time=1618ms

INFO: Sensor TextAndSecretsSensor [text]

INFO: 33 source files to be analyzed

INFO: 33/33 source files have been analyzed

INFO: Sensor TextAndSecretsSensor [text] (done) | time=8240ms

INFO: Sensor IaC Docker Sensor [iac]

INFO: 0 source files to be analyzed

INFO: 0/0 source files have been analyzed

INFO: Sensor IaC Docker Sensor [iac] (done) | time=108ms

INFO: Sensor Serverless configuration file sensor [security]

INFO: 0 Serverless function entries were found in the project

INFO: 0 Serverless function handlers were kept as entrypoints

INFO: Sensor Serverless configuration file sensor [security] (done) | time=5ms

INFO: Sensor AWS SAM template file sensor [security]

INFO: Sensor AWS SAM template file sensor [security] (done) | time=1ms

INFO: Sensor AWS SAM Inline template file sensor [security]

INFO: Sensor AWS SAM Inline template file sensor [security] (done) | time=1ms

INFO: Sensor javabugs [dbd]

As per the logs, the LCOV files were not found. A common cause would be that the file is not cached between different CI steps. In other words, the step that generated the file didn’t store it or make it available in the scan step.

Or there is something wrong with the generation step and the file is not generated to begin with.

Regards,
Ismail

Thanks Cherri,

Task: PublishCodeCoverageResults@1 this step, i can see the line coverage in azure pipeline extension
Then should i store that file and where if yes please suggest that azure pipeline which step(Task) need to add?

Hi,

It should be available to the task that runs the analysis
I guess this one:

task: SonarCloudAnalyze@1
displayName: Run Sonar analysis
enabled: true

Can check in the logs showing the default jacoco is this reason?

LOGS:

Load metrics repository

INFO: Load metrics repository (done) | time=110ms

INFO: Sensor cache enabled

INFO: Load sensor cache

INFO: Load sensor cache (94 KB) | time=750ms

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=2ms

INFO: Sensor JavaScript/TypeScript analysis [javascript]

INFO: Detected os: Linux arch: amd64 alpine: false. Platform: LINUX_X64

INFO: Using Node.js executable: ‘node’.

Hi,

Do you use JaCoCo coverage in your project?

No we have cobetura

For Java, please check the documentation

It is the same principle as for LCOV file, you need the coverage file to be available during the scan step.

Regards,
Ismail

We are using the React JS language. Should we install the LCOV tool?
Also, please suggest any changes to the package.json?

Hi,
It doesn’t matter what tool you use to generate the lcov.info file, the file has just to be available to the sonar scanner during the scan step.

Regards,
Ismail Cherri

Thanks, Ismail Cherri.

I got code coverage results in the SonarCloud dashboard.
image

1 Like

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