Skip to content

Commit

Permalink
test: switch child_process to startVitest
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Feb 4, 2024
1 parent 342c892 commit d31deef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 49 deletions.
52 changes: 25 additions & 27 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { exec } from 'node:child_process';
import { existsSync, readFileSync, rmSync } from 'node:fs';
import { beforeEach, expect, test } from 'vitest';
import { beforeEach, expect, test, vi } from 'vitest';
import { startVitest } from 'vitest/node';
import { stabilizeReport } from './utils';

import { outputFile } from './vite.test-config';
const outputFile = 'report-from-tests.xml';

beforeEach(cleanup);
beforeEach(() => {
if (existsSync(outputFile)) {
rmSync(outputFile);
}
});

test('writes a report', async () => {
expect(existsSync(outputFile)).toBe(false);
Expand Down Expand Up @@ -64,40 +68,34 @@ test('writes a report', async () => {
});

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

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

expect(stabilizeReport(data)).toMatchInlineSnapshot(
const call = spy.mock.lastCall?.[0];
spy.mockRestore();

expect(stabilizeReport(call)).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 },
});
const spy = vi.spyOn(console, 'log');
await runVitest({ sonarReporterOptions: { silent: true } });

expect(existsSync(outputFile)).toBe(true);
expect(data).toBe('');
expect(spy).not.toHaveBeenCalled();
spy.mockRestore();
});

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);
await startVitest('test', [], {
watch: false,
reporters: new URL('../src/index.ts', import.meta.url).href,
outputFile,
include: ['test/fixtures/*.test.ts'],
...opts,
});

return stdout;
}

function cleanup() {
if (existsSync(outputFile)) {
rmSync(outputFile);
}
}
22 changes: 0 additions & 22 deletions test/vite.test-config.ts

This file was deleted.

5 changes: 5 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ export default defineConfig({
reporters: 'verbose',
include: ['test/*.test.ts'],
watchExclude: ['report-from-tests.xml'],
onConsoleLog(log) {
if (log.includes('SonarQube report written to')) {
return false;
}
},
},
});

0 comments on commit d31deef

Please sign in to comment.