-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
jsdom.ts
135 lines (127 loc) · 3.98 KB
/
jsdom.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { importModule } from 'local-pkg'
import type { Environment } from '../../types'
import { populateGlobal } from './utils'
function catchWindowErrors(window: Window) {
let userErrorListenerCount = 0
function throwUnhandlerError(e: ErrorEvent) {
if (userErrorListenerCount === 0 && e.error != null)
process.emit('uncaughtException', e.error)
}
const addEventListener = window.addEventListener.bind(window)
const removeEventListener = window.removeEventListener.bind(window)
window.addEventListener('error', throwUnhandlerError)
window.addEventListener = function (...args: Parameters<typeof addEventListener>) {
if (args[0] === 'error')
userErrorListenerCount++
return addEventListener.apply(this, args)
}
window.removeEventListener = function (...args: Parameters<typeof removeEventListener>) {
if (args[0] === 'error' && userErrorListenerCount)
userErrorListenerCount--
return removeEventListener.apply(this, args)
}
return function clearErrorHandlers() {
window.removeEventListener('error', throwUnhandlerError)
}
}
export default <Environment>({
name: 'jsdom',
transformMode: 'web',
async setupVM({ jsdom = {} }) {
const {
CookieJar,
JSDOM,
ResourceLoader,
VirtualConsole,
} = await importModule('jsdom') as typeof import('jsdom')
const {
html = '<!DOCTYPE html>',
userAgent,
url = 'http://localhost:3000',
contentType = 'text/html',
pretendToBeVisual = true,
includeNodeLocations = false,
runScripts = 'dangerously',
resources,
console = false,
cookieJar = false,
...restOptions
} = jsdom as any
const dom = new JSDOM(
html,
{
pretendToBeVisual,
resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : undefined),
runScripts,
url,
virtualConsole: (console && globalThis.console) ? new VirtualConsole().sendTo(globalThis.console) : undefined,
cookieJar: cookieJar ? new CookieJar() : undefined,
includeNodeLocations,
contentType,
userAgent,
...restOptions,
},
)
const clearWindowErrors = catchWindowErrors(dom.window as any)
// TODO: browser doesn't expose Buffer, but a lot of dependencies use it
dom.window.Buffer = Buffer
// inject structuredClone if it exists
if (typeof structuredClone !== 'undefined' && !dom.window.structuredClone)
dom.window.structuredClone = structuredClone
return {
getVmContext() {
return dom.getInternalVMContext()
},
teardown() {
clearWindowErrors()
dom.window.close()
},
}
},
async setup(global, { jsdom = {} }) {
const {
CookieJar,
JSDOM,
ResourceLoader,
VirtualConsole,
} = await importModule('jsdom') as typeof import('jsdom')
const {
html = '<!DOCTYPE html>',
userAgent,
url = 'http://localhost:3000',
contentType = 'text/html',
pretendToBeVisual = true,
includeNodeLocations = false,
runScripts = 'dangerously',
resources,
console = false,
cookieJar = false,
...restOptions
} = jsdom as any
const dom = new JSDOM(
html,
{
pretendToBeVisual,
resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : undefined),
runScripts,
url,
virtualConsole: (console && global.console) ? new VirtualConsole().sendTo(global.console) : undefined,
cookieJar: cookieJar ? new CookieJar() : undefined,
includeNodeLocations,
contentType,
userAgent,
...restOptions,
},
)
const { keys, originals } = populateGlobal(global, dom.window, { bindFunctions: true })
const clearWindowErrors = catchWindowErrors(global)
return {
teardown(global) {
clearWindowErrors()
dom.window.close()
keys.forEach(key => delete global[key])
originals.forEach((v, k) => global[k] = v)
},
}
},
})