test: improve coverage of lib/events.js

PR-URL: https://github.com/nodejs/node/pull/38582
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
This commit is contained in:
Rongjian Zhang authored and Node.js GitHub Bot committed 2021-06-11 02:35:14 +00:00
1 parent bf2473d384
commit a596fdf4d6
2 files changed
+26

No files matched your search

@@ -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);
@@ -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');
@@ -359,6 +366,7 @@ async function abortableOnAfterDone() {
async function run() {
const funcs = [
basic,
invalidArgType,
error,
errorDelayed,
throwInLoop,