Angular coverage with Jest

First of all, thank you for the repo reference, that was useful.
I have one more issue with my Angular application coverage generated by Jest.

I am using Jest to generate test coverage: jest --coverage.

Project structure:

angular-app
    coverage
        lcov.info
    src
        app
            app.component.ts
            app.component.html
    sonar-project.properties

lcov.info has following paths:

SF:src\app\app.component.html
SF:src\app\app.component.ts

My CI (TeamCity) gives me this error:

...
[16:28:07][Step 4/4] INFO: Base dir: /mnt/ramdisk/agents/agent16/work/dbe8ad65d6f5738a
[16:28:07][Step 4/4] INFO: Working dir: /mnt/ramdisk/agents/agent16/work/dbe8ad65d6f5738a/.scannerwork
...
[Step 4/4] INFO: Analysing [/mnt/ramdisk/agents/agent16/work/dbe8ad65d6f5738a/./coverage/lcov.info]
[Step 4/4] INFO: Sensor SonarJS Coverage [javascript] (done) | time=158ms
[16:28:11][Step 4/4] WARN: Could not resolve 103 file paths in [/mnt/ramdisk/agents/agent16/work/dbe8ad65d6f5738a/./coverage/lcov.info], first unresolved path: src/app/app.component.html
...

Should the path in the lcov be relative to current directory?
I suppose working directory is angular-app and the lcov.info is in coverage directory - may this be the error? How to handle this case?

sonar-project.properties:

sonar.projectVersion=1.0

sonar.sources=src
sonar.tests=src
sonar.exclusions=dist/**/*,node_modules/**/*,**/*.html,**/*.css,**/*.scss,**/*.spec.ts,**/*test.ts,**/*.js
sonar.coverage.exclusions=**/*.js,src/main.ts,src/polyfills.ts,**/*environment*.ts,**/*module.ts
sonar.test.inclusions=**/*.spec.ts
sonar.javascript.lcov.reportPaths=./coverage/lcov.info

sonar.sourceEncoding=UTF-8

# https://www.npmjs.com/package/jest-sonar-reporter
sonar.testExecutionReportPaths=coverage/test-reporter.xml

Hi Felix, welcome to the SonarSource Community!

Administrative note: I moved your post to be its own fresh topic since you replied to a year-old conversation with a slightly different angle (the involvement of jest).

Comparing your lcov.info contents with the one generated in my example Angular project, the clear difference to me is relative versus absolute paths. I don’t know jest; is there an option to have it generate absolute paths instead? Give that a shot.

1 Like

Hi Jeff, thank you for answer.

I have found this: Configuring Jest · Jest

Used following values:

"coverageReporters": ["text", "text-summary","json", ["lcov", {"projectRoot": "/"}]],

Still getting errors, probably due to: Since Jest 25, coverage reports are having a different source path

I am still trying to fix it. I will post an update here once I find one :slight_smile:

This is just a backreference: Sonar not showing correct Angular project test coverage - #2 by Jeff_Zapotoczny