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