-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhappy-dom.ts
More file actions
37 lines (33 loc) · 1.08 KB
/
happy-dom.ts
File metadata and controls
37 lines (33 loc) · 1.08 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
import { GlobalWindow } from 'happy-dom'
const win = new GlobalWindow({ url: 'http://localhost' })
// Register all browser globals from the window instance
for (const key of Object.getOwnPropertyNames(win)) {
if (key === 'constructor' || key === 'undefined' || key === 'NaN' || key === 'Infinity') continue
try {
;(globalThis as any)[key] = (win as any)[key]
}
catch {
// Some properties may not be configurable
}
}
// Register prototype-level getters/methods
for (const key of Object.getOwnPropertyNames(Object.getPrototypeOf(win))) {
if (key === 'constructor' || key in globalThis) continue
try {
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(win), key)
if (descriptor?.get) {
Object.defineProperty(globalThis, key, {
get: () => (win as any)[key],
configurable: true,
})
}
else if (typeof (win as any)[key] === 'function') {
;(globalThis as any)[key] = (win as any)[key].bind(win)
}
}
catch {
// Skip non-configurable properties
}
}
// Ensure window is set
;(globalThis as any).window = win