• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  assert.throws(
42    () => process.emitWarning('test', 'DeprecationWarning'),
43    /^DeprecationWarning: test$/);
44  process.throwDeprecation = false;
45  setImmediate(test5);
46}
47
48function test5() {
49  // Setting toString to a non-function should not cause an error
50  const err = new Error('test');
51  err.toString = 1;
52  process.emitWarning(err);
53  setImmediate(test6);
54}
55
56function test6() {
57  process.emitWarning('test', { detail: 'foo' });
58  process.on('warning', (warning) => {
59    assert.strictEqual(warning.detail, 'foo');
60  });
61}
62
63test1();
64