brf/client/test/jsdom_polyfills.ts
2025-12-13 21:12:08 +01:00

40 lines
1.3 KiB
TypeScript

// modified version of: https://github.com/modosc/global-jsdom/blob/d1dd3cdeeeddd4d0653496a728e0f81e18776654/packages/global-jsdom/esm/index.mjs
// @ts-ignore
import JSDOM from 'jsdom'
const html = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>'
const jsdom = new JSDOM.JSDOM(html, {
// set a default url if we don't get one - otherwise things explode when we
// copy localstorage keys
url: 'http://localhost:3000',
// enable pretendtobevisual by default since react needs
// window.requestanimationframe, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
pretendToBeVisual: true,
})
const { window } = jsdom
const { document } = window
const KEYS = Object.getOwnPropertyNames(window).filter((k) => !k.startsWith('_') && !(k in globalThis))
// @ts-ignore
KEYS.forEach((key) => (globalThis[key] = window[key]))
globalThis.document = document
globalThis.window = window
window.console = globalThis.console
ElementInternals.prototype.setFormValue = () => {}
ElementInternals.prototype.setValidity = () => {}
// since cant modify the constructor we instantiate states in a getter
Object.defineProperty(ElementInternals.prototype, 'states', {
get() {
if (!this._states) this._states = new Set()
return this._states
},
})