You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to be able to inject the document to use for my render() to allow me to use happy-dom or JSDOM to test my Preact components without assigning a global document.
Currently you must assign it to the globalThis object like so:
import{h,render}from'preact'// Imagine this is a mock Windowconstbrowser=newBrowser(`<body></body>`)// Assign itto the global context so Preact can use themglobalThis.document=browser.document// Run the preact renderrender(<div>Hi</div>,browser.document.body)
However this prevents me from being able to run the tests concurrently
I have tried to use Node's node:vm module to alter the globalThis context, however this does not work as it doesn't have access to imports.
I have tried bundling the import and using that as a script evaluation in the mock browser, however this is slow and cumbersome, negating the benefits of making tests concurrent.
alshdavid
changed the title
Add an option to inject the window/document context for a render()
Add an option to inject the document for a render()Sep 16, 2024
The Problem
I'd like to be able to inject the
document
to use for myrender()
to allow me to use happy-dom or JSDOM to test my Preact components without assigning a global document.Currently you must assign it to the
globalThis
object like so:However this prevents me from being able to run the tests concurrently
What I've Tried
I have tried to use Node's
node:vm
module to alter the globalThis context, however this does not work as it doesn't have access to imports.I have tried bundling the import and using that as a script evaluation in the mock browser, however this is slow and cumbersome, negating the benefits of making tests concurrent.
Request
From what I can tell, the
globalThis.document
is used in thediff
function: https://github.com/preactjs/preact/blob/main/src/diff/index.js#L414element.ownerDocument
This would allow preact to use the
document
object associated with the currentHTMLElement
renderToDom(component, target, globalContext)
.This would allow me to inject the specific DOM into the render pipeline. This might cause issues with iframes though
I can raise a PR for this, is that okay?
The text was updated successfully, but these errors were encountered: