Sonarqube is not displaying code coverage information for Typescript

My Nestjs project is undergoing Sonarqube analysis. Despite passing all unit tests with jest and achieving an 80% code coverage, Sonarqube still displays a 0% coverage.

Here is my configuration in the sonar-project.properties file:

sonar.projectKey=<project-key>
sonar.projectName=
sonar.sources=src
sonar.tests=src
sonar.inclusions=**
sonar.test.inclusions=src/**/*.spec.ts
sonar.testExecutionReportPaths=test-report.xml
sonar.exclusions=node_modules 

The jest.json setup looks like this:

{
"moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "json"
  ],
  "transform": {
      "^.+\\.tsx?$": "ts-jest"
  },
  "testRegex": "/src/.*\\.(test|spec).(ts|tsx|js)$",
  "testCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"],
  "coverageReporters": ["json", "lcov"]
}

Extract from my package.json:

  "devDependencies": {
// List of dependencies
  },
  "jest": {
// Jest configuration details
  }

All source code and respective unit tests are located together, with test files having the .spec.ts extension. The local tests pass successfully and generate a test-report.xml locally. Running unit tests on Jenkins before scanning with Sonar scanner.

Ensuring adequate code coverage is critical for our pipeline to progress. Any suggestions?

Sonarqube Enterprise Edition Version 7.9.5 (build 38598)

Answer №1

Did you attempt to indicate the location of your lcov.info file by using this format:

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

You can find more information about these settings here:

Answer №2

Make sure to include the following line in your jest config:

"coverageReporters": [
      "html",
      "lcov"
    ]

Don't forget to update your sonar file with the line below (if you don't have one, create a new one in the root directory and name it sonar-project.properties):

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

Answer №3

It's hard to say for sure, but the symptoms you're experiencing seem familiar to a situation I encountered with the SonarTS plugin. At that time, we were using an older version of SonarQube (7.9.2). Could you let us know which version you are currently using?

Through some trial and error, I found that if a file path in the "lcov.info" file crosses a symbolic link, the SonarTS plugin fails to access the file, resulting in zero coverage being reported. If one file in the lcov.info file crosses a symbolic link, it's likely that all files will be affected.

To resolve this issue, I created a script that processes the lcov.info file, substituting all file paths with their absolute equivalents while following symbolic links.

The snippet of code within our Jenkins shared library appears as follows:

shterse "cat coverage/lcov.info | " +
   "while IFS= read -r line; do if [[ \"$line\" == \"SF:\"* ]]; then line=\"SF:$(realpath \"${line#SF:}\")\"; fi; echo \"$line\"; done > /tmp/lcov.info"
sh "mv /tmp/lcov.info coverage/lcov.info"

In the above code, "shterse" is defined as:

// This will turn off verbose output.
def shterse(cmd) {
    sh('#!/bin/sh -e\n' + cmd)
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating a skeletal design effect into a table featuring sorting and pagination options

Is there a way to implement the Skeleton effect in a MUI table that only requires sorting and pagination functionalities? I tried using the 'loading' hook with fake data fetching and a 3-second delay, but it doesn't seem to work with DataGri ...

Mastering Angular Apollo Error Resolution Methods

Hey everyone, I'm facing a problem with apollo-angular and apollo-link-error that I just can't seem to figure out. I've experimented with different approaches but have had no luck catching errors on the client-side of my angular web app. Bel ...

Launching the file explorer when the icon is clicked

I am currently working with Angular 5 using Typescript. I need assistance in opening the file explorer window to add an attachment when clicking on an icon. I have successfully done this for a button, but I am facing issues with the click event binding on ...

The argument with type 'void' cannot be assigned to a parameter with type 'Action'

Just getting started with Typescript and using VSCode. Encountering the following Error: *[ts] Argument of type 'void' is not assignable to parameter of type 'Action'. (parameter) action: void Here is the code snippet causing the err ...

What's the best way to utilize the tsconfig.json "types" field within a monorepository setting?

Part 1 - Utilizing the "types" Field When a TypeScript library like library A provides type definitions alongside its normal exports, it looks like this: declare global { function someGlobalFunction(): void; } Library B depends on the type definitions ...

Excessive recursive template causing call stack overflow in tree

By utilizing this particular gist, I am able to effortlessly showcase a tree structure. https://i.sstatic.net/yUNyw.png The aspect I aim to alter is how the first level is presented, as illustrated below: https://i.sstatic.net/Wg7JG.png Despite attempt ...

Why is the Routes module failing to acknowledge the component?

I am in the process of developing my own Portfolio and decided to use Angular 12. Despite following all of the instructions on angular.io, I am facing challenges with Routing. To demonstrate my work more effectively, I have created a Stack Blitz: My Portf ...

Can a custom type be created in Typescript that permits objects but excludes Errors?

Currently working on resolving an odd scenario with a logger. Essentially, calls like log({ info: 'information' }) function as expected, but log(new Error()) falls short. While no solution is readily available, the goal is to override the log m ...

Navigating through a large array list that contains both arrays and objects in Typescript:

I have an array containing arrays of objects, each with at least 10 properties. My goal is to extract and store only the ids of these objects in the same order. Here is the code I have written for this task: Here is the structure of my data: organisationC ...

EmotionJS Component library's Component is not able to receive the Theme prop

I am in the process of developing a component library using Emotion and Typescript. However, I have encountered an issue when trying to import the component into a different project that utilizes EmotionJS and NextJS - it does not recognize the Theme prop. ...

Utilizing a function in an infinite loop within *ngFor along with an asynchronous pipe for an HTTP call

Using a function in an *ngFor statement: @Component({ selector: 'foo-view', template: '<div *ngFor="let foo of loadAll() | async"></div>' }) export class FooComponent { loadAll(): Observable<Foo[]> { return ...

Sometimes, it feels like TypeScript's async await does not actually wait for the task to complete before moving on

Recently, I have been transitioning to using the async await pattern more frequently instead of the traditional Promise syntax because it can help in keeping the code structure cleaner. After some experimentation, I felt like I had a good grasp on how to u ...

The functionality of routerLink is not functioning as expected when used on button elements

My dashboard is designed to display information retrieved from Firebase. However, I am facing an issue with a button labeled as (more) that should redirect me to a specific page when clicked. Unfortunately, the button doesn't seem to be working as int ...

Challenges encountered while retrieving data from Cloud Firestore

Here's the setup I have on my Cloud Firestore: Collection question: - userID - text and Collection user: - name - key I am able to easily retrieve the question data from the database and display it, but currently without the user data. Then, I nee ...

Determining the name of the currently focused DOM element in Angular2

How can I detect the name of a selected element from a group of select elements on a page? For example: <select (click)="functionDetectName()" name="test1"> <select (click)="functionDetectName()" name="test2"> <select (click)="functionDete ...

Typescript library available as a private npm dependency

I have developed a Typescript library that I bundle as an npm module. During the development of my frontend application, I easily integrated this library using yarn link. As I set up GitLab CI for other developers to work on the frontend application, I am ...

Tips for enlarging the font size of a number as the number increases

Utilizing the react-countup library to animate counting up to a specific value. When a user clicks a button, the generated value is 9.57, and through react-counter, it visually increments from 1.00 to 9.57 over time. Here's the code snippet: const { ...

Learn the steps to dynamically show a navbar component upon logging in without the need to refresh the page using Angular 12

After logging in successfully, I want to display a navbar on my landing page. Currently, the navbar only shows up if I reload the entire page after logging in. There must be a better way to achieve this without a full page reload. app.component.html <a ...

Leveraging the power of Angular's Rxjs Observables in conjunction with the n

Currently, I am working on a project where I'm utilizing Angular 7 along with ngrx store to manage the app state. In my components, I am subscribing to the app state in the OnInit method. Within the store, there are multiple variables that can be swap ...

The implementation of user context failed to meet expectations in terms of writing

I need some clarification regarding userContext in react with typescript. Initially, I define it in RubroContext.tsx import { createContext, useContext } from "react"; import { RubroType1, RubroType2 } from "../Interfaces/interfaces"; ...