Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage: Source file path in lcov coverage report isn't correct #542

Closed
nicolaisueper opened this issue May 24, 2018 · 12 comments
Closed
Labels

Comments

@nicolaisueper
Copy link

nicolaisueper commented May 24, 2018

  • Issue

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.

bildschirmfoto 2018-05-24 um 15 23 12

  • Expected behavior

The SF paths inside the lcov.info file match the source file location.

  • Output from your debug log
    [ ~~~You can activate the debug logger by setting the environment variable TS_JEST_DEBUG="true" before running yarn test. The output of the logger
    will be in <your_project_dir>/node_modules/ts-jest/debug.txt~~~ the file didn't exist, so my terminal output ]
➜  jest-ts-lcov-demo git:(master) ✗ TS_JEST_DEBUG="true" npm test

> jest-ts-lcov-demo@0.0.0 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.
  • Link to a minimal repo that reproduces this issue

https://github.com/nicolaisueper/jest-ts-lcov-demo

  • Steps to Reproduce
    • run npm test
    • check the paths inside the coverage/lcov.info file
@nicolaisueper
Copy link
Author

cc/ @mischah

@nicolaisueper nicolaisueper changed the title Coverage: Source file path in lcov report wrong Coverage: Source file path in lcov coverage report isn't correct May 24, 2018
@benoitdemaegdt
Copy link

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?

@huafu huafu added the 🐛 Bug label Aug 4, 2018
@huafu huafu self-assigned this Aug 8, 2018
@huafu
Copy link
Collaborator

huafu commented Aug 8, 2018

Possibly related: jestjs/jest#3753

@huafu huafu removed their assignment Aug 8, 2018
@benoitdemaegdt
Copy link

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 😉

@huafu
Copy link
Collaborator

huafu commented Aug 10, 2018

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

@pierreneveu
Copy link

@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 😉

@benoitdemaegdt
Copy link

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 !

@huafu
Copy link
Collaborator

huafu commented Aug 10, 2018

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

@pierreneveu
Copy link

here is a screen of the correct path inside the terminal:
coverage

All i did was :

  1. removing sourceMap
  2. add an index.ts inside the src folder with a console.log() inside.

Magic

@huafu
Copy link
Collaborator

huafu commented Aug 10, 2018

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.

@huafu
Copy link
Collaborator

huafu commented Sep 19, 2018

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

@huafu huafu closed this as completed Sep 19, 2018
@hweeks
Copy link

hweeks commented Sep 28, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants