-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathjsdom.ts
More file actions
51 lines (43 loc) 路 1.4 KB
/
Copy pathjsdom.ts
File metadata and controls
51 lines (43 loc) 路 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { JSDOM } from 'jsdom'
const dom = new JSDOM('<!doctype html><html><body></body></html>', {
url: 'http://localhost/',
})
const { window } = dom
function setGlobal(name: string, value: unknown) {
Object.defineProperty(globalThis, name, {
value,
configurable: true,
writable: true,
})
}
setGlobal('window', window)
setGlobal('document', window.document)
setGlobal('self', window)
setGlobal('navigator', window.navigator)
setGlobal('location', window.location)
setGlobal('history', window.history)
setGlobal('HTMLElement', window.HTMLElement)
setGlobal('Element', window.Element)
setGlobal('SVGElement', window.SVGElement)
setGlobal('DocumentFragment', window.DocumentFragment)
setGlobal('Node', window.Node)
setGlobal('MouseEvent', window.MouseEvent)
setGlobal('MutationObserver', window.MutationObserver)
setGlobal('sessionStorage', window.sessionStorage)
setGlobal('localStorage', window.localStorage)
setGlobal('getComputedStyle', window.getComputedStyle.bind(window))
setGlobal(
'requestAnimationFrame',
window.requestAnimationFrame?.bind(window) ??
((callback: (time: number) => void) =>
setTimeout(() => callback(performance.now()), 16)),
)
setGlobal(
'cancelAnimationFrame',
window.cancelAnimationFrame?.bind(window) ??
((handle: number) => clearTimeout(handle)),
)
const scrollTo = () => {}
window.scrollTo = scrollTo
setGlobal('scrollTo', scrollTo)
export { window }