Skip to content

Commit

Permalink
test: improve coverage of lib/events.js
Browse files Browse the repository at this point in the history
PR-URL: #38582
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
  • Loading branch information
pd4d10 authored and richardlau committed Jul 20, 2021
1 parent cd48ee7 commit b3b5995
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/parallel/test-event-emitter-emit-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');

// Test emit called by other context
const EE = new EventEmitter();

// Works as expected if the context has no `constructor.name`
{
const ctx = Object.create(null);
assert.throws(
() => EE.emit.call(ctx, 'error', new Error('foo')),
common.expectsError({ name: 'Error', message: 'foo' })
);
}

assert.strictEqual(EE.emit.call({}, 'foo'), false);
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ async function basic() {
assert.strictEqual(ee.listenerCount('error'), 0);
}

async function invalidArgType() {
assert.throws(() => on({}, 'foo'), common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}));
}

async function error() {
const ee = new EventEmitter();
const _err = new Error('kaboom');
Expand Down Expand Up @@ -363,6 +370,7 @@ async function abortableOnAfterDone() {
async function run() {
const funcs = [
basic,
invalidArgType,
error,
errorDelayed,
throwInLoop,
Expand Down

0 comments on commit b3b5995

Please sign in to comment.