@@ -45,16 +45,14 @@ assert.doesNotThrow(function() {
4545// an Object with a custom .inspect() function
4646const custom_inspect = { foo : 'bar' , inspect : ( ) => 'inspect' } ;
4747
48- const stdout_write = global . process . stdout . write ;
49- const stderr_write = global . process . stderr . write ;
5048const strings = [ ] ;
5149const errStrings = [ ] ;
52- global . process . stdout . write = function ( string ) {
53- strings . push ( string ) ;
54- } ;
55- global . process . stderr . write = function ( string ) {
56- errStrings . push ( string ) ;
57- } ;
50+ common . hijackStdout ( function ( data ) {
51+ strings . push ( data ) ;
52+ } ) ;
53+ common . hijackStderr ( function ( data ) {
54+ errStrings . push ( data ) ;
55+ } ) ;
5856
5957// test console.log() goes to stdout
6058console . log ( 'foo' ) ;
@@ -105,8 +103,10 @@ console.timeEnd('constructor');
105103console . time ( 'hasOwnProperty' ) ;
106104console . timeEnd ( 'hasOwnProperty' ) ;
107105
108- global . process . stdout . write = stdout_write ;
109- global . process . stderr . write = stderr_write ;
106+ assert . strictEqual ( strings . length , process . stdout . writeTimes ) ;
107+ assert . strictEqual ( errStrings . length , process . stderr . writeTimes ) ;
108+ common . restoreStdout ( ) ;
109+ common . restoreStderr ( ) ;
110110
111111// verify that console.timeEnd() doesn't leave dead links
112112const timesMapSize = console . _times . size ;
@@ -146,9 +146,6 @@ assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim()));
146146assert . strictEqual ( 'Trace: This is a {"formatted":"trace"} 10 foo' ,
147147 errStrings . shift ( ) . split ( '\n' ) . shift ( ) ) ;
148148
149- assert . strictEqual ( strings . length , 0 ) ;
150- assert . strictEqual ( errStrings . length , 0 ) ;
151-
152149assert . throws ( ( ) => {
153150 console . assert ( false , 'should throw' ) ;
154151} , common . expectsError ( {
@@ -159,3 +156,14 @@ assert.throws(() => {
159156assert . doesNotThrow ( ( ) => {
160157 console . assert ( true , 'this should not throw' ) ;
161158} ) ;
159+
160+ // hijack stderr to catch `process.emitWarning` which is using
161+ // `process.nextTick`
162+ common . hijackStderr ( common . mustCall ( function ( data ) {
163+ common . restoreStderr ( ) ;
164+
165+ // stderr.write will catch sync error, so use `process.nextTick` here
166+ process . nextTick ( function ( ) {
167+ assert . strictEqual ( data . includes ( 'no such label' ) , true ) ;
168+ } ) ;
169+ } ) ) ;
0 commit comments