@vitest/browser 3.2.4 → 4.0.0-beta.10

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