/
happy-dom.ts
29 lines (25 loc) · 937 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
import { importModule } from 'local-pkg'
import type { Environment } from '../../types'
import { populateGlobal } from './utils'
export default <Environment>({
name: 'happy-dom',
async setup(global) {
// happy-dom v3 introduced a breaking change to Window, but
// provides GlobalWindow as a way to use previous behaviour
const { Window, GlobalWindow } = await importModule('happy-dom') as typeof import('happy-dom')
const win = new (GlobalWindow || Window)()
const { keys, allowRewrite } = populateGlobal(global, win)
const originals = new Map<string | symbol, any>(
allowRewrite.map(([key]) => [key, global[key]]),
)
return {
teardown(global) {
win.document.head.innerHTML = ''
win.document.body.innerHTML = ''
win.happyDOM.cancelAsync()
keys.forEach(key => delete global[key])
originals.forEach((v, k) => global[k] = v)
},
}
},
})