How to tell SonarQube to get the coverage report from specified project directory

Hi all,

I’m very new to SonarQube and I’ve no experience working on SonarQube application.

In our organization, we are using SonarQube for code quality checks and etc.

I have one project which is already configured with our organization’s SonarQube and I’ve given one task to unit test my application. I’ve unit tested my application and generated the coverage report using an npm package called jest-sonar-reporter

My project is built using React and Django and the project’s structure kinda looks like this.

DjangoApp1/
DjangoApp2/
DjangoProject/
ReactDirectory/
    - build/
    - coverage/
    - node_modules/
    - src/
    - package.json
    - package.lock.json
    - sonarTestResults/
        -  sonarqube-codecoverage-report.xml

So now how can I tell SonarQube to pick the sonarqube-codecoverage-report.xml coverage report which is in ReactDirectory/sonarTestResults/sonarqube-codecoverage-report.xml and show the appropriate metrics result for code coverage for the project in the SonarQube dashboard?

Please, someone, advise/help me with this.

Thank you in advance

Hi,

Welcome to the community!

You’ll need to pass the path-from-project-root as the argument to the correct analysis parameter. The docs on the scanner you’re using should tell you how.

 
HTH,
Ann

Hi @ganncamp, thank you for your response.

Actually, we’re using Jenkins to build the project, I’m really not sure where to give this file path as the analysis parameter.

Thank you

Hi,

Depending on how you’re doing this in Jenkins, you can pass it on the analysis command line or add it in the field for arguments in the analysis step.

 
HTH,
Ann

stage('SonarQubeScan') {
        steps {
            withSonarQubeEnv('SonarQube') {
                withCredentials([string(credentialsId: 'sq_prd_token', variable: 'SONAR_TOKEN')]) {
                    sh "source ~/.bash_profile;which sonar-scanner;sonar-scanner \
                        -Dsonar.sources=$WORKSPACE \
                        -Dsonar.login=$SONAR_TOKEN \
                        -Dsonar.projectKey=$SONAR_PROJECT_KEY\
                        -Dsonar.sonar.javascript.lcov.reportPaths=<path to report>"
                }
            }
        }
    }

This is one stage in Jenkinsfile. in this, I’ve added like sonar.sonar.javascript.lcov.reportPaths=<path to report>

So the path should be the relative or absolute path? I’ve given the relative path and the coverage file is not detecting I feel because in the SonarQube dashboard coverage % is still 0.

Hi,

Your <path to report> is typically going to be relative to project root (which is presumably where analysis is running from). What to share the relevant log lines where it’s not finding your report?

 
Ann

Hi Ann,

sorry for the delayed response! Our organization had not given admin privileges to the project. So once that getting done I’m able to see the administration tab and could relate to what you were trying to say.

But for the react project I have generated the generic code coverage report. This stackoverflow question I have followed to generate the generic coverage report (https://stackoverflow.com/questions/51657821/sonarqube-coverage-configuration-for-react-and-jest)

But in the admin settings, I was not able to find the testExecutionReportPaths field to add the path of the generic code coverage report.

Could you please help me with this?

Hi,

You won’t find that in the interface; it’s only settable via analysis parameters.

 
Ann

Can you please tell me where and how can I set that? an example would definitely help me.

Thanks,
Aashay

Hi Aashay,

You can add this to your analysis parameters (in your Maven pom if you have one or in your sonar-project.properties file) or on the analysis command line with -Dsonar.testExecutionReportPaths=[relative path from project root]

 
HTH,
Ann

Hi Ann,

Thank you for your response. I have created the sonar-project.properties file in the project root. In that, I added the sonar.testExecutionReportPaths=<project-path>/react-coverage-report.xml

Also, I have other properties where everything is working fine, but testExecutionReportPaths isn’t! :frowning:

Someone had already asked the similar question on stackoverflow https://stackoverflow.com/questions/63270803/sonarqube-and-problem-with-reactjs-project-code-coverage?noredirect=1#comment113999784_63270803

Hi,

How are you providing those properties to analysis?

 
Ann