From 0b1e844a0fced54aeacb66ba0352bc2e455507ee Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Tue, 29 Dec 2020 01:06:34 +0800 Subject: [PATCH] test: increase coverage for events 1. test EventEmitter.setMaxListeners with invalid listener count https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/events.js.html#L171 2. test EventEmitter.setMaxListeners with invalid emitter https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/events.js.html#L186 3. test getEventListeners with invalid emiiter Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/events.js.html#L706 4. test events.once with options: null Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/events.js.html#L712 5. add test case for inspect new Event() Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/event_target.js.html#L111 6. add test case for insepct new EventTarget() Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/event_target.js.html#L446 7. add test case for Event and EventTarget constructor name --- .../test-event-emitter-max-listeners.js | 14 +++++++++ test/parallel/test-events-once.js | 13 ++++++++ .../test-events-static-geteventlisteners.js | 7 +++++ test/parallel/test-eventtarget.js | 31 ++++++++++++++++++- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-event-emitter-max-listeners.js b/test/parallel/test-event-emitter-max-listeners.js index d1ac013bfb239f..a881bf86497f16 100644 --- a/test/parallel/test-event-emitter-max-listeners.js +++ b/test/parallel/test-event-emitter-max-listeners.js @@ -56,3 +56,17 @@ for (const obj of throwsObjs) { } e.emit('maxListeners'); + +{ + const { EventEmitter, defaultMaxListeners } = events; + for (const obj of throwsObjs) { + assert.throws(() => EventEmitter.setMaxListeners(obj), { + code: 'ERR_OUT_OF_RANGE', + }); + } + + assert.throws( + () => EventEmitter.setMaxListeners(defaultMaxListeners, 'INVALID_EMITTER'), + { code: 'ERR_INVALID_ARG_TYPE' } + ); +} diff --git a/test/parallel/test-events-once.js b/test/parallel/test-events-once.js index ea1963f0e58c1b..7c37f576c29fd3 100644 --- a/test/parallel/test-events-once.js +++ b/test/parallel/test-events-once.js @@ -23,6 +23,18 @@ async function onceAnEvent() { strictEqual(ee.listenerCount('myevent'), 0); } +async function onceAnEventWithNullOptions() { + const ee = new EventEmitter(); + + process.nextTick(() => { + ee.emit('myevent', 42); + }); + + const [value] = await once(ee, 'myevent', null); + strictEqual(value, 42); +} + + async function onceAnEventWithTwoArgs() { const ee = new EventEmitter(); @@ -195,6 +207,7 @@ async function eventTargetAbortSignalAfterEvent() { Promise.all([ onceAnEvent(), + onceAnEventWithNullOptions(), onceAnEventWithTwoArgs(), catchesErrors(), stopListeningAfterCatchingError(), diff --git a/test/parallel/test-events-static-geteventlisteners.js b/test/parallel/test-events-static-geteventlisteners.js index d717ff74242f90..71d9b0a28ddc75 100644 --- a/test/parallel/test-events-static-geteventlisteners.js +++ b/test/parallel/test-events-static-geteventlisteners.js @@ -4,6 +4,7 @@ const common = require('../common'); const { deepStrictEqual, + throws } = require('assert'); const { getEventListeners, EventEmitter } = require('events'); @@ -34,3 +35,9 @@ const { getEventListeners, EventEmitter } = require('events'); deepStrictEqual(getEventListeners(target, 'bar'), []); deepStrictEqual(getEventListeners(target, 'baz'), [fn1]); } + +{ + throws(() => { + getEventListeners('INVALID_EMITTER'); + }, /ERR_INVALID_ARG_TYPE/); +} diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 45c63e0dd18be3..dd3a5a106b5a01 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -13,7 +13,7 @@ const { const { once } = require('events'); -const { promisify } = require('util'); +const { promisify, inspect } = require('util'); const delay = promisify(setTimeout); // The globals are defined. @@ -541,3 +541,32 @@ let asyncTest = Promise.resolve(); et.addEventListener('foo', listener); et.dispatchEvent(new Event('foo')); } + +{ + const ev = new Event('test'); + const evConstructorName = inspect(ev, { + depth: -1 + }); + strictEqual(evConstructorName, 'Event'); + + const inspectResult = inspect(ev, { + depth: 1 + }); + ok(inspectResult.includes('Event')); +} + +{ + const et = new EventTarget(); + const inspectResult = inspect(et, { + depth: 1 + }); + ok(inspectResult.includes('EventTarget')); +} + +{ + const ev = new Event('test'); + strictEqual(ev.constructor.name, 'Event'); + + const et = new EventTarget(); + strictEqual(et.constructor.name, 'EventTarget'); +}