@vitest/browser 2.0.0-beta.11 → 2.0.0-beta.12

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,7 +1,12 @@
1
- import { c as channel, g as getBrowserState, a as client, b as getConfig, r as relative, d as rpcDone } from "./rpc-D6HtJ5Rb.js";
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) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
7
+ import { c as channel, g as getBrowserState, a as client, b as globalChannel, d as getConfig, r as relative } from "./client-Dz5Ebwug.js";
2
8
  import { http } from "/__virtual_vitest__?id=msw%2Fcore%2Fhttp";
3
9
  import { setupWorker } from "/__virtual_vitest__?id=msw%2Fbrowser";
4
- import "/__virtual_vitest__?id=vitest%2Futils";
5
10
  function generateHash(str) {
6
11
  let hash = 0;
7
12
  if (str.length === 0) {
@@ -135,46 +140,191 @@ function injectQuery(url2, queryToInject) {
135
140
  }
136
141
  const url = new URL(location.href);
137
142
  const ID_ALL = "__vitest_all__";
138
- const iframes = /* @__PURE__ */ new Map();
143
+ class IframeOrchestrator {
144
+ constructor() {
145
+ __publicField(this, "cancelled", false);
146
+ __publicField(this, "runningFiles", /* @__PURE__ */ new Set());
147
+ __publicField(this, "mocker", createModuleMocker());
148
+ __publicField(this, "iframes", /* @__PURE__ */ new Map());
149
+ }
150
+ async init() {
151
+ const testFiles = getBrowserState().files;
152
+ debug("test files", testFiles.join(", "));
153
+ this.runningFiles.clear();
154
+ testFiles.forEach((file) => this.runningFiles.add(file));
155
+ channel.addEventListener(
156
+ "message",
157
+ (e) => this.onIframeEvent(e)
158
+ );
159
+ globalChannel.addEventListener(
160
+ "message",
161
+ (e) => this.onGlobalChannelEvent(e)
162
+ );
163
+ }
164
+ async createTesters(testFiles) {
165
+ this.cancelled = false;
166
+ this.runningFiles.clear();
167
+ testFiles.forEach((file) => this.runningFiles.add(file));
168
+ const config = getConfig();
169
+ const container = await getContainer(config);
170
+ if (config.browser.ui) {
171
+ container.className = "scrolls";
172
+ container.textContent = "";
173
+ }
174
+ const { width, height } = config.browser.viewport;
175
+ this.iframes.forEach((iframe) => iframe.remove());
176
+ this.iframes.clear();
177
+ if (config.isolate === false) {
178
+ const iframe = this.createIframe(container, ID_ALL);
179
+ await setIframeViewport(iframe, width, height);
180
+ return;
181
+ }
182
+ for (const file of testFiles) {
183
+ if (this.cancelled) {
184
+ done();
185
+ return;
186
+ }
187
+ const iframe = this.createIframe(container, file);
188
+ await setIframeViewport(iframe, width, height);
189
+ await new Promise((resolve) => {
190
+ channel.addEventListener(
191
+ "message",
192
+ function handler(e) {
193
+ if (e.data.type === "done" || e.data.type === "error") {
194
+ channel.removeEventListener("message", handler);
195
+ resolve();
196
+ }
197
+ }
198
+ );
199
+ });
200
+ }
201
+ }
202
+ createIframe(container, file) {
203
+ if (this.iframes.has(file)) {
204
+ this.iframes.get(file).remove();
205
+ this.iframes.delete(file);
206
+ }
207
+ const iframe = document.createElement("iframe");
208
+ iframe.setAttribute("loading", "eager");
209
+ iframe.setAttribute(
210
+ "src",
211
+ `${url.pathname}__vitest_test__/__test__/${getBrowserState().contextId}/${encodeURIComponent(file)}`
212
+ );
213
+ iframe.setAttribute("data-vitest", "true");
214
+ iframe.style.display = "block";
215
+ iframe.style.border = "none";
216
+ iframe.style.zIndex = "1";
217
+ iframe.style.position = "relative";
218
+ iframe.setAttribute("allowfullscreen", "true");
219
+ iframe.setAttribute("allow", "clipboard-write;");
220
+ iframe.setAttribute("name", "vitest-iframe");
221
+ this.iframes.set(file, iframe);
222
+ container.appendChild(iframe);
223
+ return iframe;
224
+ }
225
+ async onGlobalChannelEvent(e) {
226
+ debug("global channel event", JSON.stringify(e.data));
227
+ switch (e.data.type) {
228
+ case "cancel": {
229
+ this.cancelled = true;
230
+ break;
231
+ }
232
+ }
233
+ }
234
+ async onIframeEvent(e) {
235
+ var _a;
236
+ debug("iframe event", JSON.stringify(e.data));
237
+ switch (e.data.type) {
238
+ case "viewport": {
239
+ const { width, height, id } = e.data;
240
+ const iframe = this.iframes.get(id);
241
+ if (!iframe) {
242
+ const error = new Error(`Cannot find iframe with id ${id}`);
243
+ channel.postMessage({
244
+ type: "viewport:fail",
245
+ id,
246
+ error: error.message
247
+ });
248
+ await client.rpc.onUnhandledError(
249
+ {
250
+ name: "Teardown Error",
251
+ message: error.message
252
+ },
253
+ "Teardown Error"
254
+ );
255
+ return;
256
+ }
257
+ await setIframeViewport(iframe, width, height);
258
+ channel.postMessage({ type: "viewport:done", id });
259
+ break;
260
+ }
261
+ case "done": {
262
+ const filenames = e.data.filenames;
263
+ filenames.forEach((filename) => this.runningFiles.delete(filename));
264
+ if (!this.runningFiles.size) {
265
+ const ui = getUiAPI();
266
+ if (ui && filenames.length > 1) {
267
+ const id = generateFileId(filenames[filenames.length - 1]);
268
+ ui.setCurrentFileId(id);
269
+ }
270
+ await done();
271
+ } else {
272
+ const iframeId = e.data.id;
273
+ (_a = this.iframes.get(iframeId)) == null ? void 0 : _a.remove();
274
+ this.iframes.delete(iframeId);
275
+ }
276
+ break;
277
+ }
278
+ case "error": {
279
+ const iframeId = e.data.id;
280
+ this.iframes.delete(iframeId);
281
+ await client.rpc.onUnhandledError(e.data.error, e.data.errorType);
282
+ if (iframeId === ID_ALL) {
283
+ this.runningFiles.clear();
284
+ } else {
285
+ this.runningFiles.delete(iframeId);
286
+ }
287
+ if (!this.runningFiles.size) {
288
+ await done();
289
+ }
290
+ break;
291
+ }
292
+ case "mock:invalidate":
293
+ this.mocker.invalidate();
294
+ break;
295
+ case "unmock":
296
+ await this.mocker.unmock(e.data);
297
+ break;
298
+ case "mock":
299
+ await this.mocker.mock(e.data);
300
+ break;
301
+ case "mock-factory:error":
302
+ case "mock-factory:response":
303
+ break;
304
+ default: {
305
+ e.data;
306
+ await client.rpc.onUnhandledError(
307
+ {
308
+ name: "Unexpected Event",
309
+ message: `Unexpected event: ${e.data.type}`
310
+ },
311
+ "Unexpected Event"
312
+ );
313
+ await done();
314
+ }
315
+ }
316
+ }
317
+ }
318
+ const orchestrator = new IframeOrchestrator();
139
319
  let promiseTesters;
140
320
  getBrowserState().createTesters = async (files) => {
141
321
  await promiseTesters;
142
- promiseTesters = createTesters(files).finally(() => {
322
+ promiseTesters = orchestrator.createTesters(files).finally(() => {
143
323
  promiseTesters = void 0;
144
324
  });
145
325
  await promiseTesters;
146
326
  };
147
- function debug(...args) {
148
- const debug2 = getConfig().env.VITEST_BROWSER_DEBUG;
149
- if (debug2 && debug2 !== "false") {
150
- client.rpc.debug(...args.map(String));
151
- }
152
- }
153
- function createIframe(container, file) {
154
- if (iframes.has(file)) {
155
- iframes.get(file).remove();
156
- iframes.delete(file);
157
- }
158
- const iframe = document.createElement("iframe");
159
- iframe.setAttribute("loading", "eager");
160
- iframe.setAttribute(
161
- "src",
162
- `${url.pathname}__vitest_test__/__test__/${getBrowserState().contextId}/${encodeURIComponent(file)}`
163
- );
164
- iframe.setAttribute("data-vitest", "true");
165
- iframe.style.display = "block";
166
- iframe.style.border = "none";
167
- iframe.style.zIndex = "1";
168
- iframe.style.position = "relative";
169
- iframe.setAttribute("allowfullscreen", "true");
170
- iframe.setAttribute("allow", "clipboard-write;");
171
- iframe.setAttribute("name", "vitest-iframe");
172
- iframes.set(file, iframe);
173
- container.appendChild(iframe);
174
- return iframe;
175
- }
176
327
  async function done() {
177
- await rpcDone();
178
328
  await client.rpc.finishBrowserTests(getBrowserState().contextId);
179
329
  }
180
330
  async function getContainer(config) {
@@ -191,136 +341,13 @@ async function getContainer(config) {
191
341
  }
192
342
  return document.querySelector("#vitest-tester");
193
343
  }
194
- const runningFiles = /* @__PURE__ */ new Set();
195
344
  client.ws.addEventListener("open", async () => {
196
345
  const testFiles = getBrowserState().files;
197
- debug("test files", testFiles.join(", "));
198
- runningFiles.clear();
199
- testFiles.forEach((file) => runningFiles.add(file));
200
- const mocker = createModuleMocker();
201
- channel.addEventListener(
202
- "message",
203
- async (e) => {
204
- var _a;
205
- debug("channel event", JSON.stringify(e.data));
206
- switch (e.data.type) {
207
- case "viewport": {
208
- const { width, height, id } = e.data;
209
- const iframe = iframes.get(id);
210
- if (!iframe) {
211
- const error = new Error(`Cannot find iframe with id ${id}`);
212
- channel.postMessage({
213
- type: "viewport:fail",
214
- id,
215
- error: error.message
216
- });
217
- await client.rpc.onUnhandledError(
218
- {
219
- name: "Teardown Error",
220
- message: error.message
221
- },
222
- "Teardown Error"
223
- );
224
- return;
225
- }
226
- await setIframeViewport(iframe, width, height);
227
- channel.postMessage({ type: "viewport:done", id });
228
- break;
229
- }
230
- case "done": {
231
- const filenames = e.data.filenames;
232
- filenames.forEach((filename) => runningFiles.delete(filename));
233
- if (!runningFiles.size) {
234
- const ui = getUiAPI();
235
- if (ui && filenames.length > 1) {
236
- const id = generateFileId(filenames[filenames.length - 1]);
237
- ui.setCurrentFileId(id);
238
- }
239
- await done();
240
- } else {
241
- const iframeId = e.data.id;
242
- (_a = iframes.get(iframeId)) == null ? void 0 : _a.remove();
243
- iframes.delete(iframeId);
244
- }
245
- break;
246
- }
247
- case "error": {
248
- const iframeId = e.data.id;
249
- iframes.delete(iframeId);
250
- await client.rpc.onUnhandledError(e.data.error, e.data.errorType);
251
- if (iframeId === ID_ALL) {
252
- runningFiles.clear();
253
- } else {
254
- runningFiles.delete(iframeId);
255
- }
256
- if (!runningFiles.size) {
257
- await done();
258
- }
259
- break;
260
- }
261
- case "mock:invalidate":
262
- mocker.invalidate();
263
- break;
264
- case "unmock":
265
- await mocker.unmock(e.data);
266
- break;
267
- case "mock":
268
- await mocker.mock(e.data);
269
- break;
270
- case "mock-factory:error":
271
- case "mock-factory:response":
272
- break;
273
- default: {
274
- e.data;
275
- await client.rpc.onUnhandledError(
276
- {
277
- name: "Unexpected Event",
278
- message: `Unexpected event: ${e.data.type}`
279
- },
280
- "Unexpected Event"
281
- );
282
- await done();
283
- }
284
- }
285
- }
286
- );
346
+ await orchestrator.init();
287
347
  if (testFiles.length) {
288
- await createTesters(testFiles);
348
+ await orchestrator.createTesters(testFiles);
289
349
  }
290
350
  });
291
- async function createTesters(testFiles) {
292
- runningFiles.clear();
293
- testFiles.forEach((file) => runningFiles.add(file));
294
- const config = getConfig();
295
- const container = await getContainer(config);
296
- if (config.browser.ui) {
297
- container.className = "scrolls";
298
- container.textContent = "";
299
- }
300
- const { width, height } = config.browser.viewport;
301
- iframes.forEach((iframe) => iframe.remove());
302
- iframes.clear();
303
- if (config.isolate === false) {
304
- const iframe = createIframe(container, ID_ALL);
305
- await setIframeViewport(iframe, width, height);
306
- } else {
307
- for (const file of testFiles) {
308
- const iframe = createIframe(container, file);
309
- await setIframeViewport(iframe, width, height);
310
- await new Promise((resolve) => {
311
- channel.addEventListener(
312
- "message",
313
- function handler(e) {
314
- if (e.data.type === "done" || e.data.type === "error") {
315
- channel.removeEventListener("message", handler);
316
- resolve();
317
- }
318
- }
319
- );
320
- });
321
- }
322
- }
323
- }
324
351
  function generateFileId(file) {
325
352
  const config = getConfig();
326
353
  const project = config.name || "";
@@ -336,3 +363,9 @@ async function setIframeViewport(iframe, width, height) {
336
363
  iframe.style.height = `${height}px`;
337
364
  }
338
365
  }
366
+ function debug(...args) {
367
+ const debug2 = getConfig().env.VITEST_BROWSER_DEBUG;
368
+ if (debug2 && debug2 !== "false") {
369
+ client.rpc.debug(...args.map(String));
370
+ }
371
+ }