sh33dafi / jest-sonar

A jest reporter that generates sonar reports
MIT License
50 stars 11 forks source link

In a monorepo (NPM workspace), file path is not relative to root directory #57

Closed SchulteMarkus closed 2 years ago

SchulteMarkus commented 2 years ago

In my project I am using NPM workspaces for having a monorepo.

root/
  packages/
    subdir1/
      __test__/
      src/
    subdir2/
      __test__/
      src/

My testfiles are in the "test" directories.

The Sonar Generic Test Coverage Report Format requires:

Insert a file element for each file which can be covered by tests. Its path attribute can be either absolute or relative to the root of the module.

In this case, as I have two sub-packages, two sonar-report.xml files are being created.

But in this case, the path for "file" is not relative to the root directory (or absolute) as required by SonarQube. For this reason, SonarQube does not "understand" the sonar-reports.

<testExecutions version="1">
  <file path="__test__/someSpec.spec.ts"> <!-- Should be: <file path="packages/subdir1/__test__/someSpec.spec.ts"> -->
    <testCase name="A name" duration="6" />

Change request

Please let file-path be relative to the root-directory in a monorepo, too.

sh33dafi commented 2 years ago

Hey @SchulteMarkus can you provide a sample project for me to test with? That makes it easier for me to verify the fix, thanks

SchulteMarkus commented 2 years ago

@sh33dafi https://github.com/SchulteMarkus/jest-sonar-57

sh33dafi commented 2 years ago

@sh33dafi https://github.com/SchulteMarkus/jest-sonar-57

Thanks I will have a look

sh33dafi commented 2 years ago

Hey @SchulteMarkus sorry it took a while to get back to you but I investigated the issue, the issue won't be fixed by using since jest will see the sub-package as the root dir (since jest is run from that dir) But you can use the relativeRootDir property to get a correct path in your report

If forked your example and updated the config you can check it here

The output is now

`

and

`

I hope this resolves your issue

SchulteMarkus commented 2 years ago

Thanks for your feedback @sh33dafi , I have given it a try - it works!

github-actions[bot] commented 1 year ago

Locked due to an inactive of 90 days