Skip to content

Fix test events not writable#586

Closed
oluwatimio wants to merge 4 commits intomainfrom
timi/fix-test-events-not-writable
Closed

Fix test events not writable#586
oluwatimio wants to merge 4 commits intomainfrom
timi/fix-test-events-not-writable

Conversation

@oluwatimio
Copy link

@oluwatimio oluwatimio commented Jul 29, 2025

Problem

Changes were made to custom elements to use dispatchEvent https://github.com/shop/world/pull/93582 and standard DOM Events.

The issue is that these Event objects are not writable in tests

Error: Uncaught [TypeError: Cannot set property target of [object Event] which has only a getter]

Coming from remote-dom/polyfill

I asked Claude code to explain why this is happening in our test environment and here's the explanation

The issue occurs because of a mismatch between two different Event implementations:

  1. Native DOM Events (created by userEvent.click() in tests): These have
    read-only properties like target, currentTarget, and eventPhase as defined by
    the DOM specification.

  2. Polyfilled Events (from remote-dom): These define these properties as
    writable (like target: EventTarget | null = null on line 41).

When your tests run userEvent.click(), it creates a native DOM Event object,
but then the remote-dom polyfill's dispatchEvent method tries to modify
properties on that native event as if it were a polyfilled event.

The DOM specification defines these properties as read-only getters:

  • Event.target - read-only, set when the event is dispatched
  • Event.currentTarget - read-only, changes as event bubbles through DOM
  • Event.eventPhase - read-only, indicates capture/target/bubble phase

So when the polyfill code tried to do:
event.target = this; // ❌ TypeError: Cannot set property target
event.currentTarget = currentTarget; // ❌ TypeError: Cannot set property
currentTarget
event.eventPhase = phase; // ❌ TypeError: Cannot set property
eventPhase

it failed because these are read-only on native events.

The try-catch blocks now allow the polyfill to work with both types of events

  • it will set the properties on polyfilled events (where they're writable)
    and gracefully skip setting them on native events (where they're already
    correctly set by the browser).

@github-actions

This comment has been minimized.

@oluwatimio oluwatimio self-assigned this Jul 29, 2025
@oluwatimio oluwatimio closed this Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant