vitest 4.1.5 → 5.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/LICENSE.md +7 -0
  2. package/dist/browser.d.ts +9 -9
  3. package/dist/browser.js +4 -4
  4. package/dist/chunks/{base.RR7zL1h0.js → base.Opc_YHkk.js} +10 -11
  5. package/dist/chunks/browser.d.BUhkKcDl.d.ts +899 -0
  6. package/dist/chunks/{cac.DJJmV0dT.js → cac.8N4bOkkB.js} +23 -11
  7. package/dist/chunks/{cli-api.Cjt90eJu.js → cli-api.B0RFke2g.js} +5799 -353
  8. package/dist/chunks/{config.d.A1h_Y6Jt.d.ts → config.d.D91DHYaD.d.ts} +11 -3
  9. package/dist/chunks/{console.3WNpx0tS.js → console.B3IRP8fX.js} +3 -1
  10. package/dist/chunks/{constants.CPYnjOGj.js → constants.-juJ8b_4.js} +1 -1
  11. package/dist/chunks/{coverage.d.BZtK59WP.d.ts → coverage.d.g2xbl2sP.d.ts} +4 -0
  12. package/dist/chunks/{creator.DgVhQm5q.js → creator.BqL2U_x4.js} +1 -1
  13. package/dist/chunks/{defaults.9aQKnqFk.js → defaults.szbHWQun.js} +4 -2
  14. package/dist/chunks/environment.d-DOJxxZV9.d.DOJxxZV9.d.ts +17 -0
  15. package/dist/chunks/general.d.DFAHgpC2.d.ts +247 -0
  16. package/dist/chunks/{global.d.DVsSRdQ5.d.ts → global.d.DhbKSQoV.d.ts} +4 -5
  17. package/dist/chunks/{globals.Dj1TGiMC.js → globals.EHmmu0nC.js} +15 -14
  18. package/dist/chunks/{index.DXx9Dtk7.js → index.CViWo__T.js} +5 -5
  19. package/dist/chunks/{startVitestModuleRunner.bRl2_oI_.js → index.CbgUM9E5.js} +731 -5
  20. package/dist/chunks/{test.DNmyFkvJ.js → index.D_7-4CaB.js} +2659 -14
  21. package/dist/chunks/{init-forks.UV3ZQGQH.js → init-forks.DMge3WTt.js} +1 -1
  22. package/dist/chunks/{init-threads.D3eCsY76.js → init-threads.eIoyCTon.js} +1 -1
  23. package/dist/chunks/{init.D98-gwRW.js → init.BVd7SaCA.js} +3 -5
  24. package/dist/chunks/{nativeModuleMocker.BRN2oBJd.js → nativeModuleMocker.DKpFw0pk.js} +3 -2
  25. package/dist/chunks/{index.BCY_7LL2.js → nativeModuleRunner.BOeMnHl4.js} +43 -12
  26. package/dist/chunks/node.CwFbQqI1.js +47 -0
  27. package/dist/chunks/{reporters.d.CEnv6XRv.d.ts → plugin.d.cIKZEZ16.d.ts} +306 -19
  28. package/dist/chunks/plugins.DrsmdUE2.js +37 -0
  29. package/dist/chunks/{rpc.MzXet3jl.js → rpc.DFRWVnRh.js} +16 -1
  30. package/dist/chunks/{rpc.d.B_8sPU0w.d.ts → rpc.d.7JZuxZ8u.d.ts} +19 -3
  31. package/dist/chunks/{setup-common.DYx3LtFI.js → setup-common.Hpq30zVk.js} +7 -3
  32. package/dist/chunks/{utils.BS4fH3nR.js → utils.DKODp04v.js} +3 -4
  33. package/dist/chunks/{vm.DVLYObm9.js → vm.2okbRRME.js} +6 -6
  34. package/dist/chunks/{worker.d.ZpHpO4yb.d.ts → worker.d.Bu1kXGw4.d.ts} +3 -3
  35. package/dist/cli.js +2 -2
  36. package/dist/config.cjs +4 -2
  37. package/dist/config.d.ts +21 -18
  38. package/dist/config.js +2 -2
  39. package/dist/index.d.ts +84 -22
  40. package/dist/index.js +15 -13
  41. package/dist/module-evaluator.d.ts +5 -3
  42. package/dist/module-evaluator.js +1 -1
  43. package/dist/node.d.ts +114 -19
  44. package/dist/node.js +21 -26
  45. package/dist/runtime.d.ts +40 -4
  46. package/dist/runtime.js +5 -6
  47. package/dist/{chunks/traces.DT5aQ62U.js → traces.js} +1 -1
  48. package/dist/worker.d.ts +5 -5
  49. package/dist/worker.js +21 -23
  50. package/dist/workers/forks.js +21 -23
  51. package/dist/workers/runVmTests.js +17 -16
  52. package/dist/workers/threads.js +21 -23
  53. package/dist/workers/vmForks.js +7 -9
  54. package/dist/workers/vmThreads.js +7 -9
  55. package/package.json +21 -38
  56. package/dist/chunks/benchmark.CX_oY03V.js +0 -40
  57. package/dist/chunks/benchmark.d.DAaHLpsq.d.ts +0 -24
  58. package/dist/chunks/browser.d.BcoexmFG.d.ts +0 -62
  59. package/dist/chunks/coverage.DM_a_rWm.js +0 -1087
  60. package/dist/chunks/evaluatedModules.Dg1zASAC.js +0 -17
  61. package/dist/chunks/index.DC7d2Pf8.js +0 -729
  62. package/dist/chunks/index.DdgEv5B1.js +0 -42
  63. package/dist/chunks/index.UpGiHP7g.js +0 -4255
  64. package/dist/chunks/nativeModuleRunner.BIakptoF.js +0 -36
  65. package/dist/chunks/node.COQbm6gK.js +0 -14
  66. package/dist/chunks/plugin.d.BM2TCi12.d.ts +0 -38
  67. package/dist/chunks/suite.d.udJtyAgw.d.ts +0 -10
  68. package/dist/chunks/traces.d.D2T_R8rx.d.ts +0 -60
  69. package/dist/coverage.d.ts +0 -123
  70. package/dist/coverage.js +0 -27
  71. package/dist/environments.d.ts +0 -22
  72. package/dist/environments.js +0 -5
  73. package/dist/reporters.d.ts +0 -27
  74. package/dist/reporters.js +0 -26
  75. package/dist/runners.d.ts +0 -70
  76. package/dist/runners.js +0 -19
  77. package/dist/snapshot.d.ts +0 -9
  78. package/dist/snapshot.js +0 -6
  79. package/dist/suite.d.ts +0 -5
  80. package/dist/suite.js +0 -8
@@ -1,8 +1,8 @@
1
- import fs from 'node:fs';
1
+ import fs__default from 'node:fs';
2
2
  import { isBareImport } from '@vitest/utils/helpers';
3
3
  import { i as isBuiltin, a as isBrowserExternal, t as toBuiltin } from './modules.BJuCwlRJ.js';
4
4
  import { E as EnvironmentTeardownError, a as getSafeWorkerState } from './utils.BX5Fg8C4.js';
5
- import { pathToFileURL } from 'node:url';
5
+ import { pathToFileURL, URL as URL$1 } from 'node:url';
6
6
  import { normalize, join } from 'pathe';
7
7
  import { distDir } from '../path.js';
8
8
  import { VitestModuleEvaluator, unwrapId } from '../module-evaluator.js';
@@ -11,7 +11,8 @@ import vm from 'node:vm';
11
11
  import { MockerRegistry, mockObject, RedirectedModule, AutomockedModule } from '@vitest/mocker';
12
12
  import { findMockRedirect } from '@vitest/mocker/redirect';
13
13
  import * as viteModuleRunner from 'vite/module-runner';
14
- import { T as Traces } from './traces.DT5aQ62U.js';
14
+ import { Traces } from '../traces.js';
15
+ import { Console } from 'node:console';
15
16
 
16
17
  class BareModuleMocker {
17
18
  static pendingIds = [];
@@ -626,7 +627,7 @@ function getCachedVitestImport(id, state) {
626
627
  return null;
627
628
  }
628
629
 
629
- const { readFileSync } = fs;
630
+ const { readFileSync } = fs__default;
630
631
  const VITEST_VM_CONTEXT_SYMBOL = "__vitest_vm_context__";
631
632
  const cwd = process.cwd();
632
633
  const isWindows = process.platform === "win32";
@@ -732,4 +733,729 @@ function startVitestModuleRunner(options) {
732
733
  return moduleRunner;
733
734
  }
734
735
 
735
- export { BareModuleMocker as B, VITEST_VM_CONTEXT_SYMBOL as V, VitestModuleRunner as a, VitestTransport as b, createNodeImportMeta as c, normalizeModuleId as n, startVitestModuleRunner as s };
736
+ // SEE https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/interfaces.js
737
+ const LIVING_KEYS = [
738
+ "DOMException",
739
+ "EventTarget",
740
+ "NamedNodeMap",
741
+ "Node",
742
+ "Attr",
743
+ "Element",
744
+ "DocumentFragment",
745
+ "DOMImplementation",
746
+ "Document",
747
+ "XMLDocument",
748
+ "CharacterData",
749
+ "Text",
750
+ "CDATASection",
751
+ "ProcessingInstruction",
752
+ "Comment",
753
+ "DocumentType",
754
+ "NodeList",
755
+ "RadioNodeList",
756
+ "HTMLCollection",
757
+ "HTMLOptionsCollection",
758
+ "DOMStringMap",
759
+ "DOMTokenList",
760
+ "StyleSheetList",
761
+ "HTMLElement",
762
+ "HTMLHeadElement",
763
+ "HTMLTitleElement",
764
+ "HTMLBaseElement",
765
+ "HTMLLinkElement",
766
+ "HTMLMetaElement",
767
+ "HTMLStyleElement",
768
+ "HTMLBodyElement",
769
+ "HTMLHeadingElement",
770
+ "HTMLParagraphElement",
771
+ "HTMLHRElement",
772
+ "HTMLPreElement",
773
+ "HTMLUListElement",
774
+ "HTMLOListElement",
775
+ "HTMLLIElement",
776
+ "HTMLMenuElement",
777
+ "HTMLDListElement",
778
+ "HTMLDivElement",
779
+ "HTMLAnchorElement",
780
+ "HTMLAreaElement",
781
+ "HTMLBRElement",
782
+ "HTMLButtonElement",
783
+ "HTMLCanvasElement",
784
+ "HTMLDataElement",
785
+ "HTMLDataListElement",
786
+ "HTMLDetailsElement",
787
+ "HTMLDialogElement",
788
+ "HTMLDirectoryElement",
789
+ "HTMLFieldSetElement",
790
+ "HTMLFontElement",
791
+ "HTMLFormElement",
792
+ "HTMLHtmlElement",
793
+ "HTMLImageElement",
794
+ "HTMLInputElement",
795
+ "HTMLLabelElement",
796
+ "HTMLLegendElement",
797
+ "HTMLMapElement",
798
+ "HTMLMarqueeElement",
799
+ "HTMLMediaElement",
800
+ "HTMLMeterElement",
801
+ "HTMLModElement",
802
+ "HTMLOptGroupElement",
803
+ "HTMLOptionElement",
804
+ "HTMLOutputElement",
805
+ "HTMLPictureElement",
806
+ "HTMLProgressElement",
807
+ "HTMLQuoteElement",
808
+ "HTMLScriptElement",
809
+ "HTMLSelectElement",
810
+ "HTMLSlotElement",
811
+ "HTMLSourceElement",
812
+ "HTMLSpanElement",
813
+ "HTMLTableCaptionElement",
814
+ "HTMLTableCellElement",
815
+ "HTMLTableColElement",
816
+ "HTMLTableElement",
817
+ "HTMLTimeElement",
818
+ "HTMLTableRowElement",
819
+ "HTMLTableSectionElement",
820
+ "HTMLTemplateElement",
821
+ "HTMLTextAreaElement",
822
+ "HTMLUnknownElement",
823
+ "HTMLFrameElement",
824
+ "HTMLFrameSetElement",
825
+ "HTMLIFrameElement",
826
+ "HTMLEmbedElement",
827
+ "HTMLObjectElement",
828
+ "HTMLParamElement",
829
+ "HTMLVideoElement",
830
+ "HTMLAudioElement",
831
+ "HTMLTrackElement",
832
+ "HTMLFormControlsCollection",
833
+ "SVGElement",
834
+ "SVGGraphicsElement",
835
+ "SVGSVGElement",
836
+ "SVGTitleElement",
837
+ "SVGAnimatedString",
838
+ "SVGNumber",
839
+ "SVGStringList",
840
+ "Event",
841
+ "CloseEvent",
842
+ "CustomEvent",
843
+ "MessageEvent",
844
+ "ErrorEvent",
845
+ "HashChangeEvent",
846
+ "PopStateEvent",
847
+ "StorageEvent",
848
+ "ProgressEvent",
849
+ "PageTransitionEvent",
850
+ "SubmitEvent",
851
+ "UIEvent",
852
+ "FocusEvent",
853
+ "InputEvent",
854
+ "MouseEvent",
855
+ "KeyboardEvent",
856
+ "TouchEvent",
857
+ "CompositionEvent",
858
+ "WheelEvent",
859
+ "BarProp",
860
+ "External",
861
+ "Location",
862
+ "History",
863
+ "Screen",
864
+ "Crypto",
865
+ "Performance",
866
+ "Navigator",
867
+ "PluginArray",
868
+ "MimeTypeArray",
869
+ "Plugin",
870
+ "MimeType",
871
+ "FileReader",
872
+ "FormData",
873
+ "Blob",
874
+ "File",
875
+ "FileList",
876
+ "ValidityState",
877
+ "DOMParser",
878
+ "XMLSerializer",
879
+ "XMLHttpRequestEventTarget",
880
+ "XMLHttpRequestUpload",
881
+ "XMLHttpRequest",
882
+ "WebSocket",
883
+ "NodeFilter",
884
+ "NodeIterator",
885
+ "TreeWalker",
886
+ "AbstractRange",
887
+ "Range",
888
+ "StaticRange",
889
+ "Selection",
890
+ "Storage",
891
+ "CustomElementRegistry",
892
+ "ShadowRoot",
893
+ "MutationObserver",
894
+ "MutationRecord",
895
+ "Uint8Array",
896
+ "Uint16Array",
897
+ "Uint32Array",
898
+ "Uint8ClampedArray",
899
+ "Int8Array",
900
+ "Int16Array",
901
+ "Int32Array",
902
+ "Float32Array",
903
+ "Float64Array",
904
+ "ArrayBuffer",
905
+ "DOMRectReadOnly",
906
+ "DOMRect",
907
+ "Image",
908
+ "Audio",
909
+ "Option",
910
+ "CSS"
911
+ ];
912
+ const OTHER_KEYS = [
913
+ "addEventListener",
914
+ "alert",
915
+ "blur",
916
+ "cancelAnimationFrame",
917
+ "close",
918
+ "confirm",
919
+ "createPopup",
920
+ "dispatchEvent",
921
+ "document",
922
+ "focus",
923
+ "frames",
924
+ "getComputedStyle",
925
+ "history",
926
+ "innerHeight",
927
+ "innerWidth",
928
+ "length",
929
+ "location",
930
+ "matchMedia",
931
+ "moveBy",
932
+ "moveTo",
933
+ "name",
934
+ "navigator",
935
+ "open",
936
+ "outerHeight",
937
+ "outerWidth",
938
+ "pageXOffset",
939
+ "pageYOffset",
940
+ "parent",
941
+ "postMessage",
942
+ "print",
943
+ "prompt",
944
+ "removeEventListener",
945
+ "requestAnimationFrame",
946
+ "resizeBy",
947
+ "resizeTo",
948
+ "screen",
949
+ "screenLeft",
950
+ "screenTop",
951
+ "screenX",
952
+ "screenY",
953
+ "scroll",
954
+ "scrollBy",
955
+ "scrollLeft",
956
+ "scrollTo",
957
+ "scrollTop",
958
+ "scrollX",
959
+ "scrollY",
960
+ "self",
961
+ "stop",
962
+ "top",
963
+ "Window",
964
+ "window"
965
+ ];
966
+ const KEYS = LIVING_KEYS.concat(OTHER_KEYS);
967
+
968
+ const skipKeys = [
969
+ "window",
970
+ "self",
971
+ "top",
972
+ "parent"
973
+ ];
974
+ function getWindowKeys(global, win, additionalKeys = []) {
975
+ const keysArray = [...additionalKeys, ...KEYS];
976
+ return new Set(keysArray.concat(Object.getOwnPropertyNames(win)).filter((k) => {
977
+ if (skipKeys.includes(k)) return false;
978
+ if (k in global) return keysArray.includes(k);
979
+ return true;
980
+ }));
981
+ }
982
+ function isClassLikeName(name) {
983
+ return name[0] === name[0].toUpperCase();
984
+ }
985
+ function populateGlobal(global, win, options = {}) {
986
+ const { bindFunctions = false } = options;
987
+ const keys = getWindowKeys(global, win, options.additionalKeys);
988
+ const originals = /* @__PURE__ */ new Map();
989
+ const overriddenKeys = new Set([...KEYS, ...options.additionalKeys || []]);
990
+ const overrideObject = /* @__PURE__ */ new Map();
991
+ for (const key of keys) {
992
+ const boundFunction = bindFunctions && typeof win[key] === "function" && !isClassLikeName(key) && win[key].bind(win);
993
+ if (overriddenKeys.has(key) && key in global) originals.set(key, global[key]);
994
+ Object.defineProperty(global, key, {
995
+ get() {
996
+ if (overrideObject.has(key)) return overrideObject.get(key);
997
+ if (boundFunction) return boundFunction;
998
+ return win[key];
999
+ },
1000
+ set(v) {
1001
+ overrideObject.set(key, v);
1002
+ },
1003
+ configurable: true
1004
+ });
1005
+ }
1006
+ global.window = global;
1007
+ global.self = global;
1008
+ global.top = global;
1009
+ global.parent = global;
1010
+ if (global.global) global.global = global;
1011
+ // rewrite defaultView to reference the same global context
1012
+ if (global.document && global.document.defaultView) Object.defineProperty(global.document, "defaultView", {
1013
+ get: () => global,
1014
+ enumerable: true,
1015
+ configurable: true
1016
+ });
1017
+ skipKeys.forEach((k) => keys.add(k));
1018
+ return {
1019
+ keys,
1020
+ skipKeys,
1021
+ originals
1022
+ };
1023
+ }
1024
+
1025
+ var edge = {
1026
+ name: "edge-runtime",
1027
+ viteEnvironment: "ssr",
1028
+ async setupVM() {
1029
+ const { EdgeVM } = await import('@edge-runtime/vm');
1030
+ const vm = new EdgeVM({ extend: (context) => {
1031
+ context.global = context;
1032
+ context.Buffer = Buffer;
1033
+ return context;
1034
+ } });
1035
+ return {
1036
+ getVmContext() {
1037
+ return vm.context;
1038
+ },
1039
+ teardown() {
1040
+ // nothing to teardown
1041
+ }
1042
+ };
1043
+ },
1044
+ async setup(global) {
1045
+ const { EdgeVM } = await import('@edge-runtime/vm');
1046
+ const { keys, originals } = populateGlobal(global, new EdgeVM({ extend: (context) => {
1047
+ context.global = context;
1048
+ context.Buffer = Buffer;
1049
+ KEYS.forEach((key) => {
1050
+ if (key in global) context[key] = global[key];
1051
+ });
1052
+ return context;
1053
+ } }).context, { bindFunctions: true });
1054
+ return { teardown(global) {
1055
+ keys.forEach((key) => delete global[key]);
1056
+ originals.forEach((v, k) => global[k] = v);
1057
+ } };
1058
+ }
1059
+ };
1060
+
1061
+ async function teardownWindow(win) {
1062
+ if (win.close && win.happyDOM.abort) {
1063
+ await win.happyDOM.abort();
1064
+ win.close();
1065
+ } else win.happyDOM.cancelAsync();
1066
+ }
1067
+ var happy = {
1068
+ name: "happy-dom",
1069
+ viteEnvironment: "client",
1070
+ async setupVM({ happyDOM = {} }) {
1071
+ const { Window } = await import('happy-dom');
1072
+ let win = new Window({
1073
+ ...happyDOM,
1074
+ console: console && globalThis.console ? globalThis.console : void 0,
1075
+ url: happyDOM.url || "http://localhost:3000",
1076
+ settings: {
1077
+ ...happyDOM.settings,
1078
+ disableErrorCapturing: true
1079
+ }
1080
+ });
1081
+ // TODO: browser doesn't expose Buffer, but a lot of dependencies use it
1082
+ win.Buffer = Buffer;
1083
+ // inject structuredClone if it exists
1084
+ if (typeof structuredClone !== "undefined" && !win.structuredClone) win.structuredClone = structuredClone;
1085
+ return {
1086
+ getVmContext() {
1087
+ return win;
1088
+ },
1089
+ async teardown() {
1090
+ await teardownWindow(win);
1091
+ win = void 0;
1092
+ }
1093
+ };
1094
+ },
1095
+ async setup(global, { happyDOM = {} }) {
1096
+ // happy-dom v3 introduced a breaking change to Window, but
1097
+ // provides GlobalWindow as a way to use previous behaviour
1098
+ const { Window, GlobalWindow } = await import('happy-dom');
1099
+ const win = new (GlobalWindow || Window)({
1100
+ ...happyDOM,
1101
+ console: console && global.console ? global.console : void 0,
1102
+ url: happyDOM.url || "http://localhost:3000",
1103
+ settings: {
1104
+ ...happyDOM.settings,
1105
+ disableErrorCapturing: true
1106
+ }
1107
+ });
1108
+ const { keys, originals } = populateGlobal(global, win, {
1109
+ bindFunctions: true,
1110
+ additionalKeys: [
1111
+ "Request",
1112
+ "Response",
1113
+ "MessagePort",
1114
+ "fetch",
1115
+ "Headers",
1116
+ "AbortController",
1117
+ "AbortSignal",
1118
+ "URL",
1119
+ "URLSearchParams",
1120
+ "FormData"
1121
+ ]
1122
+ });
1123
+ return { async teardown(global) {
1124
+ await teardownWindow(win);
1125
+ keys.forEach((key) => delete global[key]);
1126
+ originals.forEach((v, k) => global[k] = v);
1127
+ } };
1128
+ }
1129
+ };
1130
+
1131
+ function catchWindowErrors(window) {
1132
+ let userErrorListenerCount = 0;
1133
+ function throwUnhandlerError(e) {
1134
+ if (userErrorListenerCount === 0 && e.error != null) {
1135
+ e.preventDefault();
1136
+ process.emit("uncaughtException", e.error);
1137
+ }
1138
+ }
1139
+ const addEventListener = window.addEventListener.bind(window);
1140
+ const removeEventListener = window.removeEventListener.bind(window);
1141
+ window.addEventListener("error", throwUnhandlerError);
1142
+ window.addEventListener = function(...args) {
1143
+ if (args[0] === "error") userErrorListenerCount++;
1144
+ return addEventListener.apply(this, args);
1145
+ };
1146
+ window.removeEventListener = function(...args) {
1147
+ if (args[0] === "error" && userErrorListenerCount) userErrorListenerCount--;
1148
+ return removeEventListener.apply(this, args);
1149
+ };
1150
+ return function clearErrorHandlers() {
1151
+ window.removeEventListener("error", throwUnhandlerError);
1152
+ };
1153
+ }
1154
+ let NodeFormData_;
1155
+ let NodeBlob_;
1156
+ let NodeRequest_;
1157
+ var jsdom = {
1158
+ name: "jsdom",
1159
+ viteEnvironment: "client",
1160
+ async setupVM({ jsdom = {} }) {
1161
+ // delay initialization because it takes ~1s
1162
+ NodeFormData_ = globalThis.FormData;
1163
+ NodeBlob_ = globalThis.Blob;
1164
+ NodeRequest_ = globalThis.Request;
1165
+ const { CookieJar, JSDOM, ResourceLoader, VirtualConsole } = await import('jsdom');
1166
+ 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;
1167
+ let virtualConsole;
1168
+ if (console && globalThis.console) {
1169
+ virtualConsole = new VirtualConsole();
1170
+ // jsdom <27
1171
+ if ("sendTo" in virtualConsole) virtualConsole.sendTo(globalThis.console);
1172
+ else virtualConsole.forwardTo(globalThis.console);
1173
+ }
1174
+ let dom = new JSDOM(html, {
1175
+ pretendToBeVisual,
1176
+ resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : void 0),
1177
+ runScripts,
1178
+ url,
1179
+ virtualConsole,
1180
+ cookieJar: cookieJar ? new CookieJar() : void 0,
1181
+ includeNodeLocations,
1182
+ contentType,
1183
+ userAgent,
1184
+ ...restOptions
1185
+ });
1186
+ const clearAddEventListenerPatch = patchAddEventListener(dom.window);
1187
+ const clearWindowErrors = catchWindowErrors(dom.window);
1188
+ const utils = createCompatUtils(dom.window);
1189
+ // TODO: browser doesn't expose Buffer, but a lot of dependencies use it
1190
+ dom.window.Buffer = Buffer;
1191
+ dom.window.jsdom = dom;
1192
+ dom.window.Request = createCompatRequest(utils);
1193
+ dom.window.URL = createJSDOMCompatURL(utils);
1194
+ for (const name of [
1195
+ "structuredClone",
1196
+ "BroadcastChannel",
1197
+ "MessageChannel",
1198
+ "MessagePort",
1199
+ "TextEncoder",
1200
+ "TextDecoder"
1201
+ ]) {
1202
+ const value = globalThis[name];
1203
+ if (typeof value !== "undefined" && typeof dom.window[name] === "undefined") dom.window[name] = value;
1204
+ }
1205
+ for (const name of [
1206
+ "fetch",
1207
+ "Response",
1208
+ "Headers",
1209
+ "AbortController",
1210
+ "AbortSignal",
1211
+ "URLSearchParams"
1212
+ ]) {
1213
+ const value = globalThis[name];
1214
+ if (typeof value !== "undefined") dom.window[name] = value;
1215
+ }
1216
+ return {
1217
+ getVmContext() {
1218
+ return dom.getInternalVMContext();
1219
+ },
1220
+ teardown() {
1221
+ clearAddEventListenerPatch();
1222
+ clearWindowErrors();
1223
+ dom.window.close();
1224
+ dom = void 0;
1225
+ }
1226
+ };
1227
+ },
1228
+ async setup(global, { jsdom = {} }) {
1229
+ // delay initialization because it takes ~1s
1230
+ NodeFormData_ = globalThis.FormData;
1231
+ NodeBlob_ = globalThis.Blob;
1232
+ NodeRequest_ = globalThis.Request;
1233
+ const { CookieJar, JSDOM, ResourceLoader, VirtualConsole } = await import('jsdom');
1234
+ 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;
1235
+ let virtualConsole;
1236
+ if (console && globalThis.console) {
1237
+ virtualConsole = new VirtualConsole();
1238
+ // jsdom <27
1239
+ if ("sendTo" in virtualConsole) virtualConsole.sendTo(globalThis.console);
1240
+ else virtualConsole.forwardTo(globalThis.console);
1241
+ }
1242
+ const dom = new JSDOM(html, {
1243
+ pretendToBeVisual,
1244
+ resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : void 0),
1245
+ runScripts,
1246
+ url,
1247
+ virtualConsole,
1248
+ cookieJar: cookieJar ? new CookieJar() : void 0,
1249
+ includeNodeLocations,
1250
+ contentType,
1251
+ userAgent,
1252
+ ...restOptions
1253
+ });
1254
+ const clearAddEventListenerPatch = patchAddEventListener(dom.window);
1255
+ const { keys, originals } = populateGlobal(global, dom.window, { bindFunctions: true });
1256
+ const clearWindowErrors = catchWindowErrors(global);
1257
+ const utils = createCompatUtils(dom.window);
1258
+ global.jsdom = dom;
1259
+ global.Request = createCompatRequest(utils);
1260
+ global.URL = createJSDOMCompatURL(utils);
1261
+ return { teardown(global) {
1262
+ clearAddEventListenerPatch();
1263
+ clearWindowErrors();
1264
+ dom.window.close();
1265
+ delete global.jsdom;
1266
+ keys.forEach((key) => delete global[key]);
1267
+ originals.forEach((v, k) => global[k] = v);
1268
+ } };
1269
+ }
1270
+ };
1271
+ function createCompatRequest(utils) {
1272
+ class Request extends NodeRequest_ {
1273
+ constructor(...args) {
1274
+ const [input, init] = args;
1275
+ if (init?.body != null) {
1276
+ const compatInit = { ...init };
1277
+ if (init.body instanceof utils.window.Blob) compatInit.body = utils.makeCompatBlob(init.body);
1278
+ if (init.body instanceof utils.window.FormData) compatInit.body = utils.makeCompatFormData(init.body);
1279
+ super(input, compatInit);
1280
+ } else super(...args);
1281
+ }
1282
+ static [Symbol.hasInstance](instance) {
1283
+ return instance instanceof NodeRequest_;
1284
+ }
1285
+ }
1286
+ return Request;
1287
+ }
1288
+ function createJSDOMCompatURL(utils) {
1289
+ class URL extends URL$1 {
1290
+ static createObjectURL(blob) {
1291
+ if (blob instanceof utils.window.Blob) {
1292
+ const compatBlob = utils.makeCompatBlob(blob);
1293
+ return URL$1.createObjectURL(compatBlob);
1294
+ }
1295
+ return URL$1.createObjectURL(blob);
1296
+ }
1297
+ static [Symbol.hasInstance](instance) {
1298
+ return instance instanceof URL$1;
1299
+ }
1300
+ }
1301
+ return URL;
1302
+ }
1303
+ function createCompatUtils(window) {
1304
+ // this returns a hidden Symbol(impl)
1305
+ // this is cursed, and jsdom should just implement fetch API itself
1306
+ const implSymbol = Object.getOwnPropertySymbols(Object.getOwnPropertyDescriptors(new window.Blob()))[0];
1307
+ const utils = {
1308
+ window,
1309
+ makeCompatFormData(formData) {
1310
+ const nodeFormData = new NodeFormData_();
1311
+ formData.forEach((value, key) => {
1312
+ if (value instanceof window.Blob) nodeFormData.append(key, utils.makeCompatBlob(value));
1313
+ else nodeFormData.append(key, value);
1314
+ });
1315
+ return nodeFormData;
1316
+ },
1317
+ makeCompatBlob(blob) {
1318
+ const buffer = blob[implSymbol]._buffer;
1319
+ return new NodeBlob_([buffer], { type: blob.type });
1320
+ }
1321
+ };
1322
+ return utils;
1323
+ }
1324
+ function patchAddEventListener(window) {
1325
+ const abortControllers = /* @__PURE__ */ new WeakMap();
1326
+ const JSDOMAbortSignal = window.AbortSignal;
1327
+ const JSDOMAbortController = window.AbortController;
1328
+ const originalAddEventListener = window.EventTarget.prototype.addEventListener;
1329
+ function getJsdomAbortController(signal) {
1330
+ if (!abortControllers.has(signal)) {
1331
+ const jsdomAbortController = new JSDOMAbortController();
1332
+ signal.addEventListener("abort", () => {
1333
+ jsdomAbortController.abort(signal.reason);
1334
+ });
1335
+ abortControllers.set(signal, jsdomAbortController);
1336
+ }
1337
+ return abortControllers.get(signal);
1338
+ }
1339
+ window.EventTarget.prototype.addEventListener = function addEventListener(type, callback, options) {
1340
+ if (typeof options === "object" && options?.signal != null) {
1341
+ const { signal, ...otherOptions } = options;
1342
+ // - this happens because AbortSignal is provided by Node.js,
1343
+ // but jsdom APIs require jsdom's AbortSignal, while Node APIs
1344
+ // (like fetch and Request) require a Node.js AbortSignal
1345
+ // - disable narrow typing with "as any" because we need it later
1346
+ if (!(signal instanceof JSDOMAbortSignal)) {
1347
+ const jsdomCompatOptions = Object.create(null);
1348
+ Object.assign(jsdomCompatOptions, otherOptions);
1349
+ jsdomCompatOptions.signal = getJsdomAbortController(signal).signal;
1350
+ return originalAddEventListener.call(this, type, callback, jsdomCompatOptions);
1351
+ }
1352
+ }
1353
+ return originalAddEventListener.call(this, type, callback, options);
1354
+ };
1355
+ return () => {
1356
+ window.EventTarget.prototype.addEventListener = originalAddEventListener;
1357
+ };
1358
+ }
1359
+
1360
+ // some globals we do not want, either because deprecated or we set it ourselves
1361
+ const denyList = new Set([
1362
+ "GLOBAL",
1363
+ "root",
1364
+ "global",
1365
+ "Buffer",
1366
+ "ArrayBuffer",
1367
+ "Uint8Array"
1368
+ ]);
1369
+ const nodeGlobals = /* @__PURE__ */ new Map();
1370
+ function populateNodeGlobals() {
1371
+ if (nodeGlobals.size !== 0) return;
1372
+ const names = Object.getOwnPropertyNames(globalThis);
1373
+ const length = names.length;
1374
+ for (let i = 0; i < length; i++) {
1375
+ const globalName = names[i];
1376
+ if (!denyList.has(globalName)) {
1377
+ const descriptor = Object.getOwnPropertyDescriptor(globalThis, globalName);
1378
+ if (!descriptor) throw new Error(`No property descriptor for ${globalName}, this is a bug in Vitest.`);
1379
+ nodeGlobals.set(globalName, descriptor);
1380
+ }
1381
+ }
1382
+ }
1383
+ var node = {
1384
+ name: "node",
1385
+ viteEnvironment: "ssr",
1386
+ async setupVM() {
1387
+ populateNodeGlobals();
1388
+ const vm = await import('node:vm');
1389
+ let context = vm.createContext();
1390
+ let global = vm.runInContext("this", context);
1391
+ const contextGlobals = new Set(Object.getOwnPropertyNames(global));
1392
+ for (const [nodeGlobalsKey, descriptor] of nodeGlobals) if (!contextGlobals.has(nodeGlobalsKey)) if (descriptor.configurable) Object.defineProperty(global, nodeGlobalsKey, {
1393
+ configurable: true,
1394
+ enumerable: descriptor.enumerable,
1395
+ get() {
1396
+ // @ts-expect-error: no index signature
1397
+ const val = globalThis[nodeGlobalsKey];
1398
+ // override lazy getter
1399
+ Object.defineProperty(global, nodeGlobalsKey, {
1400
+ configurable: true,
1401
+ enumerable: descriptor.enumerable,
1402
+ value: val,
1403
+ writable: descriptor.writable === true || nodeGlobalsKey === "performance"
1404
+ });
1405
+ return val;
1406
+ },
1407
+ set(val) {
1408
+ // override lazy getter
1409
+ Object.defineProperty(global, nodeGlobalsKey, {
1410
+ configurable: true,
1411
+ enumerable: descriptor.enumerable,
1412
+ value: val,
1413
+ writable: true
1414
+ });
1415
+ }
1416
+ });
1417
+ else if ("value" in descriptor) Object.defineProperty(global, nodeGlobalsKey, {
1418
+ configurable: false,
1419
+ enumerable: descriptor.enumerable,
1420
+ value: descriptor.value,
1421
+ writable: descriptor.writable
1422
+ });
1423
+ else Object.defineProperty(global, nodeGlobalsKey, {
1424
+ configurable: false,
1425
+ enumerable: descriptor.enumerable,
1426
+ get: descriptor.get,
1427
+ set: descriptor.set
1428
+ });
1429
+ global.global = global;
1430
+ global.Buffer = Buffer;
1431
+ global.ArrayBuffer = ArrayBuffer;
1432
+ // TextEncoder (global or via 'util') references a Uint8Array constructor
1433
+ // different than the global one used by users in tests. This makes sure the
1434
+ // same constructor is referenced by both.
1435
+ global.Uint8Array = Uint8Array;
1436
+ return {
1437
+ getVmContext() {
1438
+ return context;
1439
+ },
1440
+ teardown() {
1441
+ context = void 0;
1442
+ global = void 0;
1443
+ }
1444
+ };
1445
+ },
1446
+ async setup(global) {
1447
+ global.console.Console = Console;
1448
+ return { teardown(global) {
1449
+ delete global.console.Console;
1450
+ } };
1451
+ }
1452
+ };
1453
+
1454
+ const environments = {
1455
+ node,
1456
+ jsdom,
1457
+ "happy-dom": happy,
1458
+ "edge-runtime": edge
1459
+ };
1460
+
1461
+ export { BareModuleMocker as B, VITEST_VM_CONTEXT_SYMBOL as V, VitestModuleRunner as a, VitestTransport as b, createNodeImportMeta as c, environments as e, normalizeModuleId as n, populateGlobal as p, startVitestModuleRunner as s };