@@ -24,6 +24,7 @@ import getConsoleOutput from './getConsoleOutput';
2424import getResultHeader from './getResultHeader' ;
2525
2626type write = ( chunk : string , enc ?: any , cb ?: ( ) => void ) => boolean ;
27+ type FlushBufferedOutput = ( ) => void ;
2728
2829const TITLE_BULLET = chalk . bold ( '\u25cf ' ) ;
2930
@@ -35,6 +36,7 @@ class DefaultReporter extends BaseReporter {
3536 _globalConfig : GlobalConfig ;
3637 _out : write ;
3738 _status : Status ;
39+ _bufferedOutput : Set < FlushBufferedOutput > ;
3840
3941 constructor ( globalConfig : GlobalConfig ) {
4042 super ( ) ;
@@ -43,6 +45,7 @@ class DefaultReporter extends BaseReporter {
4345 this . _out = process . stdout . write . bind ( process . stdout ) ;
4446 this . _err = process . stderr . write . bind ( process . stderr ) ;
4547 this . _status = new Status ( ) ;
48+ this . _bufferedOutput = new Set ( ) ;
4649 this . _wrapStdio ( process . stdout ) ;
4750 this . _wrapStdio ( process . stderr ) ;
4851 this . _status . onChange ( ( ) => {
@@ -57,25 +60,28 @@ class DefaultReporter extends BaseReporter {
5760 let buffer = [ ] ;
5861 let timeout = null ;
5962
60- const doFlush = ( ) => {
63+ const flushBufferedOutput = ( ) => {
6164 const string = buffer . join ( '' ) ;
6265 buffer = [ ] ;
6366 // This is to avoid conflicts between random output and status text
6467 this . _clearStatus ( ) ;
6568 originalWrite . call ( stream , string ) ;
6669 this . _printStatus ( ) ;
70+ this . _bufferedOutput . delete ( flushBufferedOutput ) ;
6771 } ;
6872
69- const flush = ( ) => {
73+ this . _bufferedOutput . add ( flushBufferedOutput ) ;
74+
75+ const debouncedFlush = ( ) => {
7076 // If the process blows up no errors would be printed.
7177 // There should be a smart way to buffer stderr, but for now
7278 // we just won't buffer it.
7379 if ( stream === process . stderr ) {
74- doFlush ( ) ;
80+ flushBufferedOutput ( ) ;
7581 } else {
7682 if ( ! timeout ) {
7783 timeout = setTimeout ( ( ) => {
78- doFlush ( ) ;
84+ flushBufferedOutput ( ) ;
7985 timeout = null ;
8086 } , 100 ) ;
8187 }
@@ -85,11 +91,18 @@ class DefaultReporter extends BaseReporter {
8591 // $FlowFixMe
8692 stream . write = chunk => {
8793 buffer . push ( chunk ) ;
88- flush ( ) ;
94+ debouncedFlush ( ) ;
8995 return true ;
9096 } ;
9197 }
9298
99+ // Don't wait for the debounced call and flush all output immediately.
100+ forceFlushBufferedOutput ( ) {
101+ for ( const flushBufferedOutput of this . _bufferedOutput ) {
102+ flushBufferedOutput ( ) ;
103+ }
104+ }
105+
93106 _clearStatus ( ) {
94107 if ( isInteractive ) {
95108 this . _out ( this . _clear ) ;
@@ -116,6 +129,7 @@ class DefaultReporter extends BaseReporter {
116129 }
117130
118131 onRunComplete ( ) {
132+ this . forceFlushBufferedOutput ( ) ;
119133 this . _status . runFinished ( ) ;
120134 // $FlowFixMe
121135 process . stdout . write = this . _out ;
@@ -139,6 +153,7 @@ class DefaultReporter extends BaseReporter {
139153 test . context . config ,
140154 testResult ,
141155 ) ;
156+ this . forceFlushBufferedOutput ( ) ;
142157 }
143158
144159 _printTestFileSummary (
0 commit comments