From 45e188b2e3d64bdc8e96396e27db73209f116ae1 Mon Sep 17 00:00:00 2001 From: Adrian Estrada Date: Sat, 11 Apr 2020 13:10:10 -0500 Subject: [PATCH] test: refactor events tests for invalid listeners MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/32769 Reviewed-By: Anna Henningsen Reviewed-By: Gireesh Punathil Reviewed-By: Gerhard Stöbich Reviewed-By: Richard Lau --- .../test-event-emitter-add-listeners.js | 11 ---------- .../test-event-emitter-invalid-listener.js | 20 +++++++++++++++++++ test/parallel/test-event-emitter-once.js | 11 ---------- test/parallel/test-event-emitter-prepend.js | 11 ---------- .../test-event-emitter-remove-listeners.js | 11 ---------- 5 files changed, 20 insertions(+), 44 deletions(-) create mode 100644 test/parallel/test-event-emitter-invalid-listener.js diff --git a/test/parallel/test-event-emitter-add-listeners.js b/test/parallel/test-event-emitter-add-listeners.js index 4b0305c176bd14..f42d1f24878483 100644 --- a/test/parallel/test-event-emitter-add-listeners.js +++ b/test/parallel/test-event-emitter-add-listeners.js @@ -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' -}); diff --git a/test/parallel/test-event-emitter-invalid-listener.js b/test/parallel/test-event-emitter-invalid-listener.js new file mode 100644 index 00000000000000..3070d4ed7517de --- /dev/null +++ b/test/parallel/test-event-emitter-invalid-listener.js @@ -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`); +} diff --git a/test/parallel/test-event-emitter-once.js b/test/parallel/test-event-emitter-once.js index 1ad2de1da556bf..983f6141b9dc4c 100644 --- a/test/parallel/test-event-emitter-once.js +++ b/test/parallel/test-event-emitter-once.js @@ -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. diff --git a/test/parallel/test-event-emitter-prepend.js b/test/parallel/test-event-emitter-prepend.js index c65b6b8f780f5c..ffe8544911365c 100644 --- a/test/parallel/test-event-emitter-prepend.js +++ b/test/parallel/test-event-emitter-prepend.js @@ -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'); diff --git a/test/parallel/test-event-emitter-remove-listeners.js b/test/parallel/test-event-emitter-remove-listeners.js index 91e1f071046ac1..f37d26eb258c23 100644 --- a/test/parallel/test-event-emitter-remove-listeners.js +++ b/test/parallel/test-event-emitter-remove-listeners.js @@ -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 = () => {};