@@ -2,6 +2,10 @@ import type { Writable } from 'node:stream'
22import type { Vitest } from '../../core'
33import { stripVTControlCharacters } from 'node:util'
44
5+ /** Minimum time between two renders, no matter how many scheduled renderes were called */
6+ const DEFAULT_RENDER_THRESHOLD_MS = 100
7+
8+ /** Interval between automatic renders. If no test state changes happened, this will increase just duration field */
59const DEFAULT_RENDER_INTERVAL_MS = 1_000
610
711const ESC = '\x1B['
@@ -10,9 +14,10 @@ const MOVE_CURSOR_ONE_ROW_UP = `${ESC}1A`
1014const SYNC_START = `${ ESC } ?2026h`
1115const SYNC_END = `${ ESC } ?2026l`
1216
13- interface Options {
17+ export interface Options {
1418 logger : Vitest [ 'logger' ]
1519 interval ?: number
20+ threshold ?: number
1621 getWindow : ( ) => string [ ]
1722}
1823
@@ -36,10 +41,12 @@ export class WindowRenderer {
3641
3742 constructor ( options : Options ) {
3843 this . options = {
39- interval : DEFAULT_RENDER_INTERVAL_MS ,
4044 ...options ,
45+ threshold : options . threshold ?? DEFAULT_RENDER_THRESHOLD_MS ,
46+ interval : options . interval ?? DEFAULT_RENDER_INTERVAL_MS ,
4147 }
4248
49+ // Capture the original write methods early, before intercepting these
4350 this . streams = {
4451 output : options . logger . outputStream . write . bind ( options . logger . outputStream ) ,
4552 error : options . logger . errorStream . write . bind ( options . logger . errorStream ) ,
@@ -50,6 +57,14 @@ export class WindowRenderer {
5057 this . interceptStream ( process . stderr , 'error' ) ,
5158 )
5259
60+ // Intercept calls to custom VitestOptions.stdout and stderr streams
61+ if ( options . logger . outputStream !== process . stdout ) {
62+ this . cleanups . push ( this . interceptStream ( options . logger . outputStream , 'output' ) )
63+ }
64+ if ( options . logger . errorStream !== process . stderr ) {
65+ this . cleanups . push ( this . interceptStream ( options . logger . errorStream , 'error' ) )
66+ }
67+
5368 // Write buffered content on unexpected exits, e.g. direct `process.exit()` calls
5469 this . options . logger . onTerminalCleanup ( ( ) => {
5570 this . flushBuffer ( )
@@ -86,9 +101,14 @@ export class WindowRenderer {
86101 this . renderScheduled = true
87102 this . flushBuffer ( )
88103
89- setTimeout ( ( ) => {
104+ if ( this . options . threshold ) {
105+ setTimeout ( ( ) => {
106+ this . renderScheduled = false
107+ } , this . options . threshold ) . unref ( )
108+ }
109+ else {
90110 this . renderScheduled = false
91- } , 100 ) . unref ( )
111+ }
92112 }
93113 }
94114
@@ -121,9 +141,12 @@ export class WindowRenderer {
121141 }
122142
123143 private render ( message ?: string , type : StreamType = 'output' ) {
144+ this . write ( SYNC_START )
145+
124146 if ( this . finished ) {
125147 this . clearWindow ( )
126- return this . write ( message || '' , type )
148+ this . write ( message || '' , type )
149+ return this . write ( SYNC_END )
127150 }
128151
129152 const windowContent = this . options . getWindow ( )
@@ -134,7 +157,6 @@ export class WindowRenderer {
134157 padding -= getRenderedRowCount ( [ message ] , this . options . logger . getColumns ( ) )
135158 }
136159
137- this . write ( SYNC_START )
138160 this . clearWindow ( )
139161
140162 if ( message ) {
@@ -165,7 +187,7 @@ export class WindowRenderer {
165187 this . windowHeight = 0
166188 }
167189
168- private interceptStream ( stream : NodeJS . WriteStream , type : StreamType ) {
190+ private interceptStream ( stream : NodeJS . WriteStream | Writable , type : StreamType ) {
169191 const original = stream . write
170192
171193 // @ts -expect-error -- not sure how 2 overloads should be typed
0 commit comments