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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy