SonarCloud not reading lcov reports and Code coverage not being displayed

We have a project in bit bucket and the sonar cloud is integrated with the bitbucket pipeline. We have microservices architecture and each service has its separate tests and coverage reports. (Coverage generated through the nyc)

folder structure

bitbucket pipeline

options:
  max-time: 40

clone:
  depth: full    # SonarCloud scanner needs the full history to assign issues properly

definitions:
  services:
    mongo:
      image: mongo_replset
    dynamodb:
      image: amazon/dynamodb-local
    redis:
      image: redis
    docker:
      memory: 4096
  caches:
    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
  steps:
    - step: &sonarcloud-analysis
        name: SonarCloud analysis
        caches:
          - sonar
        size: 2x
        services:
          - docker
        script:
          - pipe: sonarsource/sonarcloud-scan:1.4.0
            variables:
              SONAR_TOKEN: ${SONAR_TOKEN}
              DEBUG: ${DEBUG_MODE}
              XTRA_ARGS: '-Dsonar.javascript.lcov.reportPaths=/src/**/lcov.info'
              
pipelines:
  branches:
    '{develop}':
      - step:
          name: Check MongoDB
          size: 2x
          image: mongo_replset
          script:
            - mongo mongodb --eval 'rs.status()'
          services:
            - mongo
      - step:
          name: Build
          size: 2x
          image: node:16
          script:
            - ./test_runner.sh dev10 us-east-2 --service all test-unit || true
            - ./test_runner.sh dev10 us-east-2 print_test_report
          services:
            - dynamodb
            - mongo
            - redis
          artifacts: # defining the artifacts to be passed to each future step.
            - temp.txt
            - message.txt
      - step: *sonarcloud-analysis
  pull-requests:
    '**':
      - step: *sonarcloud-analysis

sonar-project.properties file

sonar.projectKey=<project-key>
sonar.projectName=<project-name>
sonar.projectVersion=1.0

sonar.host.url=https://sonarcloud.io
sonar.organization=<orgnization-name>

sonar.language=js
sonar.sources=src
sonar.sourceEncoding=UTF-8
sonar.exclusions=db/**/*,src/test-utils/**/*, src/test_functions/**/*, src/**/test-manual/*, src/**/package-lock.json
sonar.cpd.exclusions=**/EnumServiceImpl.ts, **/solutions/**, **/src/admin/index.tsx, **/src/index.tsx

sonar.tests=src
sonar.test.inclusions=**/*.spec.ts
sonar.test.exclusions=**/*.stories.tsx
sonar.ts.tslintconfigpath=tslint.json
sonar.junit.reportPaths=reports/junit.xml
sonar.javascript.lcov.reportPaths=src/**/lcov.info
sonar.typescript.tsconfigPath=src/**/tsconfig.json
sonar.coverage.inclusions=src
sonar.scm.exclusions.disabled=true
sonar.coverage.exclusions=src/api/**/*.ts
  1. Do we need to upload the lcov.info file to our bit bucket repo? ( If we write some new code segment and test cases for that code segment and push the changes with the lcov.info file to the branch then we can see the coverage)
  2. When we are merging a PR we run all the test cases. What we tried was to check the coverage at this point without pushing the lcov.info file to the repo. But we’re not getting coverage with this method.
08:52:42.816 DEBUG: Property sonar.javascript.lcov.reportPaths is used.
08:52:42.816 DEBUG: Using 'src/**/lcov.info' to resolve LCOV files
08:52:43.005 INFO: No LCOV files were found using src/**/lcov.info
08:52:43.005 WARN: No coverage information will be saved because all LCOV files cannot be found.
08:52:43.005 INFO: Sensor JavaScript/TypeScript Coverage [javascript] (done) | time=189ms

Hey there.

Your lcov.info file should be regenerated each time you run a build, before SonarCloud analysis runs. It looks like that isn’t the case right now (as there’s no nyc command being run).

You might also find the JavaScript/TypeScript Test Coverage | SonarCloud Docs documentation useful.

Hi,
We’re running the npm run test-unit command in our test_runner.sh script.
Here is a sample of package.json file of one of our micro service.

{
  "name": "<name>",
  "version": "1.0.0",
  "description":  "microservice",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "pre-api-test": "tsc && cd ../../api/ && npm run publish $ENV $REGION generate-html",
    "pretest": "tsc",
    "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha --timeout 20000 -r ts-node/register 'test/**/*.ts' --exit",
    "test-unit": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' nyc mocha --timeout 0 -r ts-node/register 'test/unit/**/*.ts' --exit",
    "test-unit-compile": "cd ../layers/saas-helpers/ && npm run build && cd ../../fees/ && npm run test-unit",
    "test-api": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\", \"esModuleInterop\": true }' mocha --timeout 20000 -r ts-node/register 'test/api/**/*tests.ts' --exit",
    "coverage": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\", \"esModuleInterop\": true }' nyc mocha --timeout 200000 -r ts-node/register 'test/unit/**/*.test.ts' --exit"
  },
  "repository": {
    "type": "git",
    "url": "none"
  },
  "keywords": [
  ],
  "dependencies": {
    "uuid": "^8.3.0",
    "cron-parser": "^4.0.0"
  },
  "devDependencies": {
    "@aws-sdk/client-cloudwatch-events": "^3.27.0",
    "@types/aws-lambda": "^8.10.59",
    "@types/chai": "^4.2.12",
    "@types/chai-as-promised": "^7.1.4",
    "@types/mocha": "^8.0.3",
    "@types/node": "^13.13.15",
    "@types/uuid": "^8.3.4",
    "aws-sdk": "^2.918.0",
    "chai": "^4.2.0",
    "chai-as-promised": "^7.1.1",
    "mocha": "^10.0.0",
    "nyc": "^15.1.0",
    "ts-node": "^8.10.2",
    "typescript": "^4.8.2"
  },
  "nyc": {
    "reporter": [
      "text",
      "lcov",
      "text-summary"
    ],
    "extension": [
      ".ts"
    ],
    "include": [
      "src/**/*.js",
      "src/**/*.ts"
    ],
    "exclude": [
      "node_modules",
      "lib",
      "pkgs"
    ],
    "require": [
      "ts-node/register"
    ],
    "sourceMap": true,
    "instrument": true,
    "check-coverage": false,
    "cache": true
  },
  "dependencies": {
    "cron-parser": "^4.0.0",
    "uuid": "^8.3.0"
  }
}

Hi colin,

Any update on this? Still haven’t be able to resolve the issue.

Hey there.

Thanks for the additional information.

Stepping back a bit – what’s the file path you expect your lcov.info file to be generated at? Have you confirmed that it actually exists (running a cat path/to/lcov.info should help you do that).

Based on your package.json file, it would look like you have something defined to generate the coverage:

Is that actually running in the context of your pipeline and generating the coverage information?