Skip to content

Commit 3463f9b

Browse files
committedMar 28, 2024··
fix(vitest): correctly send console events when state changes
1 parent 7e91781 commit 3463f9b

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed
 

‎packages/vitest/src/runtime/console.ts

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

88
export const UNKNOWN_TEST_ID = '__vitest__unknown_test__'
99

@@ -27,13 +27,15 @@ function getTaskIdByStack(root: string) {
2727
return UNKNOWN_TEST_ID
2828
}
2929

30-
export function createCustomConsole(state: WorkerGlobalState) {
30+
export function createCustomConsole() {
3131
const stdoutBuffer = new Map<string, any[]>()
3232
const stderrBuffer = new Map<string, any[]>()
3333
const timers = new Map<string, { stdoutTime: number; stderrTime: number; timer: any }>()
3434

3535
const { setTimeout, clearTimeout } = getSafeTimers()
3636

37+
const state = () => getWorkerState()
38+
3739
// group sync console.log calls with macro task
3840
function schedule(taskId: string) {
3941
const timer = timers.get(taskId)!
@@ -56,7 +58,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
5658
return
5759
const content = buffer.map(i => String(i)).join('')
5860
const timer = timers.get(taskId)!
59-
state.rpc.onUserConsoleLog({
61+
state().rpc.onUserConsoleLog({
6062
type: 'stdout',
6163
content: content || '<empty line>',
6264
taskId,
@@ -72,7 +74,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
7274
return
7375
const content = buffer.map(i => String(i)).join('')
7476
const timer = timers.get(taskId)!
75-
state.rpc.onUserConsoleLog({
77+
state().rpc.onUserConsoleLog({
7678
type: 'stderr',
7779
content: content || '<empty line>',
7880
taskId,
@@ -85,7 +87,8 @@ export function createCustomConsole(state: WorkerGlobalState) {
8587

8688
const stdout = new Writable({
8789
write(data, encoding, callback) {
88-
const id = state?.current?.id || state?.current?.file?.id || getTaskIdByStack(state.ctx.config.root)
90+
const s = state()
91+
const id = s?.current?.id || s?.current?.file?.id || getTaskIdByStack(s.config.root)
8992
let timer = timers.get(id)
9093
if (timer) {
9194
timer.stdoutTime = timer.stdoutTime || RealDate.now()
@@ -106,7 +109,8 @@ export function createCustomConsole(state: WorkerGlobalState) {
106109
})
107110
const stderr = new Writable({
108111
write(data, encoding, callback) {
109-
const id = state?.current?.id || state?.current?.file?.id || getTaskIdByStack(state.ctx.config.root)
112+
const s = state()
113+
const id = s?.current?.id || s?.current?.file?.id || getTaskIdByStack(s.config.root)
110114
let timer = timers.get(id)
111115
if (timer) {
112116
timer.stderrTime = timer.stderrTime || RealDate.now()

‎packages/vitest/src/runtime/setup-node.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import timers from 'node:timers'
44
import { isatty } from 'node:tty'
55
import { installSourcemapsSupport } from 'vite-node/source-map'
66
import { createColors, setupColors } from '@vitest/utils'
7-
import type { EnvironmentOptions, ResolvedConfig, ResolvedTestEnvironment, WorkerGlobalState } from '../types'
7+
import type { EnvironmentOptions, ResolvedConfig, ResolvedTestEnvironment } from '../types'
88
import { VitestSnapshotEnvironment } from '../integrations/snapshot/environments/node'
99
import { getSafeTimers, getWorkerState } from '../utils'
1010
import * as VitestIndex from '../index'
@@ -56,13 +56,13 @@ export async function setupGlobalEnv(config: ResolvedConfig, { environment }: Re
5656
})
5757

5858
if (!config.disableConsoleIntercept)
59-
await setupConsoleLogSpy(state)
59+
await setupConsoleLogSpy()
6060
}
6161

62-
export async function setupConsoleLogSpy(state: WorkerGlobalState) {
62+
export async function setupConsoleLogSpy() {
6363
const { createCustomConsole } = await import('./console')
6464

65-
globalThis.console = createCustomConsole(state)
65+
globalThis.console = createCustomConsole()
6666
}
6767

6868
export async function withEnv(

0 commit comments

Comments
 (0)
Please sign in to comment.