mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
test: refactor events tests for invalid listeners
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
30c2b0f798
commit
45e188b2e3
5 files changed
+20
-44
No files matched your search
@@ -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'
|
||||
});
|
||||
@@ -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`);
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -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 = () => {};
|
||||
|
||||
Reference in new issue
Block a user