diff --git a/lib/events.js b/lib/events.js index 89d32d681908c1..d61c7c1e40d844 100644 --- a/lib/events.js +++ b/lib/events.js @@ -845,7 +845,7 @@ function listenerCount(type, listener) { if (typeof evlistener === 'function') { if (listener != null) { - return listener === evlistener ? 1 : 0; + return listener === evlistener || listener === evlistener.listener ? 1 : 0; } return 1; diff --git a/test/parallel/test-events-listener-count-with-listener.js b/test/parallel/test-events-listener-count-with-listener.js index 080ce6d704513e..e269a319074aca 100644 --- a/test/parallel/test-events-listener-count-with-listener.js +++ b/test/parallel/test-events-listener-count-with-listener.js @@ -12,6 +12,18 @@ assert.strictEqual(EE.listenerCount('event'), 0); assert.strictEqual(EE.listenerCount('event', handler), 0); assert.strictEqual(EE.listenerCount('event', anotherHandler), 0); +EE.once('event', handler); + +assert.strictEqual(EE.listenerCount('event'), 1); +assert.strictEqual(EE.listenerCount('event', handler), 1); +assert.strictEqual(EE.listenerCount('event', anotherHandler), 0); + +EE.removeAllListeners('event') + +assert.strictEqual(EE.listenerCount('event'), 0); +assert.strictEqual(EE.listenerCount('event', handler), 0); +assert.strictEqual(EE.listenerCount('event', anotherHandler), 0); + EE.on('event', handler); assert.strictEqual(EE.listenerCount('event'), 1);