@@ -3,7 +3,7 @@ import { Console } from 'node:console'
3
3
import { relative } from 'node:path'
4
4
import { getColors , getSafeTimers } from '@vitest/utils'
5
5
import { RealDate } from '../integrations/mock/date'
6
- import type { WorkerGlobalState } from '../types '
6
+ import { getWorkerState } from '../utils '
7
7
8
8
export const UNKNOWN_TEST_ID = '__vitest__unknown_test__'
9
9
@@ -27,13 +27,15 @@ function getTaskIdByStack(root: string) {
27
27
return UNKNOWN_TEST_ID
28
28
}
29
29
30
- export function createCustomConsole ( state : WorkerGlobalState ) {
30
+ export function createCustomConsole ( ) {
31
31
const stdoutBuffer = new Map < string , any [ ] > ( )
32
32
const stderrBuffer = new Map < string , any [ ] > ( )
33
33
const timers = new Map < string , { stdoutTime : number ; stderrTime : number ; timer : any } > ( )
34
34
35
35
const { setTimeout, clearTimeout } = getSafeTimers ( )
36
36
37
+ const state = ( ) => getWorkerState ( )
38
+
37
39
// group sync console.log calls with macro task
38
40
function schedule ( taskId : string ) {
39
41
const timer = timers . get ( taskId ) !
@@ -56,7 +58,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
56
58
return
57
59
const content = buffer . map ( i => String ( i ) ) . join ( '' )
58
60
const timer = timers . get ( taskId ) !
59
- state . rpc . onUserConsoleLog ( {
61
+ state ( ) . rpc . onUserConsoleLog ( {
60
62
type : 'stdout' ,
61
63
content : content || '<empty line>' ,
62
64
taskId,
@@ -72,7 +74,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
72
74
return
73
75
const content = buffer . map ( i => String ( i ) ) . join ( '' )
74
76
const timer = timers . get ( taskId ) !
75
- state . rpc . onUserConsoleLog ( {
77
+ state ( ) . rpc . onUserConsoleLog ( {
76
78
type : 'stderr' ,
77
79
content : content || '<empty line>' ,
78
80
taskId,
@@ -85,7 +87,8 @@ export function createCustomConsole(state: WorkerGlobalState) {
85
87
86
88
const stdout = new Writable ( {
87
89
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 )
89
92
let timer = timers . get ( id )
90
93
if ( timer ) {
91
94
timer . stdoutTime = timer . stdoutTime || RealDate . now ( )
@@ -106,7 +109,8 @@ export function createCustomConsole(state: WorkerGlobalState) {
106
109
} )
107
110
const stderr = new Writable ( {
108
111
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 )
110
114
let timer = timers . get ( id )
111
115
if ( timer ) {
112
116
timer . stderrTime = timer . stderrTime || RealDate . now ( )
0 commit comments