forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-global-console-exists.js
More file actions
47 lines (37 loc) · 1.32 KB
/
Copy pathtest-global-console-exists.js
File metadata and controls
47 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* eslint-disable node-core/required-modules */
'use strict';
// Ordinarily test files must require('common') but that action causes
// the global console to be compiled, defeating the purpose of this test.
const assert = require('assert');
const EventEmitter = require('events');
const leakWarning = /EventEmitter memory leak detected\. 2 hello listeners/;
let writeTimes = 0;
let warningTimes = 0;
process.on('warning', () => {
// This will be called after the default internal
// process warning handler is called. The default
// process warning writes to the console, which will
// invoke the monkeypatched process.stderr.write
// below.
assert.strictEqual(writeTimes, 1);
EventEmitter.setDefaultMaxListeners(oldDefault);
warningTimes++;
});
process.on('exit', () => {
assert.strictEqual(warningTimes, 1);
});
process.stderr.write = (data) => {
if (writeTimes === 0)
assert.ok(leakWarning.test(data));
else
assert.fail('stderr.write should be called only once');
writeTimes++;
};
const oldDefault = EventEmitter.getDefaultMaxListeners();
EventEmitter.setDefaultMaxListeners(1);
const e = new EventEmitter();
e.on('hello', () => {});
e.on('hello', () => {});
// TODO: Figure out how to validate console. Currently,
// there is no obvious way of validating that console
// exists here exactly when it should.