diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 763542eb0fc2fa..133edc23b2423a 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'); @@ -44,6 +45,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 99d717abda6e8d..82a89caae1fea4 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); @@ -40,7 +41,15 @@ ok(EventTarget); ev.preventDefault(); strictEqual(ev.defaultPrevented, false); } - +{ + // 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');