Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase coverage for events #36668

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/parallel/test-event-emitter-max-listeners.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
}