diff --git a/src/runtime/internal/Component.ts b/src/runtime/internal/Component.ts
index 3e48010547..0e9e4f0610 100644
--- a/src/runtime/internal/Component.ts
+++ b/src/runtime/internal/Component.ts
@@ -212,6 +212,9 @@ if (typeof HTMLElement === 'function') {
$on(type, callback) {
// TODO should this delegate to addEventListener?
+ if (!is_function(callback)) {
+ return noop;
+ }
const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = []));
callbacks.push(callback);
@@ -244,6 +247,9 @@ export class SvelteComponent {
}
$on(type, callback) {
+ if (!is_function(callback)) {
+ return noop;
+ }
const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = []));
callbacks.push(callback);
diff --git a/test/runtime/samples/component-events-nullish/Widget.svelte b/test/runtime/samples/component-events-nullish/Widget.svelte
new file mode 100644
index 0000000000..07ae574403
--- /dev/null
+++ b/test/runtime/samples/component-events-nullish/Widget.svelte
@@ -0,0 +1,14 @@
+
+
\ No newline at end of file
diff --git a/test/runtime/samples/component-events-nullish/_config.js b/test/runtime/samples/component-events-nullish/_config.js
new file mode 100644
index 0000000000..7c540b2a08
--- /dev/null
+++ b/test/runtime/samples/component-events-nullish/_config.js
@@ -0,0 +1,9 @@
+export default {
+ async test({ assert, component, window, target }) {
+ const event = new window.MouseEvent('click');
+ const button = target.querySelector('button');
+
+ await button.dispatchEvent(event);
+ assert.equal(component.logs.length, 0);
+ }
+};
diff --git a/test/runtime/samples/component-events-nullish/main.svelte b/test/runtime/samples/component-events-nullish/main.svelte
new file mode 100644
index 0000000000..0dee33b5d1
--- /dev/null
+++ b/test/runtime/samples/component-events-nullish/main.svelte
@@ -0,0 +1,6 @@
+
+
+