Skip to content

Commit

Permalink
test: increase coverage for events
Browse files Browse the repository at this point in the history
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

PR-URL: #36668
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
Lxxyx authored and nodejs-github-bot committed Jan 4, 2021
1 parent a19af5e commit 7c76762
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
14 changes: 14 additions & 0 deletions test/parallel/test-event-emitter-max-listeners.js
Expand Up @@ -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' }
);
}
13 changes: 13 additions & 0 deletions test/parallel/test-events-once.js
Expand Up @@ -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();

Expand Down Expand Up @@ -195,6 +207,7 @@ async function eventTargetAbortSignalAfterEvent() {

Promise.all([
onceAnEvent(),
onceAnEventWithNullOptions(),
onceAnEventWithTwoArgs(),
catchesErrors(),
stopListeningAfterCatchingError(),
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-events-static-geteventlisteners.js
Expand Up @@ -4,6 +4,7 @@ const common = require('../common');

const {
deepStrictEqual,
throws
} = require('assert');

const { getEventListeners, EventEmitter } = require('events');
Expand Down Expand Up @@ -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/);
}
31 changes: 30 additions & 1 deletion test/parallel/test-eventtarget.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
}

0 comments on commit 7c76762

Please sign in to comment.