From f4532371944d9e408bbb25a41cabd5a0cd8ce905 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 15 Jul 2025 14:42:33 +0200 Subject: [PATCH] test: print onUserConsoleLog separetly from test run (#8312) --- test/reporters/tests/test-run.test.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/reporters/tests/test-run.test.ts b/test/reporters/tests/test-run.test.ts index cafe8c33a..5fbe48e93 100644 --- a/test/reporters/tests/test-run.test.ts +++ b/test/reporters/tests/test-run.test.ts @@ -165,17 +165,22 @@ describe('TestCase', () => { console.log("Test running!") }); `, - }) + }, {}, { printTestRunEvents: true }) + // console.log is reported outside of the test run cycle expect(report).toMatchInlineSnapshot(` " + onTestRunStart (1 specifications) onTestModuleQueued (example.test.ts) onTestModuleCollected (example.test.ts) onTestModuleStart (example.test.ts) onTestCaseReady (example.test.ts) |single test case| - onUserConsoleLog (example.test.ts) |single test case| > Test running! onTestCaseResult (example.test.ts) |single test case| - onTestModuleEnd (example.test.ts)" + onTestModuleEnd (example.test.ts) + + onTestRunEnd (passed, 1 modules, 0 errors) + + onUserConsoleLog (example.test.ts) |single test case| > Test running!" `) }) @@ -1146,6 +1151,7 @@ async function run( class CustomReporter implements Reporter { calls: string[] = [] + logs: string[] = [] ctx!: Vitest constructor(private options: ReporterOptions = {}) {} @@ -1163,6 +1169,9 @@ class CustomReporter implements Reporter { onTestRunEnd(modules: ReadonlyArray, errors: ReadonlyArray, state: TestRunEndReason) { if (this.options.printTestRunEvents) { this.calls.push(`onTestRunEnd (${state}, ${modules.length} modules, ${errors.length} errors)`) + if (this.logs.length) { + this.calls.push('', ...this.logs) + } } } @@ -1202,7 +1211,7 @@ class CustomReporter implements Reporter { const task = this.ctx.state.idMap.get(log.taskId!) const test = task && this.ctx.state.getReportedEntity(task) as TestCase - this.calls.push(`${padded(test!, 'onUserConsoleLog')} (${this.normalizeFilename(test!.module)}) |${test!.name}| > ${log.content.replaceAll('\n', '')}`) + this.logs.push(`onUserConsoleLog (${this.normalizeFilename(test!.module)}) |${test!.name}| > ${log.content.replaceAll('\n', '')}`) } onHookStart(hook: ReportedHookContext) { -- 2.51.2