Issue with Coverage Results and Test Reports in SonarQube

Hello,

I’m encountering a problem with my SonarQube results. I’ve used Karma for unit tests in my Angular project and set up a pipeline with GitHub Actions to automate the process. The issue is that the coverage results displayed in SonarQube are different from those in my GitHub Actions output.



Additionally, when I added test reporters to include test results in SonarQube, the coverage and test results are still not being displayed correctly.

Here’s a all about my project setup:

my sonar-project.properties

sonar.projectKey=githubactions-CI-CD
sonar.sources=src
sonar.tests=src
sonar.test.inclusions=**/*.spec.ts
sonar.javascript.lcov.reportPaths=coverage/name-project/lcov.info
sonar.junit.reportPaths=reports/unit-test-results.xml

my karma.conf.ts

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage'),
      require('karma-junit-reporter'), // Added plugin
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      jasmine: {
        // you can add configuration options for Jasmine here
        // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
        // for example, you can disable the random execution with `random: false`
        // or set a specific seed with `seed: 4321`
      },
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    jasmineHtmlReporter: {
      suppressAll: true // removes the duplicated traces
    },
    coverageReporter: {
      dir: require('path').join(__dirname, './coverage/name-project'),
      subdir: '.',
      reporters: [
        { type: 'html' },
        { type: 'text-summary' },
        { type: 'lcov' }
      ]
    },
    junitReporter: {
      outputDir: 'reports', // Directory for the output files
      outputFile: 'unit-test-results.xml', // Name of the output file
      useBrowserName: false // Do not include browser name in the file name
    },
    reporters: ['progress', 'kjhtml', 'junit'], // Added 'junit' to the reporters array
    browsers: ['Chrome'],
    restartOnFileChange: true
  });
};

my githubaction pipline

name: CI/CD Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '16'

      - name: Install dependencies
        run: |
          npm install --force

      - name: Run tests with coverage
        run: npm test 
        continue-on-error: true

      - name: Upload coverage report
        uses: actions/upload-artifact@v3
        with:
          name: coverage
          path: coverage/name-project/lcov.info

      - name: Upload coverage to SonarQube
        uses: sonarsource/sonarqube-scan-action@master
        with:
            args: |
             -Dsonar.javascript.lcov.reportPaths=coverage/name-project/lcov.info
             -Dsonar.junit.reportPaths=reports/unit-test-results.xml
             
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
      
 

the github actions log :

/usr/bin/docker run --name a67a9f980438ab1914cbe97e2d166707b13e1_5dcd22 --label 8a67a9 --workdir /github/workspace --rm -e "SONAR_TOKEN" -e "SONAR_HOST_URL" -e "INPUT_ARGS" -e "INPUT_PROJECTBASEDIR" -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_ACTOR_ID" -e "GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_SUMMARY" -e "GITHUB_STATE" -e "GITHUB_OUTPUT" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_ENVIRONMENT" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e "ACTIONS_RESULTS_URL" -e GITHUB_ACTIONS=true -e CI=true --entrypoint "/entrypoint.sh" -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/frontend/frontend":"/github/workspace" 8a67a9:f980438ab1914cbe97e2d166707b13e1 -Dsonar.javascript.lcov.reportPaths=coverage/name-project/lcov.info
-Dsonar.junit.reportPaths=reports/unit-test-results.xml
INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /github/workspace/sonar-project.properties
INFO: SonarScanner 5.0.1.3006
INFO: Java 17.0.11 Alpine (64-bit)
INFO: Linux 6.5.0-1025-azure amd64
INFO: User cache: /opt/sonar-scanner/.sonar/cache
INFO: Analyzing on SonarQube server 9.9.6.92038
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=134ms
INFO: Server id: 147B411E-AZDVr-EWgpUI_EiE0mLy
INFO: User cache: /opt/sonar-scanner/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=80ms
INFO: Load/download plugins (done) | time=3978ms
INFO: Process project properties
INFO: Process project properties (done) | time=9ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=1ms
INFO: Project key: githubactions-CI-CD
INFO: Base dir: /github/workspace
INFO: Working dir: /github/workspace/.scannerwork
INFO: Load project settings for component key: 'githubactions-CI-CD'
INFO: Load project settings for component key: 'githubactions-CI-CD' (done) | time=57ms
INFO: Auto-configuring with CI 'Github Actions'
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=119ms
INFO: Load active rules
INFO: Load active rules (done) | time=2762ms
INFO: Load analysis cache
INFO: Load analysis cache | time=91ms
INFO: Load project repositories
INFO: Load project repositories (done) | time=77ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: **/*.spec.ts
INFO:   Included tests: **/*.spec.ts
INFO: Some of the project files were automatically excluded because they looked like generated code. Enable debug logging to see which files were excluded. You can disable bundle detection by setting sonar.javascript.detectBundles=false
INFO: 701 files indexed
INFO: 794 files ignored because of inclusion/exclusion patterns
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for css: Sonar way
INFO: Quality profile for js: Sonar way
INFO: Quality profile for json: Sonar way
INFO: Quality profile for ts: Sonar way
INFO: Quality profile for web: Sonar way
INFO: ------------- Run sensors on module githubactions-CI-CD
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=60ms
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=4ms
INFO: Sensor IaC CloudFormation Sensor [iac]
INFO: 0 source files to be analyzed
INFO: 0/0 source files have been analyzed
INFO: Sensor IaC CloudFormation Sensor [iac] (done) | time=141ms
INFO: Sensor IaC Kubernetes Sensor [iac]
INFO: 0 source files to be analyzed
INFO: 0/0 source files have been analyzed
INFO: Sensor IaC Kubernetes Sensor [iac] (done) | time=134ms
INFO: Sensor JavaScript analysis [javascript]
WARN: Node.js version 20 is not recommended, you might experience issues. Please use a recommended version of Node.js [16, 18]
INFO: 35 source files to be analyzed
INFO: 26/35 files analyzed, current file: /github/workspace/src/assets/js/dashboard/dashboard-4.js
INFO: 35/35 source files have been analyzed
INFO: Hit the cache for 0 out of 35
INFO: Miss the cache for 35 out of 35: ANALYSIS_MODE_INELIGIBLE [35/35]
INFO: Sensor JavaScript analysis [javascript] (done) | time=15830ms
INFO: Sensor TypeScript analysis [javascript]
INFO: Resolving TSConfig files using 'tsconfig.json' from property sonar.typescript.tsconfigPaths
INFO: Found 1 TSConfig file(s): [/github/workspace/tsconfig.json]
INFO: Creating TypeScript program
INFO: TypeScript configuration file /github/workspace/tsconfig.json
INFO: 208 source files to be analyzed
INFO: Creating TypeScript program (done) | time=2195ms
INFO: Starting analysis with current program
INFO: Analyzed 208 file(s) with current program
INFO: 208/208 source files have been analyzed
INFO: Hit the cache for 0 out of 208
INFO: Miss the cache for 208 out of 208: ANALYSIS_MODE_INELIGIBLE [208/208]
INFO: Sensor TypeScript analysis [javascript] (done) | time=9574ms
INFO: Sensor CSS Rules [javascript]
INFO: 149 source files to be analyzed
INFO: 149/149 source files have been analyzed
INFO: Hit the cache for 0 out of 0
INFO: Miss the cache for 0 out of 0
INFO: Sensor CSS Rules [javascript] (done) | time=2400ms
INFO: Sensor JavaScript/TypeScript Coverage [javascript]
INFO: Analysing [/github/workspace/coverage/name-project/lcov.info]
INFO: Sensor JavaScript/TypeScript Coverage [javascript] (done) | time=35ms
INFO: Sensor CSS Metrics [javascript]
INFO: Sensor CSS Metrics [javascript] (done) | time=572ms
INFO: Sensor C# Project Type Information [csharp]
INFO: Sensor C# Project Type Information [csharp] (done) | time=1ms
INFO: Sensor C# Analysis Log [csharp]
INFO: Sensor C# Analysis Log [csharp] (done) | time=23ms
INFO: Sensor C# Properties [csharp]
INFO: Sensor C# Properties [csharp] (done) | time=0ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=479ms
INFO: Sensor TextAndSecretsSensor [text]
INFO: 393 source files to be analyzed
INFO: 393/393 source files have been analyzed
INFO: Sensor TextAndSecretsSensor [text] (done) | time=711ms
INFO: Sensor VB.NET Project Type Information [vbnet]
INFO: Sensor VB.NET Project Type Information [vbnet] (done) | time=1ms
INFO: Sensor VB.NET Analysis Log [vbnet]
INFO: Sensor VB.NET Analysis Log [vbnet] (done) | time=13ms
INFO: Sensor VB.NET Properties [vbnet]
INFO: Sensor VB.NET Properties [vbnet] (done) | time=0ms
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=117ms
INFO: ------------- Run sensors on project
INFO: Sensor Analysis Warnings import [csharp]
INFO: Sensor Analysis Warnings import [csharp] (done) | time=1ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=22ms
INFO: CPD Executor 59 files had no CPD blocks
INFO: CPD Executor Calculating CPD for 161 files
INFO: CPD Executor CPD calculation finished (done) | time=154ms
INFO: Analysis report generated in 139ms, dir size=2.6 MB
INFO: Analysis report compressed in 830ms, zip size=1.4 MB
INFO: Analysis report uploaded in 251ms
INFO: ANALYSIS SUCCESSFUL, you can find the results at: ***/dashboard?id=githubactions-CI-CD
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at ***/api/ce/task?id=AZFtFHRffrc002xAIUmn
INFO: Analysis total time: 39.310 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 45.544s
INFO: Final Memory: 17M/67M
INFO: ------------------------------------------------------------------------

Hi,

This dusty-but-still-relevant blog post should help.

JUnit reports are supported for Java, not JavaScript. In fact, we don’t offer test execution reporting for JavaScript at all.

 
HTH,
Ann

Thank you for answering. Does that mean even the number of unit tests will not appear on the dashboard even for spring boot tests ?

Hi,

In a Java project, sonar.junit.reportPaths is supported.

 
HTH,
Ann

ok , thank you very much

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