The "Source File" inside the generated lcov reports doesn't match the actual location.
It seems, like ts-jest does not preserve the original file location.
Because of this, my IDE (webstorm) isn't able to visually represent the coverage for files which aren't located at the src root.

The SF paths inside the lcov.info file match the source file location.
➜ jest-ts-lcov-demo git:(master) ✗ TS_JEST_DEBUG="true" npm test
> [email protected] test /Users/doge/Workspace/jest-ts-lcov-demo
> jest
PASS __tests__/application.test.ts
Application
user-util.ts
sumAges(users)
✓ should sum up the ages (7ms)
--------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
user-util.ts | 100 | 100 | 100 | 100 | |
--------------|----------|----------|----------|----------|-------------------|
=============================== Coverage summary ===============================
Statements : 100% ( 3/3 )
Branches : 100% ( 0/0 )
Functions : 100% ( 2/2 )
Lines : 100% ( 2/2 )
================================================================================
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 4.18s
Ran all test suites.
https://github.com/nicolaisueper/jest-ts-lcov-demo
npm testcoverage/lcov.info filecc/ @mischah
I have exactly the same issue in my project.
I tried to run jest on your minimal repo. In coverage/lcov.info, I get :
SF:<...>/jest-ts-lcov-demo/src/user-util.ts
but it should be :
SF:<...>/jest-ts-lcov-demo/src/util/user-util.ts
So far I haven't figured out where this problem comes from. Any idea?
Possibly related: https://github.com/facebook/jest/issues/3753
After further investigation, I came up with a solution.
It appears that the problem comes from your TypeScript configs.
In your tsconfig.json file, simply remove the line "sourceRoot": "./src". It should solve the issue.
Hope it can help 😉
@benoitdemaegdt I've tried your solution in the https://github.com/nicolaisueper/jest-ts-lcov-demo bare repo, as well as in that same repo with updated dependencies, but it doesn't seem to work, the path in the coverage is still wrong:
PASS __tests__/application.test.ts
Application
user-util.ts
sumAges(users)
✓ should sum up the ages (4ms)
--------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
user-util.ts | 100 | 100 | 100 | 100 | |
--------------|----------|----------|----------|----------|-------------------|
=============================== Coverage summary ===============================
Statements : 100% ( 3/3 )
Branches : 100% ( 0/0 )
Functions : 100% ( 2/2 )
Lines : 100% ( 2/2 )
================================================================================
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.229s
Ran all test suites.
_I wanted to upload a screenshot with configs and such but sounds like github upload has issues just right now..._
@benoitdemaegdt was right about the tsconfig.json , but it appears that removing the sourceRoot line isn't enough.
You also have to had "inlineSourceMap": true inside your tsconfig.json. And because sourceMap and inlineSourceMap don't do well together, set the first one to false
According to official doc inlineSourceMap set to true, emit a single file with source maps instead of having a separate file. And unfortunately TS set this config to false by default.
i've tried it in here https://github.com/nicolaisueper/jest-ts-lcov-demo and it works like a charm.
here is the SF line in coverga/lcov.info
SF:/Users/pneveu/Downloads/jest-ts-lcov-demo-master/src/util/user-util.ts
Hope it can help 😉
Well, you're right. It only changes the path in lcov.info :
Before removing "sourceRoot": "./src"
SF:/<...>/jest-ts-lcov-demo/src/user-util.ts
After removing "sourceRoot": "./src"
SF:/<...>/jest-ts-lcov-demo/src/util/user-util.ts
which is the correct path.
However, in one of my other projects, removing "sourceRoot": "./src" also changed the path in the coverage. Unfortunatly I cannot manage to reproduce this in https://github.com/nicolaisueper/jest-ts-lcov-demo. I'll try further attemps !
@benoitdemaegdt oh I did not notice the path was wrong in that way in lcov. I did a lot of investigation and tried many stuff in that repo without success yesterday, I think the bug I linked above has something to do with it.
Anyway, yeah the sourceRoot in tsconfig as per the documentation is only there to provide the root location of the sources for generated source maps, usually not something you want to have in your tsconfig.
here is a screen of the correct path inside the terminal:

All i did was :
sourceMapsrc folder with a console.log() inside. Magic
Thanks @pierreneveu. Yeah it sounds like lcov is building relative paths using the whole set of files, and not using (or wrongly) the path given in sourcemap data nor the root dir of jest.
closing this since it's a lcov bug (if it's a bug), feel free to re-open after testing with latest version if you think it should remain open
This issue is linked very directly from the first google result for:
sonarqube could note resolve file paths typescript
There's a simple portable fix I've put together that I'd like to share:
const path = require('path')
const {readFileSync, writeFileSync} = require('fs')
(() => {
const lcovFile = path.resolve(__dirname, './coverage/lcov.info')
const rawFile = readFileSync(lcovFile, 'utf8')
const rebuiltPaths = rawFile.split('\n').map( singleLine => {
if (singleLine.startsWith('SF:')) {
return singleLine.replace('SF:', `SF:${__dirname}/`)
}
return singleLine
}).join('\n')
console.log(rebuiltPaths)
writeFileSync(lcovFile, rebuiltPaths, 'utf8')
})()
This should sit in the same directory as your package.json and should be run _after_ you've completed your test run with coverage output, but _before_ you report to sonar qube. All this does is take every lcov SF, or Source File, and just blindly prepends the CWD to them. This will always give an absolute path to the files themselves. Obviously a dumb hack, but portable and issue solving without having to make even more sacrifices in project config to appease sonar qube and it's incredibly opaque runtime. As a helping hand, I have it show you the file it's outputting to help with debugging a failure.
Most helpful comment
I have exactly the same issue in my project.
I tried to run jest on your minimal repo. In
coverage/lcov.info, I get :SF:<...>/jest-ts-lcov-demo/src/user-util.tsbut it should be :
SF:<...>/jest-ts-lcov-demo/src/util/user-util.tsSo far I haven't figured out where this problem comes from. Any idea?