@vitest/browser 2.0.0-beta.3 → 2.0.0-beta.5
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/context.d.ts +8 -0
- package/dist/client/.vite/manifest.json +24 -0
- package/dist/client/__vitest__/assets/index-B74JATHR.css +1 -0
- package/dist/client/__vitest__/assets/index-BoV0brgU.js +51 -0
- package/dist/client/__vitest__/index.html +2 -2
- package/dist/client/__vitest_browser__/orchestrator-CEB54dI4.js +192 -0
- package/dist/client/__vitest_browser__/{rpc-Hw6RY18C.js → rpc-DBukiZYG.js} +217 -416
- package/dist/client/__vitest_browser__/{tester-Ly7RLJnN.js → tester-C3Mchfpf.js} +246 -33
- package/dist/client/esm-client-injector.js +12 -28
- package/dist/client/{index.html → orchestrator.html} +8 -7
- package/dist/client/tester.html +4 -3
- package/dist/index.js +243 -258
- package/package.json +9 -7
- package/dist/client/__vitest__/assets/index-C576npev.css +0 -1
- package/dist/client/__vitest__/assets/index-DBVixaI-.js +0 -51
- package/dist/client/__vitest_browser__/main-uRgy0zc4.js +0 -102
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
})()
|
|
18
18
|
</script>
|
|
19
19
|
<!-- !LOAD_METADATA! -->
|
|
20
|
-
<script type="module" crossorigin src="./assets/index-
|
|
21
|
-
<link rel="stylesheet" crossorigin href="./assets/index-
|
|
20
|
+
<script type="module" crossorigin src="./assets/index-BoV0brgU.js"></script>
|
|
21
|
+
<link rel="stylesheet" crossorigin href="./assets/index-B74JATHR.css">
|
|
22
22
|
</head>
|
|
23
23
|
<body>
|
|
24
24
|
<div id="app"></div>
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { g as getBrowserState, c as client, a as channel, b as getConfig, r as relative, d as rpcDone } from "./rpc-DBukiZYG.js";
|
|
2
|
+
function generateHash(str) {
|
|
3
|
+
let hash = 0;
|
|
4
|
+
if (str.length === 0)
|
|
5
|
+
return `${hash}`;
|
|
6
|
+
for (let i = 0; i < str.length; i++) {
|
|
7
|
+
const char = str.charCodeAt(i);
|
|
8
|
+
hash = (hash << 5) - hash + char;
|
|
9
|
+
hash = hash & hash;
|
|
10
|
+
}
|
|
11
|
+
return `${hash}`;
|
|
12
|
+
}
|
|
13
|
+
function getUiAPI() {
|
|
14
|
+
return window.__vitest_ui_api__;
|
|
15
|
+
}
|
|
16
|
+
const url = new URL(location.href);
|
|
17
|
+
const ID_ALL = "__vitest_all__";
|
|
18
|
+
const iframes = /* @__PURE__ */ new Map();
|
|
19
|
+
let promiseTesters;
|
|
20
|
+
getBrowserState().createTesters = async (files) => {
|
|
21
|
+
await promiseTesters;
|
|
22
|
+
promiseTesters = createTesters(files).finally(() => {
|
|
23
|
+
promiseTesters = void 0;
|
|
24
|
+
});
|
|
25
|
+
await promiseTesters;
|
|
26
|
+
};
|
|
27
|
+
function debug(...args) {
|
|
28
|
+
const debug2 = getConfig().env.VITEST_BROWSER_DEBUG;
|
|
29
|
+
if (debug2 && debug2 !== "false")
|
|
30
|
+
client.rpc.debug(...args.map(String));
|
|
31
|
+
}
|
|
32
|
+
function createIframe(container, file) {
|
|
33
|
+
if (iframes.has(file)) {
|
|
34
|
+
iframes.get(file).remove();
|
|
35
|
+
iframes.delete(file);
|
|
36
|
+
}
|
|
37
|
+
const iframe = document.createElement("iframe");
|
|
38
|
+
iframe.setAttribute("loading", "eager");
|
|
39
|
+
iframe.setAttribute("src", `${url.pathname}__vitest_test__/__test__/${encodeURIComponent(file)}`);
|
|
40
|
+
iframe.setAttribute("data-vitest", "true");
|
|
41
|
+
const config = getConfig().browser;
|
|
42
|
+
iframe.style.width = `${config.viewport.width}px`;
|
|
43
|
+
iframe.style.height = `${config.viewport.height}px`;
|
|
44
|
+
iframe.style.display = "block";
|
|
45
|
+
iframe.style.border = "none";
|
|
46
|
+
iframe.setAttribute("allowfullscreen", "true");
|
|
47
|
+
iframe.setAttribute("allow", "clipboard-write;");
|
|
48
|
+
iframes.set(file, iframe);
|
|
49
|
+
container.appendChild(iframe);
|
|
50
|
+
return iframe;
|
|
51
|
+
}
|
|
52
|
+
async function done() {
|
|
53
|
+
await rpcDone();
|
|
54
|
+
await client.rpc.finishBrowserTests();
|
|
55
|
+
}
|
|
56
|
+
async function getContainer(config) {
|
|
57
|
+
if (config.browser.ui) {
|
|
58
|
+
const element = document.querySelector("#tester-ui");
|
|
59
|
+
if (!element) {
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
setTimeout(() => {
|
|
62
|
+
resolve(getContainer(config));
|
|
63
|
+
}, 30);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return element;
|
|
67
|
+
}
|
|
68
|
+
return document.querySelector("#vitest-tester");
|
|
69
|
+
}
|
|
70
|
+
client.ws.addEventListener("open", async () => {
|
|
71
|
+
const testFiles = getBrowserState().files;
|
|
72
|
+
debug("test files", testFiles.join(", "));
|
|
73
|
+
if (!testFiles.length) {
|
|
74
|
+
await done();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const runningFiles = new Set(testFiles);
|
|
78
|
+
channel.addEventListener("message", async (e) => {
|
|
79
|
+
var _a;
|
|
80
|
+
debug("channel event", JSON.stringify(e.data));
|
|
81
|
+
switch (e.data.type) {
|
|
82
|
+
case "viewport": {
|
|
83
|
+
const { width, height, id } = e.data;
|
|
84
|
+
const widthStr = typeof width === "number" ? `${width}px` : width;
|
|
85
|
+
const heightStr = typeof height === "number" ? `${height}px` : height;
|
|
86
|
+
const iframe = iframes.get(id);
|
|
87
|
+
if (!iframe) {
|
|
88
|
+
const error = new Error(`Cannot find iframe with id ${id}`);
|
|
89
|
+
channel.postMessage({ type: "viewport:fail", id, error: error.message });
|
|
90
|
+
await client.rpc.onUnhandledError({
|
|
91
|
+
name: "Teardown Error",
|
|
92
|
+
message: error.message
|
|
93
|
+
}, "Teardown Error");
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
iframe.style.width = widthStr;
|
|
97
|
+
iframe.style.height = heightStr;
|
|
98
|
+
const ui = getUiAPI();
|
|
99
|
+
if (ui) {
|
|
100
|
+
await new Promise((r) => requestAnimationFrame(r));
|
|
101
|
+
ui.recalculateDetailPanels();
|
|
102
|
+
}
|
|
103
|
+
channel.postMessage({ type: "viewport:done", id });
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "done": {
|
|
107
|
+
const filenames = e.data.filenames;
|
|
108
|
+
filenames.forEach((filename) => runningFiles.delete(filename));
|
|
109
|
+
if (!runningFiles.size) {
|
|
110
|
+
const ui = getUiAPI();
|
|
111
|
+
if (ui && filenames.length > 1) {
|
|
112
|
+
const id = generateFileId(filenames[filenames.length - 1]);
|
|
113
|
+
ui.setCurrentById(id);
|
|
114
|
+
}
|
|
115
|
+
await done();
|
|
116
|
+
} else {
|
|
117
|
+
const iframeId = filenames.length > 1 ? ID_ALL : filenames[0];
|
|
118
|
+
(_a = iframes.get(iframeId)) == null ? void 0 : _a.remove();
|
|
119
|
+
iframes.delete(iframeId);
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case "error": {
|
|
124
|
+
const iframeId = e.data.files.length > 1 ? ID_ALL : e.data.files[0];
|
|
125
|
+
iframes.delete(iframeId);
|
|
126
|
+
await client.rpc.onUnhandledError(e.data.error, e.data.errorType);
|
|
127
|
+
if (iframeId === ID_ALL)
|
|
128
|
+
runningFiles.clear();
|
|
129
|
+
else
|
|
130
|
+
runningFiles.delete(iframeId);
|
|
131
|
+
if (!runningFiles.size)
|
|
132
|
+
await done();
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
default: {
|
|
136
|
+
await client.rpc.onUnhandledError({
|
|
137
|
+
name: "Unexpected Event",
|
|
138
|
+
message: `Unexpected event: ${e.data.type}`
|
|
139
|
+
}, "Unexpected Event");
|
|
140
|
+
await done();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
await createTesters(testFiles);
|
|
145
|
+
});
|
|
146
|
+
async function createTesters(testFiles) {
|
|
147
|
+
const config = getConfig();
|
|
148
|
+
const container = await getContainer(config);
|
|
149
|
+
if (config.browser.ui) {
|
|
150
|
+
container.className = "scrolls";
|
|
151
|
+
container.textContent = "";
|
|
152
|
+
}
|
|
153
|
+
if (config.isolate === false) {
|
|
154
|
+
createIframe(
|
|
155
|
+
container,
|
|
156
|
+
ID_ALL
|
|
157
|
+
);
|
|
158
|
+
const ui = getUiAPI();
|
|
159
|
+
if (ui) {
|
|
160
|
+
await new Promise((r) => requestAnimationFrame(r));
|
|
161
|
+
ui.recalculateDetailPanels();
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
for (const file of testFiles) {
|
|
165
|
+
const ui = getUiAPI();
|
|
166
|
+
createIframe(
|
|
167
|
+
container,
|
|
168
|
+
file
|
|
169
|
+
);
|
|
170
|
+
if (ui) {
|
|
171
|
+
const id = generateFileId(file);
|
|
172
|
+
ui.setCurrentById(id);
|
|
173
|
+
await new Promise((r) => requestAnimationFrame(r));
|
|
174
|
+
ui.recalculateDetailPanels();
|
|
175
|
+
}
|
|
176
|
+
await new Promise((resolve) => {
|
|
177
|
+
channel.addEventListener("message", function handler(e) {
|
|
178
|
+
if (e.data.type === "done" || e.data.type === "error") {
|
|
179
|
+
channel.removeEventListener("message", handler);
|
|
180
|
+
resolve();
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function generateFileId(file) {
|
|
188
|
+
const config = getConfig();
|
|
189
|
+
const project = config.name || "";
|
|
190
|
+
const path = relative(config.root, file);
|
|
191
|
+
return generateHash(`${path}${project}`);
|
|
192
|
+
}
|