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 <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Zijian Liu <[email protected]>
  • Loading branch information
pd4d10 authored and nodejs-github-bot committed Jun 11, 2021
1 parent bf2473d commit a596fdf
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 @@ -35,6 +35,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 @@ -359,6 +366,7 @@ async function abortableOnAfterDone() {
async function run() {
const funcs = [
basic,
invalidArgType,
error,
errorDelayed,
throwInLoop,
Expand Down

0 comments on commit a596fdf

Please sign in to comment.