@vitest/browser 3.2.0-beta.2 → 3.2.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.
@@ -1,287 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import { g as getBrowserState, a as getConfig, r as relative } from "./utils-Owv5OOOf.js";
5
- import { channel, globalChannel, client } from "@vitest/browser/client";
6
- function generateHash(str) {
7
- let hash = 0;
8
- if (str.length === 0) {
9
- return `${hash}`;
10
- }
11
- for (let i = 0; i < str.length; i++) {
12
- const char = str.charCodeAt(i);
13
- hash = (hash << 5) - hash + char;
14
- hash = hash & hash;
15
- }
16
- return `${hash}`;
17
- }
18
- function getUiAPI() {
19
- return window.__vitest_ui_api__;
20
- }
21
- const url = new URL(location.href);
22
- const ID_ALL = "__vitest_all__";
23
- class IframeOrchestrator {
24
- constructor() {
25
- __publicField(this, "cancelled", false);
26
- __publicField(this, "recreateNonIsolatedIframe", false);
27
- __publicField(this, "iframes", /* @__PURE__ */ new Map());
28
- debug("init orchestrator", getBrowserState().sessionId);
29
- channel.addEventListener(
30
- "message",
31
- (e) => this.onIframeEvent(e)
32
- );
33
- globalChannel.addEventListener(
34
- "message",
35
- (e) => this.onGlobalChannelEvent(e)
36
- );
37
- }
38
- async createTesters(options) {
39
- this.cancelled = false;
40
- const config = getConfig();
41
- debug("create testers", options.files.join(", "));
42
- const container = await getContainer(config);
43
- if (config.browser.ui) {
44
- container.className = "absolute origin-top mt-[8px]";
45
- container.parentElement.setAttribute("data-ready", "true");
46
- if (container.textContent) {
47
- container.textContent = "";
48
- }
49
- }
50
- if (config.browser.isolate === false) {
51
- await this.runNonIsolatedTests(container, options);
52
- return;
53
- }
54
- this.iframes.forEach((iframe) => iframe.remove());
55
- this.iframes.clear();
56
- for (let i = 0; i < options.files.length; i++) {
57
- if (this.cancelled) {
58
- return;
59
- }
60
- const file = options.files[i];
61
- debug("create iframe", file);
62
- await this.runIsolatedTestInIframe(
63
- container,
64
- file,
65
- options
66
- );
67
- }
68
- }
69
- async cleanupTesters() {
70
- const config = getConfig();
71
- if (config.browser.isolate) {
72
- const files = Array.from(this.iframes.keys());
73
- const ui = getUiAPI();
74
- if (ui && files[0]) {
75
- const id = generateFileId(files[0]);
76
- ui.setCurrentFileId(id);
77
- }
78
- return;
79
- }
80
- const iframe = this.iframes.get(ID_ALL);
81
- if (!iframe) {
82
- return;
83
- }
84
- await sendEventToIframe({
85
- event: "cleanup",
86
- iframeId: ID_ALL
87
- });
88
- this.recreateNonIsolatedIframe = true;
89
- }
90
- async runNonIsolatedTests(container, options) {
91
- if (this.recreateNonIsolatedIframe) {
92
- this.recreateNonIsolatedIframe = false;
93
- this.iframes.get(ID_ALL).remove();
94
- this.iframes.delete(ID_ALL);
95
- debug("recreate non-isolated iframe");
96
- }
97
- if (!this.iframes.has(ID_ALL)) {
98
- debug("preparing non-isolated iframe");
99
- await this.prepareIframe(container, ID_ALL);
100
- }
101
- const config = getConfig();
102
- const { width, height } = config.browser.viewport;
103
- const iframe = this.iframes.get(ID_ALL);
104
- await setIframeViewport(iframe, width, height);
105
- debug("run non-isolated tests", options.files.join(", "));
106
- await sendEventToIframe({
107
- event: "execute",
108
- iframeId: ID_ALL,
109
- files: options.files,
110
- method: options.method,
111
- context: options.providedContext
112
- });
113
- }
114
- async runIsolatedTestInIframe(container, file, options) {
115
- const config = getConfig();
116
- const { width, height } = config.browser.viewport;
117
- if (this.iframes.has(file)) {
118
- this.iframes.get(file).remove();
119
- this.iframes.delete(file);
120
- }
121
- const iframe = await this.prepareIframe(container, file);
122
- await setIframeViewport(iframe, width, height);
123
- await sendEventToIframe({
124
- event: "execute",
125
- files: [file],
126
- method: options.method,
127
- iframeId: file,
128
- context: options.providedContext
129
- });
130
- await sendEventToIframe({
131
- event: "cleanup",
132
- iframeId: file
133
- });
134
- }
135
- async prepareIframe(container, iframeId) {
136
- const iframe = this.createTestIframe(iframeId);
137
- container.appendChild(iframe);
138
- await new Promise((resolve, reject) => {
139
- iframe.onload = () => {
140
- this.iframes.set(iframeId, iframe);
141
- sendEventToIframe({
142
- event: "prepare",
143
- iframeId
144
- }).then(resolve, reject);
145
- };
146
- iframe.onerror = (e) => {
147
- if (typeof e === "string") {
148
- reject(new Error(e));
149
- } else if (e instanceof ErrorEvent) {
150
- reject(e.error);
151
- } else {
152
- reject(new Error(`Cannot load the iframe ${iframeId}.`));
153
- }
154
- };
155
- });
156
- return iframe;
157
- }
158
- createTestIframe(iframeId) {
159
- const iframe = document.createElement("iframe");
160
- const src = `${url.pathname}__vitest_test__/__test__/?sessionId=${getBrowserState().sessionId}&iframeId=${iframeId}`;
161
- iframe.setAttribute("loading", "eager");
162
- iframe.setAttribute("src", src);
163
- iframe.setAttribute("data-vitest", "true");
164
- iframe.style.border = "none";
165
- iframe.style.width = "100%";
166
- iframe.style.height = "100%";
167
- iframe.setAttribute("allowfullscreen", "true");
168
- iframe.setAttribute("allow", "clipboard-write;");
169
- iframe.setAttribute("name", "vitest-iframe");
170
- return iframe;
171
- }
172
- async onGlobalChannelEvent(e) {
173
- debug("global channel event", JSON.stringify(e.data));
174
- switch (e.data.type) {
175
- case "cancel": {
176
- this.cancelled = true;
177
- break;
178
- }
179
- }
180
- }
181
- async onIframeEvent(e) {
182
- debug("iframe event", JSON.stringify(e.data));
183
- switch (e.data.event) {
184
- case "viewport": {
185
- const { width, height, iframeId: id } = e.data;
186
- const iframe = this.iframes.get(id);
187
- if (!iframe) {
188
- const error = `Cannot find iframe with id ${id}`;
189
- channel.postMessage({
190
- event: "viewport:fail",
191
- iframeId: id,
192
- error
193
- });
194
- await client.rpc.onUnhandledError(
195
- {
196
- name: "Teardown Error",
197
- message: error
198
- },
199
- "Teardown Error"
200
- );
201
- break;
202
- }
203
- await setIframeViewport(iframe, width, height);
204
- channel.postMessage({ event: "viewport:done", iframeId: id });
205
- break;
206
- }
207
- default: {
208
- if (typeof e.data.event === "string" && e.data.event.startsWith("response:")) {
209
- break;
210
- }
211
- await client.rpc.onUnhandledError(
212
- {
213
- name: "Unexpected Event",
214
- message: `Unexpected event: ${e.data.event}`
215
- },
216
- "Unexpected Event"
217
- );
218
- }
219
- }
220
- }
221
- }
222
- getBrowserState().orchestrator = new IframeOrchestrator();
223
- async function getContainer(config) {
224
- if (config.browser.ui) {
225
- const element = document.querySelector("#tester-ui");
226
- if (!element) {
227
- return new Promise((resolve) => {
228
- queueMicrotask(() => {
229
- resolve(getContainer(config));
230
- });
231
- });
232
- }
233
- return element;
234
- }
235
- return document.querySelector("#vitest-tester");
236
- }
237
- async function sendEventToIframe(event) {
238
- channel.postMessage(event);
239
- return new Promise((resolve) => {
240
- channel.addEventListener(
241
- "message",
242
- function handler(e) {
243
- if (e.data.iframeId === event.iframeId && e.data.event === `response:${event.event}`) {
244
- resolve();
245
- channel.removeEventListener("message", handler);
246
- }
247
- }
248
- );
249
- });
250
- }
251
- function generateFileId(file) {
252
- const config = getConfig();
253
- const project = config.name || "";
254
- const path = relative(config.root, file);
255
- return generateHash(`${path}${project}`);
256
- }
257
- async function setIframeViewport(iframe, width, height) {
258
- var _a, _b;
259
- const ui = getUiAPI();
260
- if (ui) {
261
- await ui.setIframeViewport(width, height);
262
- } else if (getBrowserState().provider === "webdriverio") {
263
- iframe.style.width = `${width}px`;
264
- iframe.style.height = `${height}px`;
265
- (_a = iframe.parentElement) == null ? void 0 : _a.setAttribute("data-scale", "1");
266
- } else {
267
- const scale = Math.min(
268
- 1,
269
- iframe.parentElement.parentElement.clientWidth / width,
270
- iframe.parentElement.parentElement.clientHeight / height
271
- );
272
- iframe.parentElement.style.cssText = `
273
- width: ${width}px;
274
- height: ${height}px;
275
- transform: scale(${scale});
276
- transform-origin: left top;
277
- `;
278
- (_b = iframe.parentElement) == null ? void 0 : _b.setAttribute("data-scale", String(scale));
279
- await new Promise((r) => requestAnimationFrame(r));
280
- }
281
- }
282
- function debug(...args) {
283
- const debug2 = getConfig().env.VITEST_BROWSER_DEBUG;
284
- if (debug2 && debug2 !== "false") {
285
- client.rpc.debug(...args.map(String));
286
- }
287
- }