Skip to content

Commit 34a80b3

Browse files
authored
fix: print console statements in vmThreads (#5678)
1 parent 1277dc1 commit 34a80b3

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

packages/vitest/src/runtime/console.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { relative } from 'node:path'
44
import { getColors, getSafeTimers } from '@vitest/utils'
55
import { RealDate } from '../integrations/mock/date'
66
import { getWorkerState } from '../utils'
7+
import type { WorkerGlobalState } from '../types'
78

89
export const UNKNOWN_TEST_ID = '__vitest__unknown_test__'
910

@@ -27,14 +28,14 @@ function getTaskIdByStack(root: string) {
2728
return UNKNOWN_TEST_ID
2829
}
2930

30-
export function createCustomConsole() {
31+
export function createCustomConsole(defaultState?: WorkerGlobalState) {
3132
const stdoutBuffer = new Map<string, any[]>()
3233
const stderrBuffer = new Map<string, any[]>()
3334
const timers = new Map<string, { stdoutTime: number; stderrTime: number; timer: any }>()
3435

3536
const { setTimeout, clearTimeout } = getSafeTimers()
3637

37-
const state = () => getWorkerState()
38+
const state = () => defaultState || getWorkerState()
3839

3940
// group sync console.log calls with macro task
4041
function schedule(taskId: string) {

packages/vitest/src/runtime/workers/vm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export async function runVmTests(state: WorkerGlobalState) {
4848
// because browser doesn't provide these globals
4949
context.process = process
5050
context.global = context
51-
context.console = state.config.disableConsoleIntercept ? console : createCustomConsole()
51+
context.console = state.config.disableConsoleIntercept ? console : createCustomConsole(state)
5252
// TODO: don't hardcode setImmediate in fake timers defaults
5353
context.setImmediate = setImmediate
5454
context.clearImmediate = clearImmediate

test/cli/test/setup-files.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { promises as fs } from 'node:fs'
22
import { describe, expect, it, test } from 'vitest'
33
import { editFile, runVitest } from '../../test-utils'
44

5-
test('print stdout and stderr correctly when called in the setup file', async () => {
5+
test.each(['threads', 'vmThreads'])('%s: print stdout and stderr correctly when called in the setup file', async (pool) => {
66
const { stdout, stderr } = await runVitest({
77
root: 'fixtures/setup-files',
88
include: ['empty.test.ts'],
99
setupFiles: ['./console-setup.ts'],
10+
pool,
1011
})
1112

1213
const filepath = 'console-setup.ts'

0 commit comments

Comments
 (0)