fix: preserve native dispatchEvent for Deno compatibility#7
Merged
Conversation
Replace GlobalRegistrator.register() with new GlobalWindow() from happy-dom, then selectively install DOM globals while preserving any existing dispatchEvent, Event, and CustomEvent on globalThis. The old approach called GlobalRegistrator.register() which unconditionally overwrites globalThis.dispatchEvent with Happy DOM's implementation. Deno's runtime dispatches a native 'beforeunload' event on process exit using its own Event constructor; Happy DOM's dispatchEvent rejects it with a TypeError because the event is not an instance of Happy DOM's internal Event class. The fix saves the runtime's native event primitives before setting up the Happy DOM window and restores them afterward. This leaves Happy DOM's DOM APIs (document, HTMLElement, etc.) fully functional while keeping Deno's shutdown path intact. Also removes @happy-dom/global-registrator from peerDependencies since it is no longer used.
This was referenced Apr 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When running tests with Happy DOM under Deno, the process exits with:
Deno's runtime dispatches a native
beforeunloadevent on process exit. The currentdom-env.tsusesGlobalRegistrator.register()which unconditionally overwritesglobalThis.dispatchEventwith Happy DOM's version. Happy DOM's implementation validates that the event argument is an instance of its own internalEventclass — it rejects Deno's nativeEventwith aTypeError.Fix
Replace
GlobalRegistrator.register()withnew GlobalWindow()fromhappy-dom, then install each DOM global individually. Before install, save any existingdispatchEvent,Event, andCustomEventfromglobalThisand restore them after, so Deno's shutdown path remains intact.Changes
src/dom-env.ts: replaceGlobalRegistrator.register()withnew GlobalWindow()+ selective global install, preserving nativedispatchEvent/Event/CustomEventpackage.json: remove@happy-dom/global-registratorfrompeerDependenciesandpeerDependenciesMeta— it is no longer used;happy-dom(already an optional peer) is sufficientTesting
All three runtimes pass: Node 3/3, Bun 3/3, Deno 3/3.