Search code examples
angularsonarqubejestjsnrwl-nx

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.


Solution

  • 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.