AriPerkkio / vitest-sonar-reporter

SonarQube reporter for Vitest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

adding ability to change reported root dir file path

AdaskoTheBeAsT opened this issue · comments

Could you please add ability to define root dir file path similar to
https://github.com/sh33dafi/jest-sonar#readme

it is important in monorepos where frontend is not in main folder but in subfolder and sonar is not able to process result file in such case due to invalid file path

Could you provide some more information for this. Maybe an example of file structure, vitest.config.ts and what is expected to be on the report.

ok imagine that solution folder contains subfolder frontend in which ui project is located
image

due to fact that azure devops uses sonarqube/sonarcloud step and those steps works on root folder - reports are not properly consumed when given source files are not accessible by reported path
in that case report looks like this

image

it would be really nice if by using some additional parameter I can define root folder with value ".." which in report it will be changed to

image

and then sonarqube/sonarcloud using that path will properly find source code and consume that test report

Thanks for the additional information. This feature request makes sense. I think I've run into that same exact problem myself couple of years ago with different test runner and reporter integration. Sonarqube can be tricky with relative paths.

To solve this I'm thinking about adding a new configuration option that users could use to modify the path properties. Some users could use this to append additional directories there (like in this one), and some could even use it to remove directories from the path.

In your case the configuration would look something like this:

test: {
    reporters: 'vitest-sonar-reporter',
    sonarReporterOptions: {
        onWriteFilePath(path: string) {
            // path is "apps/ui/src/app/app.spec.tsx"
            return `frontend/${path}`;
        }
    }
}

hi @AriPerkkio
thanks a lot taking a look into this - this will solve the problem with monorepo and sonarqube

in this case I could use

import * as path from 'path';
...

test: {
    reporters: 'vitest-sonar-reporter',
    sonarReporterOptions: {
        onWriteFilePath(reportPath: string) {
            // path is "apps/ui/src/app/app.spec.tsx"
            return path.relative("..", reportPath);
        }
    }
}

Yes, exactly like that.

I'll try to implement this in a week or so.

Looks like support from Vitest's side is needed first: vitest-dev/vitest#5042. Currently custom test.<custom-name> entry is passed to worker threads and it fails to serialize inline functions. With a proper custom reporter options support this will not be a problem.

This is now included in 1.1.0 release.

Thanks a lot!