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 <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>
  • Loading branch information
edsadr authored and BridgeAR committed Apr 28, 2020
1 parent 2c364d4 commit 4169bc4
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 4169bc4

Please sign in to comment.