From a596fdf4d6152c0bdf43417fd63684ccfcfcd098 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Fri, 7 May 2021 21:49:07 +0800 Subject: [PATCH] test: improve coverage of lib/events.js PR-URL: https://github.com/nodejs/node/pull/38582 Reviewed-By: James M Snell Reviewed-By: Rich Trott Reviewed-By: Zijian Liu --- .../test-event-emitter-emit-context.js | 18 ++++++++++++++++++ ...tor.js => test-events-on-async-iterator.js} | 8 ++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/parallel/test-event-emitter-emit-context.js rename test/parallel/{test-event-on-async-iterator.js => test-events-on-async-iterator.js} (97%) diff --git a/test/parallel/test-event-emitter-emit-context.js b/test/parallel/test-event-emitter-emit-context.js new file mode 100644 index 00000000000000..82e4b595185a59 --- /dev/null +++ b/test/parallel/test-event-emitter-emit-context.js @@ -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); diff --git a/test/parallel/test-event-on-async-iterator.js b/test/parallel/test-events-on-async-iterator.js similarity index 97% rename from test/parallel/test-event-on-async-iterator.js rename to test/parallel/test-events-on-async-iterator.js index 192326e12bcc39..dbd27a8a44693e 100644 --- a/test/parallel/test-event-on-async-iterator.js +++ b/test/parallel/test-events-on-async-iterator.js @@ -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,