Skip to content

Commit 45e188b

Browse files
edsadrBethGriggs
authored andcommitted
test: refactor events tests for invalid listeners
PR-URL: #32769 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent 30c2b0f commit 45e188b

5 files changed

+20
-44
lines changed

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

-11
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,3 @@ const EventEmitter = require('events');
8484
// listeners were added.
8585
assert.deepStrictEqual(ee.listeners('hello'), [listen2, listen1]);
8686
}
87-
88-
// Verify that the listener must be a function
89-
assert.throws(() => {
90-
const ee = new EventEmitter();
91-
ee.on('foo', null);
92-
}, {
93-
code: 'ERR_INVALID_ARG_TYPE',
94-
name: 'TypeError',
95-
message: 'The "listener" argument must be of type function. ' +
96-
'Received null'
97-
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const EventEmitter = require('events');
6+
7+
const eventsMethods = ['on', 'once', 'removeListener', 'prependOnceListener'];
8+
9+
// Verify that the listener must be a function for events methods
10+
for (const method of eventsMethods) {
11+
assert.throws(() => {
12+
const ee = new EventEmitter();
13+
ee[method]('foo', null);
14+
}, {
15+
code: 'ERR_INVALID_ARG_TYPE',
16+
name: 'TypeError',
17+
message: 'The "listener" argument must be of type function. ' +
18+
'Received null'
19+
}, `event.${method}('foo', null) should throw the proper error`);
20+
}

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

-11
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ e.once('e', common.mustCall());
4949

5050
e.emit('e');
5151

52-
// Verify that the listener must be a function
53-
assert.throws(() => {
54-
const ee = new EventEmitter();
55-
ee.once('foo', null);
56-
}, {
57-
code: 'ERR_INVALID_ARG_TYPE',
58-
name: 'TypeError',
59-
message: 'The "listener" argument must be of type function. ' +
60-
'Received null'
61-
});
62-
6352
{
6453
// once() has different code paths based on the number of arguments being
6554
// emitted. Verify that all of the cases are covered.

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

-11
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ myEE.prependOnceListener('foo',
1818

1919
myEE.emit('foo');
2020

21-
// Verify that the listener must be a function
22-
assert.throws(() => {
23-
const ee = new EventEmitter();
24-
ee.prependOnceListener('foo', null);
25-
}, {
26-
code: 'ERR_INVALID_ARG_TYPE',
27-
name: 'TypeError',
28-
message: 'The "listener" argument must be of type function. ' +
29-
'Received null'
30-
});
31-
3221
// Test fallback if prependListener is undefined.
3322
const stream = require('stream');
3423

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

-11
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,6 @@ function listener2() {}
144144
assert.deepStrictEqual(ee, ee.removeListener('foo', () => {}));
145145
}
146146

147-
// Verify that the removed listener must be a function
148-
assert.throws(() => {
149-
const ee = new EventEmitter();
150-
ee.removeListener('foo', null);
151-
}, {
152-
code: 'ERR_INVALID_ARG_TYPE',
153-
name: 'TypeError',
154-
message: 'The "listener" argument must be of type function. ' +
155-
'Received null'
156-
});
157-
158147
{
159148
const ee = new EventEmitter();
160149
const listener = () => {};

0 commit comments

Comments
 (0)