Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

jsdom.ts

51 lines1.4 KB

1import { JSDOM } from 'jsdom'
2​
3const dom = new JSDOM('<!doctype html><html><body></body></html>', {
4 url: 'http://localhost/',
5})
6​
7const { window } = dom
8​
9function setGlobal(name: string, value: unknown) {
10 Object.defineProperty(globalThis, name, {
11 value,
12 configurable: true,
13 writable: true,
14 })
16​
17setGlobal('window', window)
18setGlobal('document', window.document)
19setGlobal('self', window)
20setGlobal('navigator', window.navigator)
21setGlobal('location', window.location)
22setGlobal('history', window.history)
23setGlobal('HTMLElement', window.HTMLElement)
24setGlobal('Element', window.Element)
25setGlobal('SVGElement', window.SVGElement)
26setGlobal('DocumentFragment', window.DocumentFragment)
27setGlobal('Node', window.Node)
28setGlobal('MouseEvent', window.MouseEvent)
29setGlobal('MutationObserver', window.MutationObserver)
30setGlobal('sessionStorage', window.sessionStorage)
31setGlobal('localStorage', window.localStorage)
32setGlobal('getComputedStyle', window.getComputedStyle.bind(window))
33​
34setGlobal(
35 'requestAnimationFrame',
36 window.requestAnimationFrame?.bind(window) ??
37 ((callback: (time: number) => void) =>
38 setTimeout(() => callback(performance.now()), 16)),
40​
41setGlobal(
42 'cancelAnimationFrame',
43 window.cancelAnimationFrame?.bind(window) ??
44 ((handle: number) => clearTimeout(handle)),
46​
47const scrollTo = () => {}
48window.scrollTo = scrollTo
49setGlobal('scrollTo', scrollTo)
50​
51export { window }