Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Since Jest 25, coverage reports are having a different source path

I'm running an Angular project with an @nrwl/nx setup and Jest for unit tests. I have configured Jest to generate lcov files for each app and lib, which are then picked up by SonarQube Scanner to report the test coverage. Each lib is its own Sonar module.

Recently I updated my Jest version from 24.1.0 to 25.1.0. Since then my coverage in SonarQube is always at 0%, because the scanner is unable to find the files:

WARN: Could not resolve 1 file paths in [/mnt/c/Users/Patrick/Projects/projectname/apps/projectname/../../coverage/apps/projectname/lcov.info], first unresolved path: apps/projectname/src/environments/environment.ts 

I analyzed the lcov files with both version and I noticed that the generated path changed.

Jest 25.1 (does not work)

SF:apps/projectname/src/environments/environment.ts

Jest 24.1 (works)

SF:/mnt/c/Users/Patrick/Projects/projectname/apps/projectname/src/environments/environment.ts

When I change it manually to the following, this also works:

SF:src/environments/environment.ts

But now I'm stuck a little bit, because I did not find a way to tell Jest to generate the path the old way or tell Sonar that the path is now a different one.

like image 243
PaNic175 Avatar asked Feb 20 '20 15:02

PaNic175


2 Answers

I solve it moving the "coverageReporters" configuration from "jest.config.js" at root folder to "jest.config.ts" at project folder, and in lcov configuration I set:

coverageReporters: ['html', ["lcovonly", {"projectRoot": __dirname}], 'text-summary'],

setting the projectRoot to __dirname make the "SF" path relative again.

like image 159
Renan Lira Avatar answered Nov 18 '22 21:11

Renan Lira


I had exactly the same problem! I solved it by executing a script after test execution that will modify the lcov.info by converting the relative paths to absolute ones (process.cwd()). Now the CC in SonarQube is displayed again :)

const project = `name-of-your-project`;
const file = `./${project}/jest/coverage/lcov.info`;

fs.readFile(file, 'utf8', function (err,data) {
  if (err) {
    return console.error(err);
  }
  const result = data.replace(/src/g, `${process.cwd()}/${project}/src`);

  fs.writeFile(file, result, 'utf8', function (err) {
    if (err) return console.error(err);
  });
});
like image 3
fox Avatar answered Nov 18 '22 21:11

fox