Code Coverage not reflecting in Sonar Cloud

We have a React app created using Create React App and have configured it according to sonar cloud documentation but code coverage is still not getting reported. The unit test coverage is being displayed on developers local systems and we are using the jest-sonar-reporter to properly format test output. We are using Github and Github Actions for CI platform and that is all configured to run the unit tests and then the sonar scan.

Please see our repo for project and sonar configuration and CI workflow

1 Like

Hello Jason!

And welcome to the community!

First I see that you are using jest-sonar-reporter, this is third party module that is not needed. The expect format of coverage files is lcov which Jest is able to generate by itself. So you just need to make sure lcov is part of your coverageReporters Jest setting.

Then you should make sure that you have in your sonar-project.properties the sonar.javascript.lcov.reportPaths setting that point to your coverage report. You can also specify in Jest where the coverage report is generated with the coverageDirectory setting.

So as long as your lcov file is correctly generated and available at the place specified in the sonar.javascript.lcov.reportPaths settings it should work fine.

Hi Gregoire,
Thanks a lot for your valuable feedback.
Can you please give us an example of a build.yml GitHub action file when using SonarQube & Jest?

Hello Essam,

Sure here is an example of yml file for a github action with jest and coverage working:

name: Build
on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  sonarcloud:
    name: SonarCloud
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
      - name: Install dependencies
        run: yarn
      - name: Test and coverage
        run: yarn test-ci
      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        with:
          args: >
            -Dsonar.organization=myOrg
            -Dsonar.projectKey=myProjectKey
            -Dsonar.sources=src
            -Dsonar.tests=src
            -Dsonar.test.inclusions=src/**/__tests__/**
            -Dsonar.exclusions=src/**/__tests__/**
            -Dsonar.javascript.lcov.reportPaths=./coverage/lcov.info
            -Dsonar.coverage.exclusions=__mocks__/**,.storybook/**,src/stories/**,src/types/**
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets. SONAR_TOKEN }}