11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst { 51cb0ef41Sopenharmony_ci hijackStderr, 61cb0ef41Sopenharmony_ci restoreStderr 71cb0ef41Sopenharmony_ci} = require('../common/hijackstdio'); 81cb0ef41Sopenharmony_ciconst assert = require('assert'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_cifunction test1() { 111cb0ef41Sopenharmony_ci // Output is skipped if the argument to the 'warning' event is 121cb0ef41Sopenharmony_ci // not an Error object. 131cb0ef41Sopenharmony_ci hijackStderr(common.mustNotCall('stderr.write must not be called')); 141cb0ef41Sopenharmony_ci process.emit('warning', 'test'); 151cb0ef41Sopenharmony_ci setImmediate(test2); 161cb0ef41Sopenharmony_ci} 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cifunction test2() { 191cb0ef41Sopenharmony_ci // Output is skipped if it's a deprecation warning and 201cb0ef41Sopenharmony_ci // process.noDeprecation = true 211cb0ef41Sopenharmony_ci process.noDeprecation = true; 221cb0ef41Sopenharmony_ci process.emitWarning('test', 'DeprecationWarning'); 231cb0ef41Sopenharmony_ci process.noDeprecation = false; 241cb0ef41Sopenharmony_ci setImmediate(test3); 251cb0ef41Sopenharmony_ci} 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_cifunction test3() { 281cb0ef41Sopenharmony_ci restoreStderr(); 291cb0ef41Sopenharmony_ci // Type defaults to warning when the second argument is an object 301cb0ef41Sopenharmony_ci process.emitWarning('test', {}); 311cb0ef41Sopenharmony_ci process.once('warning', common.mustCall((warning) => { 321cb0ef41Sopenharmony_ci assert.strictEqual(warning.name, 'Warning'); 331cb0ef41Sopenharmony_ci })); 341cb0ef41Sopenharmony_ci setImmediate(test4); 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_cifunction test4() { 381cb0ef41Sopenharmony_ci // process.emitWarning will throw when process.throwDeprecation is true 391cb0ef41Sopenharmony_ci // and type is `DeprecationWarning`. 401cb0ef41Sopenharmony_ci process.throwDeprecation = true; 411cb0ef41Sopenharmony_ci process.once('uncaughtException', (err) => { 421cb0ef41Sopenharmony_ci assert.match(err.toString(), /^DeprecationWarning: test$/); 431cb0ef41Sopenharmony_ci }); 441cb0ef41Sopenharmony_ci try { 451cb0ef41Sopenharmony_ci process.emitWarning('test', 'DeprecationWarning'); 461cb0ef41Sopenharmony_ci } catch { 471cb0ef41Sopenharmony_ci assert.fail('Unreachable'); 481cb0ef41Sopenharmony_ci } 491cb0ef41Sopenharmony_ci process.throwDeprecation = false; 501cb0ef41Sopenharmony_ci setImmediate(test5); 511cb0ef41Sopenharmony_ci} 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_cifunction test5() { 541cb0ef41Sopenharmony_ci // Setting toString to a non-function should not cause an error 551cb0ef41Sopenharmony_ci const err = new Error('test'); 561cb0ef41Sopenharmony_ci err.toString = 1; 571cb0ef41Sopenharmony_ci process.emitWarning(err); 581cb0ef41Sopenharmony_ci setImmediate(test6); 591cb0ef41Sopenharmony_ci} 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_cifunction test6() { 621cb0ef41Sopenharmony_ci process.emitWarning('test', { detail: 'foo' }); 631cb0ef41Sopenharmony_ci process.on('warning', (warning) => { 641cb0ef41Sopenharmony_ci assert.strictEqual(warning.detail, 'foo'); 651cb0ef41Sopenharmony_ci }); 661cb0ef41Sopenharmony_ci} 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_citest1(); 69