From 58054a8e7b7d7910fe8ecb3eaf5c00c342b1da64 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Fri, 16 Oct 2026 15:29:11 +0200 Subject: [PATCH] fix: don't fail on console log pending at teardown --- packages/vitest/src/runtime/console.ts | 25 +++++++++------ test/e2e/test/console.test.ts | 44 +++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/packages/vitest/src/runtime/console.ts b/packages/vitest/src/runtime/console.ts index d6c48b074023..a3d839e94aac 100644 --- a/packages/vitest/src/runtime/console.ts +++ b/packages/vitest/src/runtime/console.ts @@ -4,7 +4,7 @@ import { relative } from 'node:path' import { Writable } from 'node:stream' import { getSafeTimers } from '@vitest/utils/timers' import c from 'tinyrainbow' -import { getWorkerState } from './utils' +import { EnvironmentTeardownError, getWorkerState } from './utils' const RealDate = globalThis.Date @@ -109,14 +109,21 @@ export function createCustomConsole(defaultState?: WorkerGlobalState): Console { ) { const timer = timers.get(taskId)! const time = type === 'stderr' ? timer.stderrTime : timer.stdoutTime - state().rpc.onUserConsoleLog({ - type, - content: content || '', - taskId, - time: time || RealDate.now(), - size, - origin, - }) + state() + .rpc.onUserConsoleLog({ + type, + content: content || '', + taskId, + time: time || RealDate.now(), + size, + origin, + }) + .catch((error) => { + // the log is already sent, the worker only closed before the reporter answered + if (!(error instanceof EnvironmentTeardownError)) { + throw error + } + }) } const stdout = new Writable({ diff --git a/test/e2e/test/console.test.ts b/test/e2e/test/console.test.ts index dd1d5a583c69..8bfa7e550fde 100644 --- a/test/e2e/test/console.test.ts +++ b/test/e2e/test/console.test.ts @@ -1,7 +1,7 @@ import { relative, resolve } from 'pathe' import { expect, test } from 'vitest' import { DefaultReporter } from 'vitest/node' -import { runInlineTests, runVitest } from '../../test-utils' +import { runInlineTests, runVitest, ts } from '../../test-utils' test('can run custom pools with Vitest', async () => { const reporter = new DefaultReporter() @@ -152,3 +152,45 @@ test('onConsoleLog receives the entity', async () => { ] `) }) + +// https://github.com/vitest-dev/vitest/issues/11153 +test('console log sent while the worker closes is not reported as an error', async () => { + const { stderr, testTree } = await runInlineTests( + { + 'late-log.test.ts': ts` + import { test } from 'vitest' + + test('logs and leaks an interval', () => { + console.log('log inside the test') + let ticks = 0 + const interval = setInterval(() => { + console.log('log after the test') + if (++ticks === 50) { + clearInterval(interval) + } + }, 2) + }) + `, + }, + { + reporters: [ + 'verbose', + { + async onUserConsoleLog() { + // keep every log pending, so the worker closes while the late logs wait for an answer + await new Promise((resolve) => setTimeout(resolve, 20)) + }, + }, + ], + }, + ) + + expect(stderr).toBe('') + expect(testTree()).toMatchInlineSnapshot(` + { + "late-log.test.ts": { + "logs and leaks an interval": "passed", + }, + } + `) +})