@@ -45,16 +45,14 @@ assert.doesNotThrow(function() {
45
45
// an Object with a custom .inspect() function
46
46
const custom_inspect = { foo : 'bar' , inspect : ( ) => 'inspect' } ;
47
47
48
- const stdout_write = global . process . stdout . write ;
49
- const stderr_write = global . process . stderr . write ;
50
48
const strings = [ ] ;
51
49
const 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
+ } ) ;
58
56
59
57
// test console.log() goes to stdout
60
58
console . log ( 'foo' ) ;
@@ -105,8 +103,10 @@ console.timeEnd('constructor');
105
103
console . time ( 'hasOwnProperty' ) ;
106
104
console . timeEnd ( 'hasOwnProperty' ) ;
107
105
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 ( ) ;
110
110
111
111
// verify that console.timeEnd() doesn't leave dead links
112
112
const timesMapSize = console . _times . size ;
@@ -146,9 +146,6 @@ assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim()));
146
146
assert . strictEqual ( 'Trace: This is a {"formatted":"trace"} 10 foo' ,
147
147
errStrings . shift ( ) . split ( '\n' ) . shift ( ) ) ;
148
148
149
- assert . strictEqual ( strings . length , 0 ) ;
150
- assert . strictEqual ( errStrings . length , 0 ) ;
151
-
152
149
assert . throws ( ( ) => {
153
150
console . assert ( false , 'should throw' ) ;
154
151
} , common . expectsError ( {
@@ -159,3 +156,14 @@ assert.throws(() => {
159
156
assert . doesNotThrow ( ( ) => {
160
157
console . assert ( true , 'this should not throw' ) ;
161
158
} ) ;
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