|
1 | 1 | /* eslint-disable required-modules */
|
2 |
| -// ordinarily test files must require('common') but that action causes |
3 |
| -// the global console to be compiled, defeating the purpose of this test |
4 | 2 |
|
5 | 3 | 'use strict';
|
6 | 4 |
|
7 |
| -const common = require('../common'); |
| 5 | +// Ordinarily test files must require('common') but that action causes |
| 6 | +// the global console to be compiled, defeating the purpose of this test. |
| 7 | + |
8 | 8 | const assert = require('assert');
|
9 | 9 | const EventEmitter = require('events');
|
10 | 10 | const leakWarning = /EventEmitter memory leak detected\. 2 hello listeners/;
|
11 | 11 |
|
12 |
| -common.hijackStderr(common.mustCall(function(data) { |
13 |
| - if (process.stderr.writeTimes === 0) { |
14 |
| - assert.ok(leakWarning.test(data)); |
15 |
| - } else { |
16 |
| - assert.fail('stderr.write should be called only once'); |
17 |
| - } |
18 |
| -})); |
19 |
| - |
20 |
| -process.on('warning', function(warning) { |
| 12 | +let writeTimes = 0; |
| 13 | +let warningTimes = 0; |
| 14 | +process.on('warning', () => { |
21 | 15 | // This will be called after the default internal
|
22 | 16 | // process warning handler is called. The default
|
23 | 17 | // process warning writes to the console, which will
|
24 | 18 | // invoke the monkeypatched process.stderr.write
|
25 | 19 | // below.
|
26 |
| - assert.strictEqual(process.stderr.writeTimes, 1); |
| 20 | + assert.strictEqual(writeTimes, 1); |
27 | 21 | EventEmitter.defaultMaxListeners = oldDefault;
|
28 |
| - // when we get here, we should be done |
| 22 | + warningTimes++; |
| 23 | +}); |
| 24 | + |
| 25 | +process.on('exit', () => { |
| 26 | + assert.strictEqual(warningTimes, 1); |
29 | 27 | });
|
30 | 28 |
|
| 29 | +process.stderr.write = (data) => { |
| 30 | + if (writeTimes === 0) |
| 31 | + assert.ok(leakWarning.test(data)); |
| 32 | + else |
| 33 | + assert.fail('stderr.write should be called only once'); |
| 34 | + |
| 35 | + writeTimes++; |
| 36 | +}; |
| 37 | + |
31 | 38 | const oldDefault = EventEmitter.defaultMaxListeners;
|
32 | 39 | EventEmitter.defaultMaxListeners = 1;
|
33 | 40 |
|
34 | 41 | const e = new EventEmitter();
|
35 |
| -e.on('hello', common.noop); |
36 |
| -e.on('hello', common.noop); |
| 42 | +e.on('hello', () => {}); |
| 43 | +e.on('hello', () => {}); |
37 | 44 |
|
38 | 45 | // TODO: Figure out how to validate console. Currently,
|
39 | 46 | // there is no obvious way of validating that console
|
|
0 commit comments