forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
happy-dom.ts
35 lines (31 loc) · 881 Bytes
/
happy-dom.ts
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
import { importModule } from 'local-pkg'
import type { Environment } from '../../types'
import { getWindowKeys } from './utils'
export default <Environment>({
name: 'happy-dom',
async setup(global) {
const { Window } = await importModule('happy-dom') as typeof import('happy-dom')
const win: any = new Window()
const keys = getWindowKeys(global, win)
const overrideObject = new Map<string, any>()
for (const key of keys) {
Object.defineProperty(global, key, {
get() {
if (overrideObject.has(key))
return overrideObject.get(key)
return win[key]
},
set(v) {
overrideObject.set(key, v)
},
configurable: true,
})
}
return {
teardown(global) {
win.happyDOM.cancelAsync()
keys.forEach(key => delete global[key])
},
}
},
})