sh33dafi / jest-sonar

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

Sonar-scanner doesn't import test execution data #30

Closed palminha closed 2 years ago

palminha commented 3 years ago

Describe the bug Using "yarn test:coverage" (jest --coverage) i can successfully generate the 'sonar-report.xml' file. Sonnar-scanner parses the file but doesn't import any test results...

INFO: Sensor Generic Test Executions Report
INFO: Parsing /Users/palminha/Development/landing-webapp/backend/coverage/sonar-report.xml
INFO: Imported test execution data for 0 files
INFO: Test execution data ignored for 231 unknown files, including:
frontend/landing_work/dashboard/about_yourself_modal.test.tsx
frontend/components/application_page/application_banner.test.tsx
frontend/curators/curator_page.test.tsx
frontend/components/talentUser/TalentProfile.test.tsx
frontend/components/backoffice/engagement_power_modal/engagement_power_modal.test.tsx

To Reproduce Steps to reproduce the behavior:

  1. Use jest-sonar to generate test files
  2. Run sonnar-scanner to import them

Expected behavior It should properly import the test execution files

Desktop (please complete the following information):

sh33dafi commented 3 years ago

Hello, In the generated sonar report are the paths to the files correct? How is your sonar-project.properties setup? According to me you also have to provide some properties to indicate which tests files need to be included e.g. sonar.test.inclusions property Can you maybe create a small sample project with the minimal setup, so I can have a look at the setup. I think this might be a configuration issue and not an issue with the lib

Maybe also have a look at this issue #25

palminha commented 3 years ago

Hi @sh33dafi i think i found the problem...

This is my project structure

<root_dir>
|-sonar-project.properties
|- <backend>
|     |- jest.config.js
|     |- <frontend>
|     |- <coverage>
|                |-sonar-report.xml
|- <frontend>

The sonar-report.xml have the following content

<testExecutions version="1">
<file path="frontend/landing_work/dashboard/about_yourself_modal.test.tsx">
<testCase name="Rating Container component shallow renders without crashing" duration="7" />

So when sonnar-scanner parses the XML file he thinks the file is relative to _/frontend instead of /backend/frontend_

palminha commented 3 years ago

Tried it with reportedFilePath: 'absolute' and the result is the same:

INFO: Parsing /Users/palminha/Development/landing-webapp/backend/coverage/sonar-report.xml
INFO: Imported test execution data for 0 files
INFO: Test execution data ignored for 232 unknown files, including:
/Users/palminha/Development/landing-webapp/backend/frontend/landing_work/dashboard/about_yourself_modal.test.tsx
/Users/palminha/Development/landing-webapp/backend/frontend/components/application_page/application_banner.test.tsx
/Users/palminha/Development/landing-webapp/backend/frontend/curators/curator_page.test.tsx
/Users/palminha/Development/landing-webapp/backend/frontend/components/backoffice/engagement_power_modal/engagement_power_modal.test.tsx
/Users/palminha/Development/landing-webapp/backend/frontend/components/talentUser/TalentProfile.test.tsx

And I confirm that *.test.tsx are properly configured as test files.... sonar is considering them

INFO: Load active rules (done) | time=3591ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: **/gems/**, **/db/schema.rb, **/javascripts/lib/vendor/*, **/javascripts/lib/vendor/**, **/*.test.tsx
INFO:   Included tests: **/*.test.tsx
INFO: Load project repositories

So, in the end, with everything seeming to be properly configured, I still got the problem...

sh33dafi commented 3 years ago

Ok so the report is taking the location of the jest.config as the root Can you try to use the relativeRootDir property here it should be possible to set a different rootFolder

palminha commented 3 years ago

hi @sh33dafi i configured the relativeRootDir

...
 reporters: ['default', ['jest-sonar', {
    reportedFilePath: 'relative',
    relativeRootDir: '../'
  }]],
...

looking at the sonar-report.xml file it seems ok:

<testExecutions version="1">
<file path="backend/frontend/components/application_page/application_evaluation.test.tsx">
<testCase name="Application Evaluation component with completed test result renders correctly" duration="1" />

When I run sonnar-scanner with the following project configuration

sonar.projectKey=landing
sonar.sources=.
sonar.qualitygate.wait=true
sonar.testExecutionReportPaths=backend/coverage/sonar-report.xml
sonar.javascript.lcov.reportPaths=backend/coverage/lcov.info,frontend/coverage/lcov.info
sonar.test.inclusions=**/*.test.tsx

I get the same message from the scanner saying that ignored 232 unknown files

INFO: Sensor Generic Test Executions Report
INFO: Parsing /Users/palminha/Development/landing-webapp/backend/coverage/sonar-report.xml
INFO: Imported test execution data for 0 files
INFO: Test execution data ignored for 232 unknown files, including:
backend/frontend/components/application_page/application_evaluation.test.tsx
backend/frontend/components/help_center/article_page/article_page.test.tsx
backend/frontend/components/talentUser/mainCard/MainCardContainer.test.tsx
backend/frontend/components/companyInfoForm/CompanyInfoForm.test.tsx
backend/frontend/components/curators/language_dimension.test.tsx
INFO: Sensor Generic Test Executions Report (done) | time=77ms
sh33dafi commented 3 years ago

I tried to reproduce the issue can you check https://github.com/sh33dafi/jest-sonar-sonar-scanner if this mirrors your situation? But I still have the feeling it's more an issue with the sonar configuration

palminha commented 3 years ago

@sh33dafi thanks for the effort... yes, that mirrors my situation. And the effect of sonnar-scanner is identical

...
INFO: Parsing /Users/palminha/Development/jest-sonar-sonar-scanner/backend/coverage/sonar-report.xml
INFO: Imported test execution data for 0 files
INFO: Test execution data ignored for 2 unknown files, including:
../frontend/__test__/test.spec.js
../frontend/__test__/file.spec.js
INFO: Sensor Generic Test Executions Report (done) | time=18ms
...
walterwabe commented 3 years ago

@palminha I'm getting the same problem. Did you find a solution?

palminha commented 3 years ago

Unfortunately no...

I was able to replicate the issue but still no idea where the problem is On Tue, Dec 29, 2020, 20:16 Walter Wabe notifications@github.com wrote:

@palminha https://github.com/palminha I'm getting the same problem. Did you find a solution?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sh33dafi/jest-sonar/issues/30#issuecomment-752227447, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC4XRB3LF6JJWGAOHKXDMDTSXI2KPANCNFSM4TMBYXLA .

palminha commented 3 years ago

@sh33dafi any news on this front?

Ekhozie commented 3 years ago

Hello, had the same issue on my side (report path were good but sonar ignored it), I fixed it by adding this configuration parameter in sonar: sonar.tests=

Like this in my sonar-project.properties

sonar.sources=src
sonar.tests=src
sonar.inclusions=**/*.ts
sonar.test.inclusions=**/*.spec.ts
sonar.testExecutionReportPaths=reports/sonar-report.xml
sonar.javascript.lcov.reportPaths=reports/coverage/lcov.info
sh33dafi commented 3 years ago

Does this help

Hello, had the same issue on my side (report path were good but sonar ignored it), I fixed it by adding this configuration parameter in sonar: sonar.tests=

Like this in my sonar-project.properties

sonar.sources=src
sonar.tests=src
sonar.inclusions=**/*.ts
sonar.test.inclusions=**/*.spec.ts
sonar.testExecutionReportPaths=reports/sonar-report.xml
sonar.javascript.lcov.reportPaths=reports/coverage/lcov.info

@palminha Does this help your case?

angel-git commented 2 years ago

@palminha

had the same issue:

INFO: Parsing /github/workspace/coverage/jest-report.xml
INFO: Imported test execution data for 0 files
INFO: Test execution data ignored for 80 unknown files, including:

in my report file had the following paths:

<file path="../../libs/feature-insights/components/.......

and my sonar config:

sonar.tests=libs

So the paths didnt match, i added the config for relativeRootDir as:

reporters: [
    'default',
    [
      'jest-sonar',
      {
        outputDirectory: 'coverage',
        outputName: 'jest-report.xml',
        reportedFilePath: 'relative',
        relativeRootDir: '<rootDir>/../',
      },
    ],
  ],

And now my report contains proper paths:

<file path="libs/feature-insights/components/....

and sonar finds the files:

INFO: Parsing /github/workspace/coverage/jest-report.xml
INFO: Imported test execution data for 80 files
INFO: Sensor Generic Test Executions Report (done) | time=55ms
palminha commented 2 years ago

@sh33dafi was this fixed by #48?

Wusulijiang13 commented 3 months ago

I fix it with adding prefix of absolute path to prependTestFileName

sonarQubeUnitReporter: {
      sonarQubeVersion: "9.x",
      outputFile: "./coverage/unit-test-result.xml",
      overrideTestDescription: true,
      testFilePattern: ".spec.ts",
      useBrowserName: false,
      prependTestFileName: __dirname
  }