Skip to content

Commit 5bc130b

Browse files
Lxxyxdanielleadams
authored andcommitted
test: increase coverage for events
1. test EventEmitter.setMaxListeners with invalid listener count https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/events.js.html#L171 2. test EventEmitter.setMaxListeners with invalid emitter https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/events.js.html#L186 3. test getEventListeners with invalid emiiter Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/events.js.html#L706 4. test events.once with options: null Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/events.js.html#L712 5. add test case for inspect new Event() Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/event_target.js.html#L111 6. add test case for insepct new EventTarget() Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/event_target.js.html#L446 7. add test case for Event and EventTarget constructor name PR-URL: #36668 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent b764269 commit 5bc130b

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

‎test/parallel/test-event-emitter-max-listeners.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ for (const obj of throwsObjs){
5656
}
5757

5858
e.emit('maxListeners');
59+
60+
{
61+
const{ EventEmitter, defaultMaxListeners }=events;
62+
for(constobjofthrowsObjs){
63+
assert.throws(()=>EventEmitter.setMaxListeners(obj),{
64+
code: 'ERR_OUT_OF_RANGE',
65+
});
66+
}
67+
68+
assert.throws(
69+
()=>EventEmitter.setMaxListeners(defaultMaxListeners,'INVALID_EMITTER'),
70+
{code: 'ERR_INVALID_ARG_TYPE'}
71+
);
72+
}

‎test/parallel/test-events-once.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ async function onceAnEvent(){
2323
strictEqual(ee.listenerCount('myevent'),0);
2424
}
2525

26+
asyncfunctiononceAnEventWithNullOptions(){
27+
constee=newEventEmitter();
28+
29+
process.nextTick(()=>{
30+
ee.emit('myevent',42);
31+
});
32+
33+
const[value]=awaitonce(ee,'myevent',null);
34+
strictEqual(value,42);
35+
}
36+
37+
2638
asyncfunctiononceAnEventWithTwoArgs(){
2739
constee=newEventEmitter();
2840

@@ -195,6 +207,7 @@ async function eventTargetAbortSignalAfterEvent(){
195207

196208
Promise.all([
197209
onceAnEvent(),
210+
onceAnEventWithNullOptions(),
198211
onceAnEventWithTwoArgs(),
199212
catchesErrors(),
200213
stopListeningAfterCatchingError(),

‎test/parallel/test-events-static-geteventlisteners.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const common = require('../common');
44

55
const{
66
deepStrictEqual,
7+
throws
78
}=require('assert');
89

910
const{ getEventListeners, EventEmitter }=require('events');
@@ -34,3 +35,9 @@ const{getEventListeners, EventEmitter } = require('events');
3435
deepStrictEqual(getEventListeners(target,'bar'),[]);
3536
deepStrictEqual(getEventListeners(target,'baz'),[fn1]);
3637
}
38+
39+
{
40+
throws(()=>{
41+
getEventListeners('INVALID_EMITTER');
42+
},/ERR_INVALID_ARG_TYPE/);
43+
}

‎test/parallel/test-eventtarget.js‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const{
1313

1414
const{ once }=require('events');
1515

16-
const{ promisify }=require('util');
16+
const{ promisify, inspect}=require('util');
1717
constdelay=promisify(setTimeout);
1818

1919
// The globals are defined.
@@ -541,3 +541,32 @@ let asyncTest = Promise.resolve();
541541
et.addEventListener('foo',listener);
542542
et.dispatchEvent(newEvent('foo'));
543543
}
544+
545+
{
546+
constev=newEvent('test');
547+
constevConstructorName=inspect(ev,{
548+
depth: -1
549+
});
550+
strictEqual(evConstructorName,'Event');
551+
552+
constinspectResult=inspect(ev,{
553+
depth: 1
554+
});
555+
ok(inspectResult.includes('Event'));
556+
}
557+
558+
{
559+
constet=newEventTarget();
560+
constinspectResult=inspect(et,{
561+
depth: 1
562+
});
563+
ok(inspectResult.includes('EventTarget'));
564+
}
565+
566+
{
567+
constev=newEvent('test');
568+
strictEqual(ev.constructor.name,'Event');
569+
570+
constet=newEventTarget();
571+
strictEqual(et.constructor.name,'EventTarget');
572+
}

0 commit comments

Comments
(0)