Skip to content

Commit

Permalink
fix: print console statements in vmThreads (#5678)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored May 6, 2024
1 parent 1277dc1 commit 34a80b3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/vitest/src/runtime/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__'

Expand All @@ -27,14 +28,14 @@ function getTaskIdByStack(root: string) {
return UNKNOWN_TEST_ID
}

export function createCustomConsole() {
export function createCustomConsole(defaultState?: WorkerGlobalState) {
const stdoutBuffer = new Map<string, any[]>()
const stderrBuffer = new Map<string, any[]>()
const timers = new Map<string, { stdoutTime: number; stderrTime: number; timer: any }>()

const { setTimeout, clearTimeout } = getSafeTimers()

const state = () => getWorkerState()
const state = () => defaultState || getWorkerState()

// group sync console.log calls with macro task
function schedule(taskId: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/workers/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/cli/test/setup-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 34a80b3

Please sign in to comment.