123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- import { Console } from 'console';
- import { importModule } from 'local-pkg';
- var node = {
- name: "node",
- async setup(global) {
- global.console.Console = Console;
- return {
- teardown(global2) {
- delete global2.console.Console;
- }
- };
- }
- };
- const LIVING_KEYS = [
- "DOMException",
- "URL",
- "URLSearchParams",
- "EventTarget",
- "NamedNodeMap",
- "Node",
- "Attr",
- "Element",
- "DocumentFragment",
- "DOMImplementation",
- "Document",
- "XMLDocument",
- "CharacterData",
- "Text",
- "CDATASection",
- "ProcessingInstruction",
- "Comment",
- "DocumentType",
- "NodeList",
- "HTMLCollection",
- "HTMLOptionsCollection",
- "DOMStringMap",
- "DOMTokenList",
- "StyleSheetList",
- "HTMLElement",
- "HTMLHeadElement",
- "HTMLTitleElement",
- "HTMLBaseElement",
- "HTMLLinkElement",
- "HTMLMetaElement",
- "HTMLStyleElement",
- "HTMLBodyElement",
- "HTMLHeadingElement",
- "HTMLParagraphElement",
- "HTMLHRElement",
- "HTMLPreElement",
- "HTMLUListElement",
- "HTMLOListElement",
- "HTMLLIElement",
- "HTMLMenuElement",
- "HTMLDListElement",
- "HTMLDivElement",
- "HTMLAnchorElement",
- "HTMLAreaElement",
- "HTMLBRElement",
- "HTMLButtonElement",
- "HTMLCanvasElement",
- "HTMLDataElement",
- "HTMLDataListElement",
- "HTMLDetailsElement",
- "HTMLDialogElement",
- "HTMLDirectoryElement",
- "HTMLFieldSetElement",
- "HTMLFontElement",
- "HTMLFormElement",
- "HTMLHtmlElement",
- "HTMLImageElement",
- "HTMLInputElement",
- "HTMLLabelElement",
- "HTMLLegendElement",
- "HTMLMapElement",
- "HTMLMarqueeElement",
- "HTMLMediaElement",
- "HTMLMeterElement",
- "HTMLModElement",
- "HTMLOptGroupElement",
- "HTMLOptionElement",
- "HTMLOutputElement",
- "HTMLPictureElement",
- "HTMLProgressElement",
- "HTMLQuoteElement",
- "HTMLScriptElement",
- "HTMLSelectElement",
- "HTMLSlotElement",
- "HTMLSourceElement",
- "HTMLSpanElement",
- "HTMLTableCaptionElement",
- "HTMLTableCellElement",
- "HTMLTableColElement",
- "HTMLTableElement",
- "HTMLTimeElement",
- "HTMLTableRowElement",
- "HTMLTableSectionElement",
- "HTMLTemplateElement",
- "HTMLTextAreaElement",
- "HTMLUnknownElement",
- "HTMLFrameElement",
- "HTMLFrameSetElement",
- "HTMLIFrameElement",
- "HTMLEmbedElement",
- "HTMLObjectElement",
- "HTMLParamElement",
- "HTMLVideoElement",
- "HTMLAudioElement",
- "HTMLTrackElement",
- "SVGElement",
- "SVGGraphicsElement",
- "SVGSVGElement",
- "SVGTitleElement",
- "SVGAnimatedString",
- "SVGNumber",
- "SVGStringList",
- "Event",
- "CloseEvent",
- "CustomEvent",
- "MessageEvent",
- "ErrorEvent",
- "HashChangeEvent",
- "PopStateEvent",
- "StorageEvent",
- "ProgressEvent",
- "PageTransitionEvent",
- "UIEvent",
- "FocusEvent",
- "InputEvent",
- "MouseEvent",
- "KeyboardEvent",
- "TouchEvent",
- "CompositionEvent",
- "WheelEvent",
- "BarProp",
- "External",
- "Location",
- "History",
- "Screen",
- "Performance",
- "Navigator",
- "PluginArray",
- "MimeTypeArray",
- "Plugin",
- "MimeType",
- "FileReader",
- "Blob",
- "File",
- "FileList",
- "ValidityState",
- "DOMParser",
- "XMLSerializer",
- "FormData",
- "XMLHttpRequestEventTarget",
- "XMLHttpRequestUpload",
- "XMLHttpRequest",
- "WebSocket",
- "NodeFilter",
- "NodeIterator",
- "TreeWalker",
- "AbstractRange",
- "Range",
- "StaticRange",
- "Selection",
- "Storage",
- "CustomElementRegistry",
- "ShadowRoot",
- "MutationObserver",
- "MutationRecord",
- "Headers",
- "AbortController",
- "AbortSignal",
- "Image",
- "Audio",
- "Option"
- ];
- const OTHER_KEYS = [
- "addEventListener",
- "alert",
- "atob",
- "blur",
- "btoa",
- "cancelAnimationFrame",
- "close",
- "confirm",
- "createPopup",
- "dispatchEvent",
- "document",
- "focus",
- "frames",
- "getComputedStyle",
- "history",
- "innerHeight",
- "innerWidth",
- "length",
- "location",
- "matchMedia",
- "moveBy",
- "moveTo",
- "name",
- "navigator",
- "open",
- "outerHeight",
- "outerWidth",
- "pageXOffset",
- "pageYOffset",
- "parent",
- "postMessage",
- "print",
- "prompt",
- "removeEventListener",
- "requestAnimationFrame",
- "resizeBy",
- "resizeTo",
- "screen",
- "screenLeft",
- "screenTop",
- "screenX",
- "screenY",
- "scroll",
- "scrollBy",
- "scrollLeft",
- "scrollTo",
- "scrollTop",
- "scrollX",
- "scrollY",
- "self",
- "stop",
- "top",
- "Window",
- "window"
- ];
- const KEYS = LIVING_KEYS.concat(OTHER_KEYS);
- const allowRewrite = [
- "Event",
- "EventTarget",
- "MessageEvent",
- "ArrayBuffer"
- ];
- const skipKeys = [
- "window",
- "self",
- "top",
- "parent"
- ];
- function getWindowKeys(global, win) {
- const keys = new Set(KEYS.concat(Object.getOwnPropertyNames(win)).filter((k) => {
- if (skipKeys.includes(k))
- return false;
- if (k in global)
- return allowRewrite.includes(k);
- return true;
- }));
- return keys;
- }
- function isClassLikeName(name) {
- return name[0] === name[0].toUpperCase();
- }
- function populateGlobal(global, win, options = {}) {
- const { bindFunctions = false } = options;
- const keys = getWindowKeys(global, win);
- const originals = new Map(
- allowRewrite.filter((key) => key in global).map((key) => [key, global[key]])
- );
- const overrideObject = /* @__PURE__ */ new Map();
- for (const key of keys) {
- const boundFunction = bindFunctions && typeof win[key] === "function" && !isClassLikeName(key) && win[key].bind(win);
- Object.defineProperty(global, key, {
- get() {
- if (overrideObject.has(key))
- return overrideObject.get(key);
- if (boundFunction)
- return boundFunction;
- return win[key];
- },
- set(v) {
- overrideObject.set(key, v);
- },
- configurable: true
- });
- }
- global.window = global;
- global.self = global;
- global.top = global;
- global.parent = global;
- if (global.global)
- global.global = global;
- skipKeys.forEach((k) => keys.add(k));
- return {
- keys,
- skipKeys,
- originals
- };
- }
- var jsdom = {
- name: "jsdom",
- async setup(global, { jsdom = {} }) {
- const {
- CookieJar,
- JSDOM,
- ResourceLoader,
- VirtualConsole
- } = await importModule("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;
- const dom = new JSDOM(
- html,
- {
- pretendToBeVisual,
- resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : void 0),
- runScripts,
- url,
- virtualConsole: console && global.console ? new VirtualConsole().sendTo(global.console) : void 0,
- cookieJar: cookieJar ? new CookieJar() : void 0,
- includeNodeLocations,
- contentType,
- userAgent,
- ...restOptions
- }
- );
- const { keys, originals } = populateGlobal(global, dom.window, { bindFunctions: true });
- return {
- teardown(global2) {
- keys.forEach((key) => delete global2[key]);
- originals.forEach((v, k) => global2[k] = v);
- }
- };
- }
- };
- var happy = {
- name: "happy-dom",
- async setup(global) {
- const { Window, GlobalWindow } = await importModule("happy-dom");
- const win = new (GlobalWindow || Window)();
- const { keys, originals } = populateGlobal(global, win, { bindFunctions: true });
- return {
- teardown(global2) {
- win.happyDOM.cancelAsync();
- keys.forEach((key) => delete global2[key]);
- originals.forEach((v, k) => global2[k] = v);
- }
- };
- }
- };
- var edge = {
- name: "edge-runtime",
- async setup(global) {
- const { EdgeVM } = await importModule("@edge-runtime/vm");
- const vm = new EdgeVM({
- extend: (context) => {
- context.global = context;
- context.Buffer = Buffer;
- return context;
- }
- });
- const { keys, originals } = populateGlobal(global, vm.context, { bindFunctions: true });
- return {
- teardown(global2) {
- keys.forEach((key) => delete global2[key]);
- originals.forEach((v, k) => global2[k] = v);
- }
- };
- }
- };
- const environments = {
- node,
- jsdom,
- "happy-dom": happy,
- "edge-runtime": edge
- };
- const envs = Object.keys(environments);
- const envPackageNames = {
- "jsdom": "jsdom",
- "happy-dom": "happy-dom",
- "edge-runtime": "@edge-runtime/vm"
- };
- const getEnvPackageName = (env) => {
- if (env === "node")
- return null;
- if (env in envPackageNames)
- return envPackageNames[env];
- return `vitest-environment-${env}`;
- };
- export { envs as a, environments as e, getEnvPackageName as g, populateGlobal as p };
|