diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 5ece72ea1d9433..47f7b1d592a1c7 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -15,6 +15,7 @@ const { ERR_INVALID_ARG_TYPE, ERR_EVENT_RECURSION, ERR_OUT_OF_RANGE, + ERR_MISSING_ARGS } } = require('internal/errors'); @@ -45,6 +46,9 @@ class Event { constructor(type, options) { + if (arguments.length === 0) { + throw new ERR_MISSING_ARGS('type'); + } if (options != null && typeof options !== 'object') throw new ERR_INVALID_ARG_TYPE('options', 'object', options); const { cancelable, bubbles, composed } = { ...options }; diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index c0e760d13b1369..cf52030aee5a05 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -29,6 +29,7 @@ ok(EventTarget); strictEqual(ev.defaultPrevented, false); strictEqual(typeof ev.timeStamp, 'number'); + // Compatibility properties with the DOM deepStrictEqual(ev.composedPath(), []); strictEqual(ev.returnValue, true); strictEqual(ev.bubbles, false); @@ -59,7 +60,15 @@ ok(EventTarget); ev.cancelBubble = 'some-truthy-value'; strictEqual(ev.cancelBubble, true); } - +{ + // No argument behavior - throw TypeError + throws(() => { + new Event(); + }, TypeError); + // Too many arguments passed behavior - ignore additional arguments + const ev = new Event('foo', {}, {}); + strictEqual(ev.type, 'foo'); +} { const ev = new Event('foo', { cancelable: true }); strictEqual(ev.type, 'foo'); @@ -419,6 +428,6 @@ ok(EventTarget); { const target = new EventTarget(); strictEqual(target.toString(), '[object EventTarget]'); - const event = new Event(); + const event = new Event(''); strictEqual(event.toString(), '[object Event]'); }