Skip to content

Commit

Permalink
test: refactor events tests for invalid listeners
Browse files Browse the repository at this point in the history
PR-URL: #32769
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Gerhard Stöbich <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
  • Loading branch information
edsadr authored and BethGriggs committed Apr 27, 2020
1 parent e9b5692 commit ee98ab5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 44 deletions.
11 changes: 0 additions & 11 deletions test/parallel/test-event-emitter-add-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,3 @@ const EventEmitter = require('events');
// listeners were added.
assert.deepStrictEqual(ee.listeners('hello'), [listen2, listen1]);
}

// Verify that the listener must be a function
assert.throws(() => {
const ee = new EventEmitter();
ee.on('foo', null);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "listener" argument must be of type function. ' +
'Received null'
});
20 changes: 20 additions & 0 deletions test/parallel/test-event-emitter-invalid-listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

require('../common');
const assert = require('assert');
const EventEmitter = require('events');

const eventsMethods = ['on', 'once', 'removeListener', 'prependOnceListener'];

// Verify that the listener must be a function for events methods
for (const method of eventsMethods) {
assert.throws(() => {
const ee = new EventEmitter();
ee[method]('foo', null);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "listener" argument must be of type function. ' +
'Received null'
}, `event.${method}('foo', null) should throw the proper error`);
}
11 changes: 0 additions & 11 deletions test/parallel/test-event-emitter-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ e.once('e', common.mustCall());

e.emit('e');

// Verify that the listener must be a function
assert.throws(() => {
const ee = new EventEmitter();
ee.once('foo', null);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "listener" argument must be of type function. ' +
'Received null'
});

{
// once() has different code paths based on the number of arguments being
// emitted. Verify that all of the cases are covered.
Expand Down
11 changes: 0 additions & 11 deletions test/parallel/test-event-emitter-prepend.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ myEE.prependOnceListener('foo',

myEE.emit('foo');

// Verify that the listener must be a function
assert.throws(() => {
const ee = new EventEmitter();
ee.prependOnceListener('foo', null);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "listener" argument must be of type function. ' +
'Received null'
});

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

Expand Down
11 changes: 0 additions & 11 deletions test/parallel/test-event-emitter-remove-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,6 @@ function listener2() {}
assert.deepStrictEqual(ee, ee.removeListener('foo', () => {}));
}

// Verify that the removed listener must be a function
assert.throws(() => {
const ee = new EventEmitter();
ee.removeListener('foo', null);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "listener" argument must be of type function. ' +
'Received null'
});

{
const ee = new EventEmitter();
const listener = () => {};
Expand Down

0 comments on commit ee98ab5

Please sign in to comment.