Open History to see the last commit
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 })
15}
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)),
39)
40β
41setGlobal(
42 'cancelAnimationFrame',
43 window.cancelAnimationFrame?.bind(window) ??
44 ((handle: number) => clearTimeout(handle)),
45)
46β
47const scrollTo = () => {}
48window.scrollTo = scrollTo
49setGlobal('scrollTo', scrollTo)
50β
51export { window }