Skip happy-dom registration in browsers#121
Conversation
LeaVerou
left a comment
There was a problem hiding this comment.
Instead of turning all our imports into async imports, which doesn't scale, we can just import (statically) a file which checks if we have a DOM and if not, dynamic imports happy-dom and does the other stuff. Then all the complexity lives in that file, which is imported normally before other imports in every test that needs it. What am I missing?
Gate `@happy-dom/global-registrator` import + register on `typeof HTMLElement === "undefined"` so the setup is a no-op in browsers. Restructure test/index.js to await happy-dom dynamically before importing test modules, avoiding a TLA-vs-sync-sibling race on PropChangeEvent's CustomEvent base class. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
test/index.js becomes a thin boot wrapper that statically imports test/util/happy-dom.js (so its TLA completes before the body runs), then dynamic-imports test/index-fn.js (which holds the plain static imports of the test tree) and restores native CustomEvent. Matches the src/index.js ↔ src/index-fn.js convention and addresses Lea's review on #121: complexity stays in one place, the test tree uses plain static imports again. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
6ba8544 to
f9e3a29
Compare
|
Tried that first (it was actually our initial shape). The literal version — f9e3a29 keeps the spirit of your suggestion: — answered by Claude (Opus 4.7) on behalf of @DmitrySharabin |
LeaVerou
left a comment
There was a problem hiding this comment.
The thing is, tests need to be able to run independently, they shouldn't break if not loaded from the root.
Until we find a better solution I'd suggest loading it in every case and just not registering in browser environments. It's wasteful, but at least the wart then lives with that file, rather than infecting the entire testsuite.
Conditionally register happy-dom in Node only so the test entry can also load in a browser. Restructure
test/index.jsto await happy-dom before importing test modules, avoiding a TLA-vs-sync-sibling race onPropChangeEvent'sCustomEventbase.🤖 Generated with Claude Code