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

See original GitHub issue
  • 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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:12

github_iconTop GitHub Comments

8reactions
hweekscommented, 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.

2reactions
benoitdemaegdtcommented, Aug 1, 2018

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Coverage issues for Typescript (LCOV Could not resolve file ...
The error message is: WARN: Could not resolve xx file paths in [coverage/lcov.info], first unresolved path:
Read more >
Sonar Coverage: file path in lcov coverage report isn't correct
1 Answer 1 · You need to make sure a coverage report is generated for the test. So if you are running something...
Read more >
Coverage reporter is asking for `lcov.info` file if `coverage` is ...
I'm using Intellij Idea + karma plugin to run karma tests. Namely, I want to run test coverage. The issue is that when...
Read more >
Ubuntu Manpage: lcovrc - lcov configuration file
The lcovrc file contains configuration information for the lcov code coverage tool (see lcov(1)). The system-wide configuration file is located at ...
Read more >
Command line usage — Coverage.py 6.4.4 documentation
annotate – Annotate source files with coverage results. ... -m, --module <pyfile> is an importable Python module, not a script path, to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found