Skip to content

Commit 6c30ddd

Browse files
cjihrigFishrock123
authored andcommitted
test: exercise EE function type checking paths
This commit adds tests for on(), once(), removeListener(), and prependOnceListener(), which all throw a TypeError if the listener argument is not a function. PR-URL: #8168 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent c7771e6 commit 6c30ddd

File tree

4 files changed

+88
-63
lines changed

4 files changed

+88
-63
lines changed
Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,72 @@
11
'use strict';
2-
require('../common');
3-
var assert = require('assert');
4-
var events = require('events');
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const EventEmitter = require('events');
55

6-
var e = new events.EventEmitter();
6+
{
7+
const ee = new EventEmitter();
8+
const events_new_listener_emited = [];
9+
const listeners_new_listener_emited = [];
710

8-
var events_new_listener_emited = [];
9-
var listeners_new_listener_emited = [];
10-
var times_hello_emited = 0;
11+
// Sanity check
12+
assert.strictEqual(ee.addListener, ee.on);
1113

12-
// sanity check
13-
assert.equal(e.addListener, e.on);
14+
ee.on('newListener', function(event, listener) {
15+
// Don't track newListener listeners.
16+
if (event === 'newListener')
17+
return;
1418

15-
e.on('newListener', function(event, listener) {
16-
if (event === 'newListener')
17-
return; // Don't track our adding of newListener listeners.
18-
console.log('newListener: ' + event);
19-
events_new_listener_emited.push(event);
20-
listeners_new_listener_emited.push(listener);
21-
});
22-
23-
function hello(a, b) {
24-
console.log('hello');
25-
times_hello_emited += 1;
26-
assert.equal('a', a);
27-
assert.equal('b', b);
28-
}
29-
e.once('newListener', function(name, listener) {
30-
assert.equal(name, 'hello');
31-
assert.equal(listener, hello);
32-
assert.deepStrictEqual(this.listeners('hello'), []);
33-
});
34-
e.on('hello', hello);
19+
events_new_listener_emited.push(event);
20+
listeners_new_listener_emited.push(listener);
21+
});
3522

36-
var foo = function() {};
37-
e.once('foo', foo);
23+
const hello = common.mustCall(function(a, b) {
24+
assert.strictEqual('a', a);
25+
assert.strictEqual('b', b);
26+
});
3827

39-
console.log('start');
28+
ee.once('newListener', function(name, listener) {
29+
assert.strictEqual(name, 'hello');
30+
assert.strictEqual(listener, hello);
31+
assert.deepStrictEqual(this.listeners('hello'), []);
32+
});
4033

41-
e.emit('hello', 'a', 'b');
34+
ee.on('hello', hello);
35+
ee.once('foo', common.fail);
36+
assert.deepStrictEqual(['hello', 'foo'], events_new_listener_emited);
37+
assert.deepStrictEqual([hello, common.fail], listeners_new_listener_emited);
4238

39+
ee.emit('hello', 'a', 'b');
40+
}
4341

4442
// just make sure that this doesn't throw:
45-
var f = new events.EventEmitter();
46-
f.setMaxListeners(0);
43+
{
44+
const f = new EventEmitter();
4745

46+
f.setMaxListeners(0);
47+
}
4848

49-
process.on('exit', function() {
50-
assert.deepStrictEqual(['hello', 'foo'], events_new_listener_emited);
51-
assert.deepStrictEqual([hello, foo], listeners_new_listener_emited);
52-
assert.equal(1, times_hello_emited);
53-
});
49+
{
50+
const listen1 = function listen1() {};
51+
const listen2 = function listen2() {};
52+
const ee = new EventEmitter();
5453

55-
var listen1 = function listen1() {};
56-
var listen2 = function listen2() {};
57-
var e1 = new events.EventEmitter();
58-
e1.once('newListener', function() {
59-
assert.deepStrictEqual(e1.listeners('hello'), []);
60-
e1.once('newListener', function() {
61-
assert.deepStrictEqual(e1.listeners('hello'), []);
54+
ee.once('newListener', function() {
55+
assert.deepStrictEqual(ee.listeners('hello'), []);
56+
ee.once('newListener', function() {
57+
assert.deepStrictEqual(ee.listeners('hello'), []);
58+
});
59+
ee.on('hello', listen2);
6260
});
63-
e1.on('hello', listen2);
64-
});
65-
e1.on('hello', listen1);
66-
// The order of listeners on an event is not always the order in which the
67-
// listeners were added.
68-
assert.deepStrictEqual(e1.listeners('hello'), [listen2, listen1]);
61+
ee.on('hello', listen1);
62+
// The order of listeners on an event is not always the order in which the
63+
// listeners were added.
64+
assert.deepStrictEqual(ee.listeners('hello'), [listen2, listen1]);
65+
}
66+
67+
// Verify that the listener must be a function
68+
assert.throws(() => {
69+
const ee = new EventEmitter();
70+
71+
ee.on('foo', null);
72+
}, /^TypeError: "listener" argument must be a function$/);

test/parallel/test-event-emitter-once.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22
const common = require('../common');
3-
var events = require('events');
3+
const assert = require('assert');
4+
const EventEmitter = require('events');
45

5-
var e = new events.EventEmitter();
6+
const e = new EventEmitter();
67

78
e.once('hello', common.mustCall(function(a, b) {}));
89

@@ -26,3 +27,10 @@ e.once('e', common.mustCall(function() {
2627
e.once('e', common.mustCall(function() {}));
2728

2829
e.emit('e');
30+
31+
// Verify that the listener must be a function
32+
assert.throws(() => {
33+
const ee = new EventEmitter();
34+
35+
ee.once('foo', null);
36+
}, /^TypeError: "listener" argument must be a function$/);

test/parallel/test-event-emitter-prepend.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ myEE.prependOnceListener('foo', common.mustCall(() => assert.equal(m++, 0)));
1717

1818
myEE.emit('foo');
1919

20+
// Verify that the listener must be a function
21+
assert.throws(() => {
22+
const ee = new EventEmitter();
23+
24+
ee.prependOnceListener('foo', null);
25+
}, /^TypeError: "listener" argument must be a function$/);
2026

2127
// Test fallback if prependListener is undefined.
2228
const stream = require('stream');

test/parallel/test-event-emitter-remove-listeners.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const events = require('events');
4+
const EventEmitter = require('events');
55

66
function listener1() {}
77
function listener2() {}
88

99
{
10-
const ee = new events.EventEmitter();
10+
const ee = new EventEmitter();
1111
ee.on('hello', listener1);
1212
ee.on('removeListener', common.mustCall((name, cb) => {
1313
assert.strictEqual(name, 'hello');
@@ -18,15 +18,15 @@ function listener2() {}
1818
}
1919

2020
{
21-
const ee = new events.EventEmitter();
21+
const ee = new EventEmitter();
2222
ee.on('hello', listener1);
2323
ee.on('removeListener', common.fail);
2424
ee.removeListener('hello', listener2);
2525
assert.deepStrictEqual([listener1], ee.listeners('hello'));
2626
}
2727

2828
{
29-
const ee = new events.EventEmitter();
29+
const ee = new EventEmitter();
3030
ee.on('hello', listener1);
3131
ee.on('hello', listener2);
3232
ee.once('removeListener', common.mustCall((name, cb) => {
@@ -46,7 +46,7 @@ function listener2() {}
4646
}
4747

4848
{
49-
const ee = new events.EventEmitter();
49+
const ee = new EventEmitter();
5050

5151
function remove1() {
5252
common.fail('remove1 should not have been called');
@@ -67,7 +67,7 @@ function listener2() {}
6767
}
6868

6969
{
70-
const ee = new events.EventEmitter();
70+
const ee = new EventEmitter();
7171
ee.on('hello', listener1);
7272
ee.on('hello', listener2);
7373
ee.once('removeListener', common.mustCall((name, cb) => {
@@ -87,7 +87,7 @@ function listener2() {}
8787
}
8888

8989
{
90-
const ee = new events.EventEmitter();
90+
const ee = new EventEmitter();
9191
const listener3 = common.mustCall(() => {
9292
ee.removeListener('hello', listener4);
9393
}, 2);
@@ -106,7 +106,7 @@ function listener2() {}
106106
}
107107

108108
{
109-
const ee = new events.EventEmitter();
109+
const ee = new EventEmitter();
110110

111111
ee.once('hello', listener1);
112112
ee.on('removeListener', common.mustCall((eventName, listener) => {
@@ -115,3 +115,10 @@ function listener2() {}
115115
}));
116116
ee.emit('hello');
117117
}
118+
119+
// Verify that the removed listener must be a function
120+
assert.throws(() => {
121+
const ee = new EventEmitter();
122+
123+
ee.removeListener('foo', null);
124+
}, /^TypeError: "listener" argument must be a function$/);

0 commit comments

Comments
 (0)