SonarQube reporter for Vitest
npm install vitest-sonar-reporter[![Version][version-badge]][npm-url]
[![Downloads][downloads-url]][npm-url]
[![Compatibility with vitest@latest][vitest-check-badge]][vitest-check-url]
Live examples | Installation | Configuration | Code coverage | Examples
---
> SonarQube reporter for Vitest
Generates Generic Execution reports from vitest tests for SonarQube to analyze.
- Project with Vitest Workspaces | Stackblitz
- Project with { type: "module" } | Stackblitz
- Project with { type: "commonjs" } | Stackblitz
vitest-sonar-reporter should be included in development dependencies. vitest is required as peer dependency.
``shFor Vitest v3 and above
npm install --save-dev vitest-sonar-reporter
Configuration
Add new custom reporter and define
outputFile in your vite.config.ts:`ts
import { defineConfig } from 'vitest/config';export default defineConfig({
test: {
reporters: [
'default', // Vitest's default reporter so that terminal output is still visible
['vitest-sonar-reporter', { outputFile: 'sonar-report.xml' }],
],
},
});
`If you are using Vitest below version
^1.3.0 you can define file in test.outputFile:`ts
test: {
reporters: ['json', 'verbose', 'vitest-sonar-reporter'],
outputFile: {
json: 'my-json-report.json',
'vitest-sonar-reporter': 'sonar-report.xml',
},
},
`sonar-project.properties:`
sonar.testExecutionReportPaths=sonar-report.xml
`$3
You can pass additional options to reporter. Note that this requires
vitest@^1.3.0.####
silentSilence reporter's verbose logging.
`ts
test: {
reporters: [
['vitest-sonar-reporter', { silent: true }]
],
}
`####
onWritePathRewrite
path attribute of . 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. '' to ''
return frontend/${path};
}
}]
],
}
``diff
-
+
`####
outputFileLocation 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!
Simply configure
vitest to output LCOV reports and instruct SonarQube to pick these reports.`ts
test: {
coverage: {
reporters: 'lcov',
},
},
``
sonar.javascript.lcov.reportPaths=./coverage/lcov.info
`Examples
$3
See examples/example-workspace for example setup using Vitest Workspaces.
$3
`ts
import { describe, expect, test } from 'vitest';describe('animals', () => {
test('dogs say woof', () => {
const dog = { say: () => 'woof' };
expect(dog.say()).toBe('woof');
});
test.todo('figure out what rabbits say', () => {
const rabbit = { say: () => '????' };
expect(rabbit.say()).toBe('?');
});
describe('flying ones', () => {
test('cats can fly', () => {
const cat = { fly: () => false };
expect(cat.fly()).toBe(true);
});
test('birds can fly', () => {
const bird = { fly: () => true };
expect(bird.fly()).toBe(true);
});
});
});
``xml
at /workspaces/example/test/animals.test.ts:15:47
at /workspaces/example/node_modules/vitest/dist/chunk-runtime-chain.7032872a.js:82:26
at runTest (/workspaces/example/node_modules/vitest/dist/entry.js:771:40)
at async runSuite (/workspaces/example/node_modules/vitest/dist/entry.js:836:13)
at async runSuite (/workspaces/example/node_modules/vitest/dist/entry.js:836:13)
at async runSuite (/workspaces/example/node_modules/vitest/dist/entry.js:836:13)
at async runFiles (/workspaces/example/node_modules/vitest/dist/entry.js:873:5)
at async startTests (/workspaces/example/node_modules/vitest/dist/entry.js:879:3)
at async /workspaces/example/node_modules/vitest/dist/entry.js:906:7
at async withEnv (/workspaces/example/node_modules/vitest/dist/entry.js:503:5)]]>
``[version-badge]: https://img.shields.io/npm/v/vitest-sonar-reporter
[npm-url]: https://www.npmjs.com/package/vitest-sonar-reporter
[downloads-url]: https://img.shields.io/npm/dm/vitest-sonar-reporter
[vitest-check-badge]: https://github.com/ariperkkio/vitest-sonar-reporter/workflows/vitest@latest%20compatibility/badge.svg
[vitest-check-url]: https://github.com/AriPerkkio/vitest-sonar-reporter/actions/workflows/vitest-latest-compatibility.yml