1// Flags: --no-warnings 2// The flag suppresses stderr output but the warning event will still emit 3'use strict'; 4 5const common = require('../common'); 6const assert = require('assert'); 7 8const testMsg = 'A Warning'; 9const testCode = 'CODE001'; 10const testDetail = 'Some detail'; 11const testType = 'CustomWarning'; 12 13process.on('warning', common.mustCall((warning) => { 14 assert(warning); 15 assert(/^(?:Warning|CustomWarning)/.test(warning.name)); 16 assert.strictEqual(warning.message, testMsg); 17 if (warning.code) assert.strictEqual(warning.code, testCode); 18 if (warning.detail) assert.strictEqual(warning.detail, testDetail); 19}, 15)); 20 21class CustomWarning extends Error { 22 constructor() { 23 super(); 24 this.name = testType; 25 this.message = testMsg; 26 this.code = testCode; 27 Error.captureStackTrace(this, CustomWarning); 28 } 29} 30 31[ 32 [testMsg], 33 [testMsg, testType], 34 [testMsg, CustomWarning], 35 [testMsg, testType, CustomWarning], 36 [testMsg, testType, testCode], 37 [testMsg, { type: testType }], 38 [testMsg, { type: testType, code: testCode }], 39 [testMsg, { type: testType, code: testCode, detail: testDetail }], 40 [new CustomWarning()], 41 // Detail will be ignored for the following. No errors thrown 42 [testMsg, { type: testType, code: testCode, detail: true }], 43 [testMsg, { type: testType, code: testCode, detail: [] }], 44 [testMsg, { type: testType, code: testCode, detail: null }], 45 [testMsg, { type: testType, code: testCode, detail: 1 }], 46].forEach((args) => { 47 process.emitWarning(...args); 48}); 49 50const warningNoToString = new CustomWarning(); 51warningNoToString.toString = null; 52process.emitWarning(warningNoToString); 53 54const warningThrowToString = new CustomWarning(); 55warningThrowToString.toString = function() { 56 throw new Error('invalid toString'); 57}; 58process.emitWarning(warningThrowToString); 59 60// TypeError is thrown on invalid input 61[ 62 [1], 63 [{}], 64 [true], 65 [[]], 66 ['', '', {}], 67 ['', 1], 68 ['', '', 1], 69 ['', true], 70 ['', '', true], 71 ['', []], 72 ['', '', []], 73 [], 74 [undefined, 'foo', 'bar'], 75 [undefined], 76].forEach((args) => { 77 assert.throws( 78 () => process.emitWarning(...args), 79 { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' } 80 ); 81}); 82