-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom vitest environment for jsdom
- Loading branch information
Showing
6 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { CookieJar, JSDOM, ResourceLoader, VirtualConsole } from 'jsdom'; | ||
import { Environment } from 'vitest' | ||
import { populateGlobal } from 'vitest/environments' | ||
|
||
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) | ||
} | ||
} | ||
|
||
const ALLOWED_KEYS = [ | ||
'Uint8Array' | ||
] | ||
|
||
export default <Environment>({ | ||
name: 'jsdom', | ||
transformMode: 'web', | ||
async setupVM({ 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 web globals if they missing in JSDOM but otherwise available in Nodejs | ||
// https://nodejs.org/dist/latest/docs/api/globals.html | ||
const globalNames = [ | ||
'structuredClone', | ||
'fetch', | ||
'Request', | ||
'Response', | ||
'BroadcastChannel', | ||
'MessageChannel', | ||
'MessagePort', | ||
] as const | ||
for (const name of globalNames) { | ||
const value = globalThis[name] | ||
if ( | ||
typeof value !== 'undefined' | ||
&& typeof dom.window[name] === 'undefined' | ||
) | ||
dom.window[name] = value | ||
} | ||
|
||
return { | ||
getVmContext() { | ||
return dom.getInternalVMContext() | ||
}, | ||
teardown() { | ||
clearWindowErrors() | ||
dom.window.close() | ||
}, | ||
} | ||
}, | ||
async setup(global, { jsdom = {} }) { | ||
const { | ||
CookieJar, | ||
JSDOM, | ||
ResourceLoader, | ||
VirtualConsole, | ||
} = await require('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 }); | ||
|
||
ALLOWED_KEYS.forEach((key) => { | ||
delete global[key]; | ||
global[key] = originals.get(key); | ||
}); | ||
|
||
const clearWindowErrors = catchWindowErrors(global) | ||
|
||
return { | ||
teardown(global) { | ||
clearWindowErrors() | ||
dom.window.close() | ||
keys.forEach(key => delete global[key]) | ||
originals.forEach((v, k) => global[k] = v) | ||
}, | ||
} | ||
}, | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "vitest-environment-custom-jsdom", | ||
"private": true, | ||
"main": "index.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters