diff --git a/packages/vitest/src/runtime/console.ts b/packages/vitest/src/runtime/console.ts index a7b1a70715bf..6158bf169e60 100644 --- a/packages/vitest/src/runtime/console.ts +++ b/packages/vitest/src/runtime/console.ts @@ -4,6 +4,7 @@ import { relative } from 'node:path' import { getColors, getSafeTimers } from '@vitest/utils' import { RealDate } from '../integrations/mock/date' import { getWorkerState } from '../utils' +import type { WorkerGlobalState } from '../types' export const UNKNOWN_TEST_ID = '__vitest__unknown_test__' @@ -27,14 +28,14 @@ function getTaskIdByStack(root: string) { return UNKNOWN_TEST_ID } -export function createCustomConsole() { +export function createCustomConsole(defaultState?: WorkerGlobalState) { const stdoutBuffer = new Map() const stderrBuffer = new Map() const timers = new Map() const { setTimeout, clearTimeout } = getSafeTimers() - const state = () => getWorkerState() + const state = () => defaultState || getWorkerState() // group sync console.log calls with macro task function schedule(taskId: string) { diff --git a/packages/vitest/src/runtime/workers/vm.ts b/packages/vitest/src/runtime/workers/vm.ts index 9db97281b162..cb9fb048b59d 100644 --- a/packages/vitest/src/runtime/workers/vm.ts +++ b/packages/vitest/src/runtime/workers/vm.ts @@ -48,7 +48,7 @@ export async function runVmTests(state: WorkerGlobalState) { // because browser doesn't provide these globals context.process = process context.global = context - context.console = state.config.disableConsoleIntercept ? console : createCustomConsole() + context.console = state.config.disableConsoleIntercept ? console : createCustomConsole(state) // TODO: don't hardcode setImmediate in fake timers defaults context.setImmediate = setImmediate context.clearImmediate = clearImmediate diff --git a/test/cli/test/setup-files.test.ts b/test/cli/test/setup-files.test.ts index 3e67edeab58f..93342aa62a9b 100644 --- a/test/cli/test/setup-files.test.ts +++ b/test/cli/test/setup-files.test.ts @@ -2,11 +2,12 @@ import { promises as fs } from 'node:fs' import { describe, expect, it, test } from 'vitest' import { editFile, runVitest } from '../../test-utils' -test('print stdout and stderr correctly when called in the setup file', async () => { +test.each(['threads', 'vmThreads'])('%s: print stdout and stderr correctly when called in the setup file', async (pool) => { const { stdout, stderr } = await runVitest({ root: 'fixtures/setup-files', include: ['empty.test.ts'], setupFiles: ['./console-setup.ts'], + pool, }) const filepath = 'console-setup.ts'