Skip to content

Commit

Permalink
feat: support onWritePath option
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Feb 17, 2024
1 parent 7159d2e commit 3ebe0f1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,36 @@ test: {
}
```

#### `onWritePath`

Rewrite `path` attribute of `<file>`. This can be useful when you need to change relative paths of the files.

```ts
test: {
reporters: [
['vitest-sonar-reporter', {
onWritePath(path: string) {
// Prefix all paths with root directory
// e.g. '<file path="test/math.ts">' to '<file path="frontend/test/math.ts">'
return `frontend/${path}`;
}
}]
],
}
```

#### `outputFile`

Location for the report.

```ts
test: {
reporters: [
['vitest-sonar-reporter', { outputFile: 'sonar-report.xml' }]
],
}
```

## Code Coverage

This reporter does not process code coverage - Vitest already supports that out-of-the-box!
Expand Down
10 changes: 9 additions & 1 deletion src/sonar-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { generateXml } from './xml.js';
export interface SonarReporterOptions {
outputFile: string;
silent?: boolean;
onWritePath: (path: string) => string;
}

/**
Expand All @@ -19,6 +20,7 @@ export default class SonarReporter implements Reporter {
constructor(options?: Partial<SonarReporterOptions>) {
this.options = {
silent: options?.silent ?? false,
onWritePath: options?.onWritePath ?? defaultOnWritePath,

// @ts-expect-error -- Can also be initialized during onInit()
outputFile: options?.outputFile,
Expand Down Expand Up @@ -57,7 +59,9 @@ export default class SonarReporter implements Reporter {
// Map filepaths to be relative to root for workspace support
const files = rawFiles?.map((file) => ({
...file,
name: relative(process.cwd(), file.filepath),
name: this.options.onWritePath(
relative(process.cwd(), file.filepath),
),
}));

const outputDirectory = dirname(reportFile);
Expand All @@ -75,6 +79,10 @@ export default class SonarReporter implements Reporter {
}
}

function defaultOnWritePath(path: string) {
return path;
}

function resolveOutputfile(config: Vitest['config']) {
if (typeof config.outputFile === 'string') {
return config.outputFile;
Expand Down
59 changes: 59 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,65 @@ test('writes a report', async () => {
`);
});

test('file path can be rewritten using options.onWritePath ', async () => {
await runVitest({
reporterOptions: {
onWritePath(path: string) {
return `custom-prefix/${path}`;
},
},
});

const contents = readFileSync(outputFile, 'utf-8');
const stable = stabilizeReport(contents);

expect(stable).toMatchInlineSnapshot(`
"<testExecutions version="1">
<file path="custom-prefix/test/fixtures/animals.test.ts">
<testCase name="animals - dogs say woof" duration="123" />
<testCase name="animals - figure out what rabbits say" duration="123">
<skipped message="figure out what rabbits say" />
</testCase>
<testCase name="animals - flying ones - cat can fly" duration="123">
<failure message="expected false to be true // Object.is equality">
<![CDATA[AssertionError: expected false to be true // Object.is equality
at <process-cwd>/test/fixtures/animals.test.js
<removed-stacktrace>
</failure>
</testCase>
<testCase name="animals - flying ones - bird can fly" duration="123" />
</file>
<file path="custom-prefix/test/fixtures/math.test.ts">
<testCase name="math - sum" duration="123" />
<testCase name="math - multiply" duration="123" />
<testCase name="math - slow calculation" duration="123" />
<testCase name="math - tricky calculation of &quot;16 / 4&quot;" duration="123">
<failure message="expected 4 to deeply equal 8">
<![CDATA[AssertionError: expected 4 to deeply equal 8
at <process-cwd>/test/fixtures/math.test.js
<removed-stacktrace>
</failure>
</testCase>
<testCase name="math - complex calculation" duration="123">
<error message="16.divideByTwo is not a function">
<![CDATA[TypeError: 16.divideByTwo is not a function
at <process-cwd>/test/fixtures/math.test.js
<removed-stacktrace>
</error>
</testCase>
<testCase name="math - random numbers are unstable" duration="123">
<skipped message="random numbers are unstable" />
</testCase>
<testCase name="math - learn square roots" duration="123">
<skipped message="learn square roots" />
</testCase>
<testCase name="math - divide - basic" duration="123" />
<testCase name="math - divide - by zero" duration="123" />
</file>
</testExecutions>"
`);
});

test('report location is logged', async () => {
const spy = vi.spyOn(console, 'log');
await runVitest();
Expand Down

0 comments on commit 3ebe0f1

Please sign in to comment.