Skip to content

Commit

Permalink
feat: option to silence logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Mar 22, 2023
1 parent 09c1333 commit 10a756f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -60,6 +60,21 @@ sonar.testExecutionReportPaths=sonar-report.xml
- ❌ Do not `import` reporter as `import SonarReporter from 'vitest-sonar-reporter';`
- ✅ Define reporter as `reporters: ['vitest-sonar-reporter']` so that `vitest` will process it. This quarantees support for projects without `{ type: 'module' }`.

### Options

You can pass additional options using `test.sonarReporterOptions` in `vite.config.ts`. Note that passing custom options to Vitest reporters is unconventional and may require you to use `@ts-ignore` when using TypeScript.

#### `silent`

Silence reporter's verbose logging.

```ts
test: {
reporters: 'vitest-sonar-reporter',
sonarReporterOptions: { silent: true }
}
```

## Code Coverage

This reporter does not process code coverage - Vitest already supports that out-of-the-box!
Expand Down
9 changes: 8 additions & 1 deletion src/sonar-reporter.ts
Expand Up @@ -10,10 +10,14 @@ import { generateXml } from './xml.js';
export default class SonarReporter implements Reporter {
ctx!: Vitest;
outputFile!: string;
silent!: boolean;

onInit(ctx: Vitest) {
this.ctx = ctx;

// @ts-expect-error -- untyped
this.silent = ctx.config.sonarReporterOptions?.silent === true;

if (!this.ctx.config.outputFile) {
throw new Error(
'SonarReporter requires config.outputFile to be defined in vite config'
Expand All @@ -38,7 +42,10 @@ export default class SonarReporter implements Reporter {
const sorted = files?.sort(sortByFilename);

writeFileSync(reportFile, generateXml(sorted), 'utf-8');
this.ctx.logger.log(`SonarQube report written to ${reportFile}`);

if (!this.silent) {
this.ctx.logger.log(`SonarQube report written to ${reportFile}`);
}
}
}

Expand Down
47 changes: 36 additions & 11 deletions test/index.test.ts
@@ -1,4 +1,4 @@
import { execSync } from 'child_process';
import { exec } from 'child_process';
import { existsSync, readFileSync, rmSync } from 'fs';
import { beforeEach, expect, test } from 'vitest';
import { stabilizeReport } from './utils';
Expand All @@ -7,18 +7,10 @@ import { outputFile } from './vite.test-config';

beforeEach(cleanup);

test('writes a report', () => {
test('writes a report', async () => {
expect(existsSync(outputFile)).toBe(false);

try {
// "vitest" binary should be available when run through package.json script
execSync('vitest --config test/vite.test-config.ts', {
stdio: 'inherit',
});
} catch (_) {
// Ignore exit codes
}

await runVitest();
expect(existsSync(outputFile)).toBe(true);

const contents = readFileSync(outputFile, 'utf-8');
Expand Down Expand Up @@ -71,6 +63,39 @@ test('writes a report', () => {
`);
});

test('report location is logged', async () => {
const data = await runVitest();
expect(existsSync(outputFile)).toBe(true);

expect(stabilizeReport(data)).toMatchInlineSnapshot(
'"SonarQube report written to <process-cwd>/report-from-tests.xml"'
);
});

test('logging can be silenced', async () => {
const data = await runVitest({
env: { ...process.env, TEST_ARGS_SILENT: 1 },
});

expect(existsSync(outputFile)).toBe(true);
expect(data).toBe('');
});

async function runVitest(opts = {}) {
// "vitest" binary should be available when run through package.json script
const subprocess = exec('vitest --config test/vite.test-config.ts', opts);

let stdout = '';
subprocess.stdout?.on('data', (data) => (stdout += data.toString()));

await new Promise((resolve, reject) => {
subprocess.on('exit', resolve);
subprocess.stderr?.on('data', reject);
});

return stdout;
}

function cleanup() {
if (existsSync(outputFile)) {
rmSync(outputFile);
Expand Down
3 changes: 2 additions & 1 deletion test/utils.ts
Expand Up @@ -40,6 +40,7 @@ export function stabilizeReport(report: string) {
limitStacktraces,
removeCwd,
removeLineNumbers,
removeDurations
removeDurations,
(text) => text.trim()
)(report);
}
5 changes: 5 additions & 0 deletions test/vite.test-config.ts
Expand Up @@ -4,6 +4,8 @@ import SonarReporter from '../src/sonar-reporter';

export const outputFile = 'report-from-tests.xml';

const silent = Boolean(process.env.TEST_ARGS_SILENT);

export default defineConfig({
test: {
watch: false,
Expand All @@ -13,5 +15,8 @@ export default defineConfig({
'test/fixtures/animals.test.ts',
'test/fixtures/math.test.ts',
],

// @ts-expect-error -- untyped
sonarReporterOptions: { silent },
},
});

0 comments on commit 10a756f

Please sign in to comment.