1'use strict'; 2 3const common = require('../common'); 4const { 5 hijackStderr, 6 restoreStderr 7} = require('../common/hijackstdio'); 8const assert = require('assert'); 9 10function test1() { 11 // Output is skipped if the argument to the 'warning' event is 12 // not an Error object. 13 hijackStderr(common.mustNotCall('stderr.write must not be called')); 14 process.emit('warning', 'test'); 15 setImmediate(test2); 16} 17 18function test2() { 19 // Output is skipped if it's a deprecation warning and 20 // process.noDeprecation = true 21 process.noDeprecation = true; 22 process.emitWarning('test', 'DeprecationWarning'); 23 process.noDeprecation = false; 24 setImmediate(test3); 25} 26 27function test3() { 28 restoreStderr(); 29 // Type defaults to warning when the second argument is an object 30 process.emitWarning('test', {}); 31 process.once('warning', common.mustCall((warning) => { 32 assert.strictEqual(warning.name, 'Warning'); 33 })); 34 setImmediate(test4); 35} 36 37function test4() { 38 // process.emitWarning will throw when process.throwDeprecation is true 39 // and type is `DeprecationWarning`. 40 process.throwDeprecation = true; 41 process.once('uncaughtException', (err) => { 42 assert.match(err.toString(), /^DeprecationWarning: test$/); 43 }); 44 try { 45 process.emitWarning('test', 'DeprecationWarning'); 46 } catch { 47 assert.fail('Unreachable'); 48 } 49 process.throwDeprecation = false; 50 setImmediate(test5); 51} 52 53function test5() { 54 // Setting toString to a non-function should not cause an error 55 const err = new Error('test'); 56 err.toString = 1; 57 process.emitWarning(err); 58 setImmediate(test6); 59} 60 61function test6() { 62 process.emitWarning('test', { detail: 'foo' }); 63 process.on('warning', (warning) => { 64 assert.strictEqual(warning.detail, 'foo'); 65 }); 66} 67 68test1(); 69