@vitest/browser 1.2.1 → 1.3.0
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.
- package/dist/client/__vitest__/assets/index-AUm0OrLY.js +35 -0
- package/dist/client/__vitest__/assets/index-iPSQW1bz.css +1 -0
- package/dist/client/__vitest__/index.html +2 -2
- package/dist/client/__vitest_browser__/main-zbvBuTsR.js +98 -0
- package/dist/client/__vitest_browser__/{index-30Y0Db8-.js → rpc-aQLc-7Wy.js} +122 -487
- package/dist/client/__vitest_browser__/tester-W7irZcd1.js +438 -0
- package/dist/client/esm-client-injector.js +61 -0
- package/dist/client/index.html +5 -52
- package/dist/client/tester.html +25 -0
- package/dist/index.js +217 -28
- package/dist/providers.js +0 -18
- package/package.json +13 -13
- package/dist/client/__vitest__/assets/index-BxfaIJso.js +0 -35
- package/dist/client/__vitest__/assets/index-b8y7ovCb.css +0 -1
|
@@ -45,68 +45,17 @@ var __publicField = (obj, key, value) => {
|
|
|
45
45
|
fetch(link.href, fetchOpts);
|
|
46
46
|
}
|
|
47
47
|
})();
|
|
48
|
-
const scriptRel = "modulepreload";
|
|
49
|
-
const assetsURL = function(dep) {
|
|
50
|
-
return "/" + dep;
|
|
51
|
-
};
|
|
52
|
-
const seen = {};
|
|
53
|
-
const __vitePreload = function preload(baseModule, deps, importerUrl) {
|
|
54
|
-
if (!deps || deps.length === 0) {
|
|
55
|
-
return baseModule();
|
|
56
|
-
}
|
|
57
|
-
const links = document.getElementsByTagName("link");
|
|
58
|
-
return Promise.all(deps.map((dep) => {
|
|
59
|
-
dep = assetsURL(dep);
|
|
60
|
-
if (dep in seen)
|
|
61
|
-
return;
|
|
62
|
-
seen[dep] = true;
|
|
63
|
-
const isCss = dep.endsWith(".css");
|
|
64
|
-
const cssSelector = isCss ? '[rel="stylesheet"]' : "";
|
|
65
|
-
const isBaseRelative = !!importerUrl;
|
|
66
|
-
if (isBaseRelative) {
|
|
67
|
-
for (let i = links.length - 1; i >= 0; i--) {
|
|
68
|
-
const link2 = links[i];
|
|
69
|
-
if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
} else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
const link = document.createElement("link");
|
|
77
|
-
link.rel = isCss ? "stylesheet" : scriptRel;
|
|
78
|
-
if (!isCss) {
|
|
79
|
-
link.as = "script";
|
|
80
|
-
link.crossOrigin = "";
|
|
81
|
-
}
|
|
82
|
-
link.href = dep;
|
|
83
|
-
document.head.appendChild(link);
|
|
84
|
-
if (isCss) {
|
|
85
|
-
return new Promise((res, rej) => {
|
|
86
|
-
link.addEventListener("load", res);
|
|
87
|
-
link.addEventListener("error", () => rej(new Error(`Unable to preload CSS for ${dep}`)));
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
})).then(() => baseModule()).catch((err) => {
|
|
91
|
-
const e = new Event("vite:preloadError", { cancelable: true });
|
|
92
|
-
e.payload = err;
|
|
93
|
-
window.dispatchEvent(e);
|
|
94
|
-
if (!e.defaultPrevented) {
|
|
95
|
-
throw err;
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
48
|
const DEFAULT_TIMEOUT = 6e4;
|
|
100
49
|
function defaultSerialize(i) {
|
|
101
50
|
return i;
|
|
102
51
|
}
|
|
103
52
|
const defaultDeserialize = defaultSerialize;
|
|
104
|
-
const { clearTimeout, setTimeout: setTimeout$1 } = globalThis;
|
|
53
|
+
const { clearTimeout: clearTimeout$1, setTimeout: setTimeout$1 } = globalThis;
|
|
105
54
|
const random = Math.random.bind(Math);
|
|
106
55
|
function createBirpc(functions, options) {
|
|
107
56
|
const {
|
|
108
57
|
post,
|
|
109
|
-
on
|
|
58
|
+
on,
|
|
110
59
|
eventNames = [],
|
|
111
60
|
serialize = defaultSerialize,
|
|
112
61
|
deserialize = defaultDeserialize,
|
|
@@ -134,7 +83,13 @@ function createBirpc(functions, options) {
|
|
|
134
83
|
let timeoutId;
|
|
135
84
|
if (timeout >= 0) {
|
|
136
85
|
timeoutId = (_b = (_a = setTimeout$1(() => {
|
|
137
|
-
|
|
86
|
+
var _a2;
|
|
87
|
+
try {
|
|
88
|
+
(_a2 = options.onTimeoutError) == null ? void 0 : _a2.call(options, method, args);
|
|
89
|
+
throw new Error(`[birpc] timeout on calling "${method}"`);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
reject(e);
|
|
92
|
+
}
|
|
138
93
|
rpcPromiseMap.delete(id);
|
|
139
94
|
}, timeout)).unref) == null ? void 0 : _b.call(_a);
|
|
140
95
|
}
|
|
@@ -146,7 +101,7 @@ function createBirpc(functions, options) {
|
|
|
146
101
|
return sendCall;
|
|
147
102
|
}
|
|
148
103
|
});
|
|
149
|
-
_promise =
|
|
104
|
+
_promise = on(async (data, ...extra) => {
|
|
150
105
|
const msg = deserialize(data);
|
|
151
106
|
if (msg.t === "q") {
|
|
152
107
|
const { m: method, a: args } = msg;
|
|
@@ -170,7 +125,7 @@ function createBirpc(functions, options) {
|
|
|
170
125
|
const { i: ack, r: result, e: error } = msg;
|
|
171
126
|
const promise = rpcPromiseMap.get(ack);
|
|
172
127
|
if (promise) {
|
|
173
|
-
clearTimeout(promise.timeoutId);
|
|
128
|
+
clearTimeout$1(promise.timeoutId);
|
|
174
129
|
if (error)
|
|
175
130
|
promise.reject(error);
|
|
176
131
|
else
|
|
@@ -378,7 +333,6 @@ class StateManager {
|
|
|
378
333
|
constructor() {
|
|
379
334
|
__publicField(this, "filesMap", /* @__PURE__ */ new Map());
|
|
380
335
|
__publicField(this, "pathsSet", /* @__PURE__ */ new Set());
|
|
381
|
-
__publicField(this, "browserTestPromises", /* @__PURE__ */ new Map());
|
|
382
336
|
__publicField(this, "idMap", /* @__PURE__ */ new Map());
|
|
383
337
|
__publicField(this, "taskFileMap", /* @__PURE__ */ new WeakMap());
|
|
384
338
|
__publicField(this, "errorsSet", /* @__PURE__ */ new Set());
|
|
@@ -512,18 +466,19 @@ class StateManager {
|
|
|
512
466
|
})));
|
|
513
467
|
}
|
|
514
468
|
}
|
|
515
|
-
function createClient(
|
|
469
|
+
function createClient(url, options = {}) {
|
|
516
470
|
const {
|
|
517
471
|
handlers = {},
|
|
518
472
|
autoReconnect = true,
|
|
519
473
|
reconnectInterval = 2e3,
|
|
520
474
|
reconnectTries = 10,
|
|
475
|
+
connectTimeout = 6e4,
|
|
521
476
|
reactive = (v) => v,
|
|
522
477
|
WebSocketConstructor = globalThis.WebSocket
|
|
523
478
|
} = options;
|
|
524
479
|
let tries = reconnectTries;
|
|
525
480
|
const ctx = reactive({
|
|
526
|
-
ws: new WebSocketConstructor(
|
|
481
|
+
ws: new WebSocketConstructor(url),
|
|
527
482
|
state: new StateManager(),
|
|
528
483
|
waitForConnection,
|
|
529
484
|
reconnect
|
|
@@ -563,7 +518,10 @@ function createClient(url2, options = {}) {
|
|
|
563
518
|
post: (msg) => ctx.ws.send(msg),
|
|
564
519
|
on: (fn) => onMessage = fn,
|
|
565
520
|
serialize: stringify,
|
|
566
|
-
deserialize: parse
|
|
521
|
+
deserialize: parse,
|
|
522
|
+
onTimeoutError(functionName) {
|
|
523
|
+
throw new Error(`[vitest-ws-client]: Timeout calling "${functionName}"`);
|
|
524
|
+
}
|
|
567
525
|
};
|
|
568
526
|
ctx.rpc = createBirpc(
|
|
569
527
|
functions,
|
|
@@ -573,14 +531,21 @@ function createClient(url2, options = {}) {
|
|
|
573
531
|
function reconnect(reset = false) {
|
|
574
532
|
if (reset)
|
|
575
533
|
tries = reconnectTries;
|
|
576
|
-
ctx.ws = new WebSocketConstructor(
|
|
534
|
+
ctx.ws = new WebSocketConstructor(url);
|
|
577
535
|
registerWS();
|
|
578
536
|
}
|
|
579
537
|
function registerWS() {
|
|
580
|
-
openPromise = new Promise((resolve2) => {
|
|
538
|
+
openPromise = new Promise((resolve2, reject) => {
|
|
539
|
+
var _a, _b;
|
|
540
|
+
const timeout = (_b = (_a = setTimeout(() => {
|
|
541
|
+
reject(new Error(`Cannot connect to the server in ${connectTimeout / 1e3} seconds`));
|
|
542
|
+
}, connectTimeout)) == null ? void 0 : _a.unref) == null ? void 0 : _b.call(_a);
|
|
543
|
+
if (ctx.ws.OPEN === ctx.ws.readyState)
|
|
544
|
+
resolve2();
|
|
581
545
|
ctx.ws.addEventListener("open", () => {
|
|
582
546
|
tries = reconnectTries;
|
|
583
547
|
resolve2();
|
|
548
|
+
clearTimeout(timeout);
|
|
584
549
|
});
|
|
585
550
|
});
|
|
586
551
|
ctx.ws.addEventListener("message", (v) => {
|
|
@@ -598,13 +563,81 @@ function createClient(url2, options = {}) {
|
|
|
598
563
|
}
|
|
599
564
|
return ctx;
|
|
600
565
|
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
566
|
+
const PORT = location.port;
|
|
567
|
+
const HOST = [location.hostname, PORT].filter(Boolean).join(":");
|
|
568
|
+
const ENTRY_URL = `${location.protocol === "https:" ? "wss:" : "ws:"}//${HOST}/__vitest_api__`;
|
|
569
|
+
let setCancel = (_) => {
|
|
570
|
+
};
|
|
571
|
+
const onCancel = new Promise((resolve2) => {
|
|
572
|
+
setCancel = resolve2;
|
|
573
|
+
});
|
|
574
|
+
const client = createClient(ENTRY_URL, {
|
|
575
|
+
handlers: {
|
|
576
|
+
onCancel: setCancel
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
const channel = new BroadcastChannel("vitest");
|
|
580
|
+
const scriptRel = "modulepreload";
|
|
581
|
+
const assetsURL = function(dep) {
|
|
582
|
+
return "/" + dep;
|
|
583
|
+
};
|
|
584
|
+
const seen = {};
|
|
585
|
+
const __vitePreload = function preload(baseModule, deps, importerUrl) {
|
|
586
|
+
let promise = Promise.resolve();
|
|
587
|
+
if (deps && deps.length > 0) {
|
|
588
|
+
const links = document.getElementsByTagName("link");
|
|
589
|
+
promise = Promise.all(deps.map((dep) => {
|
|
590
|
+
dep = assetsURL(dep);
|
|
591
|
+
if (dep in seen)
|
|
592
|
+
return;
|
|
593
|
+
seen[dep] = true;
|
|
594
|
+
const isCss = dep.endsWith(".css");
|
|
595
|
+
const cssSelector = isCss ? '[rel="stylesheet"]' : "";
|
|
596
|
+
const isBaseRelative = !!importerUrl;
|
|
597
|
+
if (isBaseRelative) {
|
|
598
|
+
for (let i = links.length - 1; i >= 0; i--) {
|
|
599
|
+
const link2 = links[i];
|
|
600
|
+
if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) {
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
} else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
const link = document.createElement("link");
|
|
608
|
+
link.rel = isCss ? "stylesheet" : scriptRel;
|
|
609
|
+
if (!isCss) {
|
|
610
|
+
link.as = "script";
|
|
611
|
+
link.crossOrigin = "";
|
|
612
|
+
}
|
|
613
|
+
link.href = dep;
|
|
614
|
+
document.head.appendChild(link);
|
|
615
|
+
if (isCss) {
|
|
616
|
+
return new Promise((res, rej) => {
|
|
617
|
+
link.addEventListener("load", res);
|
|
618
|
+
link.addEventListener("error", () => rej(new Error(`Unable to preload CSS for ${dep}`)));
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
}));
|
|
622
|
+
}
|
|
623
|
+
return promise.then(() => baseModule()).catch((err) => {
|
|
624
|
+
const e = new Event("vite:preloadError", { cancelable: true });
|
|
625
|
+
e.payload = err;
|
|
626
|
+
window.dispatchEvent(e);
|
|
627
|
+
if (!e.defaultPrevented) {
|
|
628
|
+
throw err;
|
|
629
|
+
}
|
|
630
|
+
});
|
|
631
|
+
};
|
|
632
|
+
async function importId(id) {
|
|
633
|
+
const name = `${getConfig().base || "/"}@id/${id}`;
|
|
634
|
+
return getBrowserState().wrapModule(__vitePreload(() => import(name), true ? __vite__mapDeps([]) : void 0));
|
|
635
|
+
}
|
|
636
|
+
function getConfig() {
|
|
637
|
+
return getBrowserState().config;
|
|
638
|
+
}
|
|
639
|
+
function getBrowserState() {
|
|
640
|
+
return window.__vitest_browser_runner__;
|
|
608
641
|
}
|
|
609
642
|
const { get } = Reflect;
|
|
610
643
|
function withSafeTimers(getTimers, fn) {
|
|
@@ -637,6 +670,8 @@ async function rpcDone() {
|
|
|
637
670
|
function createSafeRpc(client2, getTimers) {
|
|
638
671
|
return new Proxy(client2.rpc, {
|
|
639
672
|
get(target, p, handler) {
|
|
673
|
+
if (p === "then")
|
|
674
|
+
return;
|
|
640
675
|
const sendCall = get(target, p, handler);
|
|
641
676
|
const safeSendCall = (...args) => withSafeTimers(getTimers, async () => {
|
|
642
677
|
const result = sendCall(...args);
|
|
@@ -652,428 +687,28 @@ function createSafeRpc(client2, getTimers) {
|
|
|
652
687
|
}
|
|
653
688
|
});
|
|
654
689
|
}
|
|
655
|
-
function
|
|
656
|
-
|
|
690
|
+
async function loadSafeRpc(client2) {
|
|
691
|
+
const { getSafeTimers } = await importId("vitest/utils");
|
|
692
|
+
return createSafeRpc(client2, getSafeTimers);
|
|
657
693
|
}
|
|
658
|
-
function
|
|
659
|
-
return
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
const currentFailures = 1 + previousFailures;
|
|
673
|
-
if (currentFailures >= this.config.bail) {
|
|
674
|
-
rpc().onCancel("test-failure");
|
|
675
|
-
(_c = this.onCancel) == null ? void 0 : _c.call(this, "test-failure");
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
async onAfterRunFiles(files) {
|
|
680
|
-
var _a, _b;
|
|
681
|
-
await ((_a = super.onAfterRunFiles) == null ? void 0 : _a.call(this, files));
|
|
682
|
-
const coverage = await ((_b = coverageModule == null ? void 0 : coverageModule.takeCoverage) == null ? void 0 : _b.call(coverageModule));
|
|
683
|
-
if (coverage) {
|
|
684
|
-
await rpc().onAfterSuiteRun({
|
|
685
|
-
coverage,
|
|
686
|
-
transformMode: "web",
|
|
687
|
-
projectName: this.config.name
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
onCollected(files) {
|
|
692
|
-
return rpc().onCollected(files);
|
|
693
|
-
}
|
|
694
|
-
onTaskUpdate(task) {
|
|
695
|
-
return rpc().onTaskUpdate(task);
|
|
696
|
-
}
|
|
697
|
-
async importFile(filepath) {
|
|
698
|
-
let [test, hash] = this.hashMap.get(filepath) ?? [false, ""];
|
|
699
|
-
if (hash === "") {
|
|
700
|
-
hash = Date.now().toString();
|
|
701
|
-
this.hashMap.set(filepath, [false, hash]);
|
|
702
|
-
}
|
|
703
|
-
const base = this.config.base || "/";
|
|
704
|
-
const prefix = `${base}${/^\w:/.test(filepath) ? "@fs/" : ""}`;
|
|
705
|
-
const query = `${test ? "browserv" : "v"}=${hash}`;
|
|
706
|
-
const importpath = `${prefix}${filepath}?${query}`.replace(/\/+/g, "/");
|
|
707
|
-
await __vitePreload(() => import(importpath), true ? __vite__mapDeps([]) : void 0);
|
|
708
|
-
}
|
|
709
|
-
};
|
|
710
|
-
}
|
|
711
|
-
async function importId$1(id, basePath2) {
|
|
712
|
-
const name = `${basePath2}@id/${id}`;
|
|
713
|
-
return __vi_wrap_module__(__vitePreload(() => import(name), true ? __vite__mapDeps([]) : void 0));
|
|
714
|
-
}
|
|
715
|
-
const { Date: Date$1, console: console$1 } = globalThis;
|
|
716
|
-
async function setupConsoleLogSpy(basePath2) {
|
|
717
|
-
const { stringify: stringify2, format, inspect } = await importId$1("vitest/utils", basePath2);
|
|
718
|
-
const { log, info, error, dir, dirxml, trace, time, timeEnd, timeLog, warn, debug, count, countReset } = console$1;
|
|
719
|
-
const formatInput = (input) => {
|
|
720
|
-
if (input instanceof Node)
|
|
721
|
-
return stringify2(input);
|
|
722
|
-
return format(input);
|
|
723
|
-
};
|
|
724
|
-
const processLog = (args) => args.map(formatInput).join(" ");
|
|
725
|
-
const sendLog = (type, content) => {
|
|
726
|
-
var _a, _b;
|
|
727
|
-
if (content.startsWith("[vite]"))
|
|
728
|
-
return;
|
|
729
|
-
const unknownTestId = "__vitest__unknown_test__";
|
|
730
|
-
const taskId = ((_b = (_a = globalThis.__vitest_worker__) == null ? void 0 : _a.current) == null ? void 0 : _b.id) ?? unknownTestId;
|
|
731
|
-
rpc().sendLog({
|
|
732
|
-
content,
|
|
733
|
-
time: Date$1.now(),
|
|
734
|
-
taskId,
|
|
735
|
-
type,
|
|
736
|
-
size: content.length
|
|
737
|
-
});
|
|
738
|
-
};
|
|
739
|
-
const stdout = (base) => (...args) => {
|
|
740
|
-
sendLog("stdout", processLog(args));
|
|
741
|
-
return base(...args);
|
|
742
|
-
};
|
|
743
|
-
const stderr = (base) => (...args) => {
|
|
744
|
-
sendLog("stderr", processLog(args));
|
|
745
|
-
return base(...args);
|
|
746
|
-
};
|
|
747
|
-
console$1.log = stdout(log);
|
|
748
|
-
console$1.debug = stdout(debug);
|
|
749
|
-
console$1.info = stdout(info);
|
|
750
|
-
console$1.error = stderr(error);
|
|
751
|
-
console$1.warn = stderr(warn);
|
|
752
|
-
console$1.dir = (item, options) => {
|
|
753
|
-
sendLog("stdout", inspect(item, options));
|
|
754
|
-
return dir(item, options);
|
|
755
|
-
};
|
|
756
|
-
console$1.dirxml = (...args) => {
|
|
757
|
-
sendLog("stdout", processLog(args));
|
|
758
|
-
return dirxml(...args);
|
|
759
|
-
};
|
|
760
|
-
console$1.trace = (...args) => {
|
|
761
|
-
const content = processLog(args);
|
|
762
|
-
const error2 = new Error("Trace");
|
|
763
|
-
const stack = (error2.stack || "").split("\n").slice(2).join("\n");
|
|
764
|
-
sendLog("stdout", `${content}
|
|
765
|
-
${stack}`);
|
|
766
|
-
return trace(...args);
|
|
767
|
-
};
|
|
768
|
-
const timeLabels = {};
|
|
769
|
-
console$1.time = (label = "default") => {
|
|
770
|
-
const now = performance.now();
|
|
771
|
-
time(label);
|
|
772
|
-
timeLabels[label] = now;
|
|
773
|
-
};
|
|
774
|
-
console$1.timeLog = (label = "default") => {
|
|
775
|
-
timeLog(label);
|
|
776
|
-
if (!(label in timeLabels))
|
|
777
|
-
sendLog("stderr", `Timer "${label}" does not exist`);
|
|
778
|
-
else
|
|
779
|
-
sendLog("stdout", `${label}: ${timeLabels[label]} ms`);
|
|
780
|
-
};
|
|
781
|
-
console$1.timeEnd = (label = "default") => {
|
|
782
|
-
const end = performance.now();
|
|
783
|
-
timeEnd(label);
|
|
784
|
-
const start = timeLabels[label];
|
|
785
|
-
if (!(label in timeLabels)) {
|
|
786
|
-
sendLog("stderr", `Timer "${label}" does not exist`);
|
|
787
|
-
} else if (start) {
|
|
788
|
-
const duration = end - start;
|
|
789
|
-
sendLog("stdout", `${label}: ${duration} ms`);
|
|
790
|
-
}
|
|
791
|
-
};
|
|
792
|
-
const countLabels = {};
|
|
793
|
-
console$1.count = (label = "default") => {
|
|
794
|
-
const counter = (countLabels[label] ?? 0) + 1;
|
|
795
|
-
countLabels[label] = counter;
|
|
796
|
-
sendLog("stdout", `${label}: ${counter}`);
|
|
797
|
-
return count(label);
|
|
798
|
-
};
|
|
799
|
-
console$1.countReset = (label = "default") => {
|
|
800
|
-
countLabels[label] = 0;
|
|
801
|
-
return countReset(label);
|
|
802
|
-
};
|
|
803
|
-
}
|
|
804
|
-
function showPopupWarning(name, value, defaultValue) {
|
|
805
|
-
return (...params) => {
|
|
806
|
-
const formatedParams = params.map((p) => JSON.stringify(p)).join(", ");
|
|
807
|
-
console.warn(`Vitest encountered a \`${name}(${formatedParams})\` call that it cannot handle by default, so it returned \`${value}\`. Read more in https://vitest.dev/guide/browser#thread-blocking-dialogs.
|
|
808
|
-
If needed, mock the \`${name}\` call manually like:
|
|
809
|
-
|
|
810
|
-
\`\`\`
|
|
811
|
-
import { expect, vi } from "vitest"
|
|
812
|
-
|
|
813
|
-
vi.spyOn(window, "${name}")${defaultValue ? `.mockReturnValue(${JSON.stringify(defaultValue)})` : ""}
|
|
814
|
-
${name}(${formatedParams})
|
|
815
|
-
expect(${name}).toHaveBeenCalledWith(${formatedParams})
|
|
816
|
-
\`\`\``);
|
|
817
|
-
return value;
|
|
818
|
-
};
|
|
819
|
-
}
|
|
820
|
-
function setupDialogsSpy() {
|
|
821
|
-
globalThis.alert = showPopupWarning("alert", void 0);
|
|
822
|
-
globalThis.confirm = showPopupWarning("confirm", false, true);
|
|
823
|
-
globalThis.prompt = showPopupWarning("prompt", null, "your value");
|
|
824
|
-
}
|
|
825
|
-
class BrowserSnapshotEnvironment {
|
|
826
|
-
getVersion() {
|
|
827
|
-
return "1";
|
|
828
|
-
}
|
|
829
|
-
getHeader() {
|
|
830
|
-
return `// Vitest Snapshot v${this.getVersion()}, https://vitest.dev/guide/snapshot.html`;
|
|
831
|
-
}
|
|
832
|
-
readSnapshotFile(filepath) {
|
|
833
|
-
return rpc().readSnapshotFile(filepath);
|
|
834
|
-
}
|
|
835
|
-
saveSnapshotFile(filepath, snapshot) {
|
|
836
|
-
return rpc().saveSnapshotFile(filepath, snapshot);
|
|
837
|
-
}
|
|
838
|
-
resolvePath(filepath) {
|
|
839
|
-
return rpc().resolveSnapshotPath(filepath);
|
|
840
|
-
}
|
|
841
|
-
resolveRawPath(testPath, rawPath) {
|
|
842
|
-
return rpc().resolveSnapshotRawPath(testPath, rawPath);
|
|
843
|
-
}
|
|
844
|
-
removeSnapshotFile(filepath) {
|
|
845
|
-
return rpc().removeSnapshotFile(filepath);
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
function throwNotImplemented(name) {
|
|
849
|
-
throw new Error(`[vitest] ${name} is not implemented in browser environment yet.`);
|
|
850
|
-
}
|
|
851
|
-
class VitestBrowserClientMocker {
|
|
852
|
-
importActual() {
|
|
853
|
-
throwNotImplemented("importActual");
|
|
854
|
-
}
|
|
855
|
-
importMock() {
|
|
856
|
-
throwNotImplemented("importMock");
|
|
857
|
-
}
|
|
858
|
-
queueMock() {
|
|
859
|
-
throwNotImplemented("queueMock");
|
|
860
|
-
}
|
|
861
|
-
queueUnmock() {
|
|
862
|
-
throwNotImplemented("queueUnmock");
|
|
863
|
-
}
|
|
864
|
-
prepare() {
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
|
-
const PORT = location.port;
|
|
868
|
-
const HOST = [location.hostname, PORT].filter(Boolean).join(":");
|
|
869
|
-
const ENTRY_URL = `${location.protocol === "https:" ? "wss:" : "ws:"}//${HOST}/__vitest_api__`;
|
|
870
|
-
let config;
|
|
871
|
-
let runner;
|
|
872
|
-
const browserHashMap = /* @__PURE__ */ new Map();
|
|
873
|
-
const url = new URL(location.href);
|
|
874
|
-
const testId = url.searchParams.get("id") || "unknown";
|
|
875
|
-
const reloadTries = Number(url.searchParams.get("reloadTries") || "0");
|
|
876
|
-
const basePath = () => (config == null ? void 0 : config.base) || "/";
|
|
877
|
-
const importId = (id) => importId$1(id, basePath());
|
|
878
|
-
const viteClientPath = () => `${basePath()}@vite/client`;
|
|
879
|
-
function getQueryPaths() {
|
|
880
|
-
return url.searchParams.getAll("path");
|
|
881
|
-
}
|
|
882
|
-
let setCancel = (_) => {
|
|
694
|
+
function rpc() {
|
|
695
|
+
return globalThis.__vitest_worker__.rpc;
|
|
696
|
+
}
|
|
697
|
+
export {
|
|
698
|
+
__vitePreload as _,
|
|
699
|
+
getBrowserState as a,
|
|
700
|
+
channel as b,
|
|
701
|
+
client as c,
|
|
702
|
+
rpc as d,
|
|
703
|
+
getConfig as g,
|
|
704
|
+
importId as i,
|
|
705
|
+
loadSafeRpc as l,
|
|
706
|
+
onCancel as o,
|
|
707
|
+
rpcDone as r
|
|
883
708
|
};
|
|
884
|
-
const onCancel = new Promise((resolve2) => {
|
|
885
|
-
setCancel = resolve2;
|
|
886
|
-
});
|
|
887
|
-
const client = createClient(ENTRY_URL, {
|
|
888
|
-
handlers: {
|
|
889
|
-
onCancel: setCancel
|
|
890
|
-
}
|
|
891
|
-
});
|
|
892
|
-
const ws = client.ws;
|
|
893
|
-
async function loadConfig() {
|
|
894
|
-
let retries = 5;
|
|
895
|
-
do {
|
|
896
|
-
try {
|
|
897
|
-
await new Promise((resolve2) => setTimeout(resolve2, 150));
|
|
898
|
-
config = await client.rpc.getConfig();
|
|
899
|
-
config = unwrapConfig(config);
|
|
900
|
-
return;
|
|
901
|
-
} catch (_) {
|
|
902
|
-
}
|
|
903
|
-
} while (--retries > 0);
|
|
904
|
-
throw new Error("cannot load configuration after 5 retries");
|
|
905
|
-
}
|
|
906
|
-
function unwrapConfig(config2) {
|
|
907
|
-
return {
|
|
908
|
-
...config2,
|
|
909
|
-
// workaround RegExp serialization
|
|
910
|
-
testNamePattern: config2.testNamePattern ? parseRegexp(config2.testNamePattern) : void 0
|
|
911
|
-
};
|
|
912
|
-
}
|
|
913
|
-
function on(event, listener) {
|
|
914
|
-
window.addEventListener(event, listener);
|
|
915
|
-
return () => window.removeEventListener(event, listener);
|
|
916
|
-
}
|
|
917
|
-
function serializeError(unhandledError) {
|
|
918
|
-
return {
|
|
919
|
-
...unhandledError,
|
|
920
|
-
name: unhandledError.name,
|
|
921
|
-
message: unhandledError.message,
|
|
922
|
-
stack: String(unhandledError.stack)
|
|
923
|
-
};
|
|
924
|
-
}
|
|
925
|
-
async function defaultErrorReport(type, unhandledError) {
|
|
926
|
-
const error = serializeError(unhandledError);
|
|
927
|
-
if (testId !== "no-isolate")
|
|
928
|
-
error.VITEST_TEST_PATH = testId;
|
|
929
|
-
await client.rpc.onUnhandledError(error, type);
|
|
930
|
-
await client.rpc.onDone(testId);
|
|
931
|
-
}
|
|
932
|
-
function catchWindowErrors(cb) {
|
|
933
|
-
let userErrorListenerCount = 0;
|
|
934
|
-
function throwUnhandlerError(e) {
|
|
935
|
-
if (userErrorListenerCount === 0 && e.error != null)
|
|
936
|
-
cb(e);
|
|
937
|
-
else
|
|
938
|
-
console.error(e.error);
|
|
939
|
-
}
|
|
940
|
-
const addEventListener = window.addEventListener.bind(window);
|
|
941
|
-
const removeEventListener = window.removeEventListener.bind(window);
|
|
942
|
-
window.addEventListener("error", throwUnhandlerError);
|
|
943
|
-
window.addEventListener = function(...args) {
|
|
944
|
-
if (args[0] === "error")
|
|
945
|
-
userErrorListenerCount++;
|
|
946
|
-
return addEventListener.apply(this, args);
|
|
947
|
-
};
|
|
948
|
-
window.removeEventListener = function(...args) {
|
|
949
|
-
if (args[0] === "error" && userErrorListenerCount)
|
|
950
|
-
userErrorListenerCount--;
|
|
951
|
-
return removeEventListener.apply(this, args);
|
|
952
|
-
};
|
|
953
|
-
return function clearErrorHandlers() {
|
|
954
|
-
window.removeEventListener("error", throwUnhandlerError);
|
|
955
|
-
};
|
|
956
|
-
}
|
|
957
|
-
const stopErrorHandler = catchWindowErrors((e) => defaultErrorReport("Error", e.error));
|
|
958
|
-
const stopRejectionHandler = on("unhandledrejection", (e) => defaultErrorReport("Unhandled Rejection", e.reason));
|
|
959
|
-
let runningTests = false;
|
|
960
|
-
async function reportUnexpectedError(rpc2, type, error) {
|
|
961
|
-
const { processError } = await importId("vitest/browser");
|
|
962
|
-
const processedError = processError(error);
|
|
963
|
-
if (testId !== "no-isolate")
|
|
964
|
-
error.VITEST_TEST_PATH = testId;
|
|
965
|
-
await rpc2.onUnhandledError(processedError, type);
|
|
966
|
-
if (!runningTests)
|
|
967
|
-
await rpc2.onDone(testId);
|
|
968
|
-
}
|
|
969
|
-
ws.addEventListener("open", async () => {
|
|
970
|
-
await loadConfig();
|
|
971
|
-
let safeRpc;
|
|
972
|
-
try {
|
|
973
|
-
const { getSafeTimers } = await importId("vitest/utils");
|
|
974
|
-
safeRpc = createSafeRpc(client, getSafeTimers);
|
|
975
|
-
} catch (err) {
|
|
976
|
-
if (reloadTries >= 10) {
|
|
977
|
-
const error = serializeError(new Error('Vitest failed to load "vitest/utils" after 10 retries.'));
|
|
978
|
-
error.cause = serializeError(err);
|
|
979
|
-
await client.rpc.onUnhandledError(error, "Reload Error");
|
|
980
|
-
await client.rpc.onDone(testId);
|
|
981
|
-
return;
|
|
982
|
-
}
|
|
983
|
-
const tries = reloadTries + 1;
|
|
984
|
-
const newUrl = new URL(location.href);
|
|
985
|
-
newUrl.searchParams.set("reloadTries", String(tries));
|
|
986
|
-
location.href = newUrl.href;
|
|
987
|
-
return;
|
|
988
|
-
}
|
|
989
|
-
stopErrorHandler();
|
|
990
|
-
stopRejectionHandler();
|
|
991
|
-
catchWindowErrors((event) => reportUnexpectedError(safeRpc, "Error", event.error));
|
|
992
|
-
on("unhandledrejection", (event) => reportUnexpectedError(safeRpc, "Unhandled Rejection", event.reason));
|
|
993
|
-
globalThis.__vitest_browser__ = true;
|
|
994
|
-
globalThis.__vitest_worker__ = {
|
|
995
|
-
config,
|
|
996
|
-
browserHashMap,
|
|
997
|
-
environment: {
|
|
998
|
-
name: "browser"
|
|
999
|
-
},
|
|
1000
|
-
// @ts-expect-error untyped global for internal use
|
|
1001
|
-
moduleCache: globalThis.__vi_module_cache__,
|
|
1002
|
-
rpc: client.rpc,
|
|
1003
|
-
safeRpc,
|
|
1004
|
-
durations: {
|
|
1005
|
-
environment: 0,
|
|
1006
|
-
prepare: 0
|
|
1007
|
-
},
|
|
1008
|
-
providedContext: await client.rpc.getProvidedContext()
|
|
1009
|
-
};
|
|
1010
|
-
globalThis.__vitest_mocker__ = new VitestBrowserClientMocker();
|
|
1011
|
-
const paths = getQueryPaths();
|
|
1012
|
-
const iFrame = document.getElementById("vitest-ui");
|
|
1013
|
-
iFrame.setAttribute("src", "/__vitest__/");
|
|
1014
|
-
await setupConsoleLogSpy(basePath());
|
|
1015
|
-
setupDialogsSpy();
|
|
1016
|
-
await runTests(paths, config);
|
|
1017
|
-
});
|
|
1018
|
-
async function prepareTestEnvironment(config2) {
|
|
1019
|
-
await __vitePreload(() => import(viteClientPath()), true ? __vite__mapDeps([]) : void 0);
|
|
1020
|
-
const {
|
|
1021
|
-
startTests,
|
|
1022
|
-
setupCommonEnv,
|
|
1023
|
-
loadDiffConfig,
|
|
1024
|
-
takeCoverageInsideWorker
|
|
1025
|
-
} = await importId("vitest/browser");
|
|
1026
|
-
const executor = {
|
|
1027
|
-
executeId: (id) => importId(id)
|
|
1028
|
-
};
|
|
1029
|
-
if (!runner) {
|
|
1030
|
-
const { VitestTestRunner } = await importId("vitest/runners");
|
|
1031
|
-
const BrowserRunner = createBrowserRunner(VitestTestRunner, { takeCoverage: () => takeCoverageInsideWorker(config2.coverage, executor) });
|
|
1032
|
-
runner = new BrowserRunner({ config: config2, browserHashMap });
|
|
1033
|
-
}
|
|
1034
|
-
return {
|
|
1035
|
-
startTests,
|
|
1036
|
-
setupCommonEnv,
|
|
1037
|
-
loadDiffConfig,
|
|
1038
|
-
executor,
|
|
1039
|
-
runner
|
|
1040
|
-
};
|
|
1041
|
-
}
|
|
1042
|
-
async function runTests(paths, config2) {
|
|
1043
|
-
let preparedData;
|
|
1044
|
-
try {
|
|
1045
|
-
preparedData = await prepareTestEnvironment(config2);
|
|
1046
|
-
} catch (err) {
|
|
1047
|
-
location.reload();
|
|
1048
|
-
return;
|
|
1049
|
-
}
|
|
1050
|
-
const { startTests, setupCommonEnv, loadDiffConfig, executor, runner: runner2 } = preparedData;
|
|
1051
|
-
onCancel.then((reason) => {
|
|
1052
|
-
var _a;
|
|
1053
|
-
(_a = runner2 == null ? void 0 : runner2.onCancel) == null ? void 0 : _a.call(runner2, reason);
|
|
1054
|
-
});
|
|
1055
|
-
if (!config2.snapshotOptions.snapshotEnvironment)
|
|
1056
|
-
config2.snapshotOptions.snapshotEnvironment = new BrowserSnapshotEnvironment();
|
|
1057
|
-
try {
|
|
1058
|
-
runner2.config.diffOptions = await loadDiffConfig(config2, executor);
|
|
1059
|
-
await setupCommonEnv(config2);
|
|
1060
|
-
const files = paths.map((path) => {
|
|
1061
|
-
return `${config2.root}/${path}`.replace(/\/+/g, "/");
|
|
1062
|
-
});
|
|
1063
|
-
const now = `${(/* @__PURE__ */ new Date()).getTime()}`;
|
|
1064
|
-
files.forEach((i) => browserHashMap.set(i, [true, now]));
|
|
1065
|
-
runningTests = true;
|
|
1066
|
-
for (const file of files)
|
|
1067
|
-
await startTests([file], runner2);
|
|
1068
|
-
} finally {
|
|
1069
|
-
runningTests = false;
|
|
1070
|
-
await rpcDone();
|
|
1071
|
-
await rpc().onDone(testId);
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
709
|
function __vite__mapDeps(indexes) {
|
|
1075
710
|
if (!__vite__mapDeps.viteFileDeps) {
|
|
1076
711
|
__vite__mapDeps.viteFileDeps = []
|
|
1077
712
|
}
|
|
1078
713
|
return indexes.map((i) => __vite__mapDeps.viteFileDeps[i])
|
|
1079
|
-
}
|
|
714
|
+
}
|