@vitest/browser 2.1.3 → 2.1.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/README.md +7 -21
- package/context.d.ts +6 -0
- package/dist/client/.vite/manifest.json +7 -7
- package/dist/client/__vitest__/assets/index-CGSAdLIf.css +1 -0
- package/dist/client/__vitest__/assets/index-DWjHzG4O.js +52 -0
- package/dist/client/__vitest__/index.html +2 -2
- package/dist/client/__vitest_browser__/orchestrator-DnP17K36.js +238 -0
- package/dist/client/__vitest_browser__/{tester-DZCtFstH.js → tester-BaiNqOPw.js} +7749 -6109
- package/dist/client/__vitest_browser__/utils-Owv5OOOf.js +195 -0
- package/dist/client/error-catcher.js +3 -1
- package/dist/client/esm-client-injector.js +50 -48
- package/dist/client/orchestrator.html +2 -2
- package/dist/client/tester/tester.html +3 -19
- package/dist/client.js +28 -5
- package/dist/context.js +96 -47
- package/dist/{index-Cgg35wOd.js → index-CKtADM3n.js} +35 -3
- package/dist/index.d.ts +10 -6
- package/dist/index.js +2016 -1825
- package/dist/locators/index.js +1 -1
- package/dist/locators/playwright.js +1 -1
- package/dist/locators/preview.js +11 -11
- package/dist/locators/webdriverio.js +1 -1
- package/dist/providers.js +8 -7
- package/dist/state.js +1 -1
- package/dist/utils.js +1 -1
- package/package.json +18 -18
- package/dist/client/__vitest__/assets/index-CADIw8eX.js +0 -52
- package/dist/client/__vitest__/assets/index-D5rK8X7L.css +0 -1
- package/dist/client/__vitest_browser__/orchestrator-BCPid0xo.js +0 -1099
- package/dist/client/__vitest_browser__/preload-helper-D-WYp1PK.js +0 -317
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
})();
|
|
24
24
|
</script>
|
|
25
25
|
<!-- !LOAD_METADATA! -->
|
|
26
|
-
<script type="module"
|
|
27
|
-
<link rel="stylesheet"
|
|
26
|
+
<script type="module" src="./assets/index-DWjHzG4O.js"></script>
|
|
27
|
+
<link rel="stylesheet" href="./assets/index-CGSAdLIf.css">
|
|
28
28
|
</head>
|
|
29
29
|
<body>
|
|
30
30
|
<div id="app"></div>
|
|
@@ -0,0 +1,238 @@
|
|
|
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 { client, channel, globalChannel } 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, "runningFiles", /* @__PURE__ */ new Set());
|
|
27
|
+
__publicField(this, "iframes", /* @__PURE__ */ new Map());
|
|
28
|
+
}
|
|
29
|
+
async init() {
|
|
30
|
+
const testFiles = getBrowserState().files;
|
|
31
|
+
debug("test files", testFiles.join(", "));
|
|
32
|
+
this.runningFiles.clear();
|
|
33
|
+
testFiles.forEach((file) => this.runningFiles.add(file));
|
|
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(testFiles) {
|
|
44
|
+
this.cancelled = false;
|
|
45
|
+
this.runningFiles.clear();
|
|
46
|
+
testFiles.forEach((file) => this.runningFiles.add(file));
|
|
47
|
+
const config = getConfig();
|
|
48
|
+
const container = await getContainer(config);
|
|
49
|
+
if (config.browser.ui) {
|
|
50
|
+
container.className = "absolute origin-top mt-[8px]";
|
|
51
|
+
container.parentElement.setAttribute("data-ready", "true");
|
|
52
|
+
container.textContent = "";
|
|
53
|
+
}
|
|
54
|
+
const { width, height } = config.browser.viewport;
|
|
55
|
+
this.iframes.forEach((iframe) => iframe.remove());
|
|
56
|
+
this.iframes.clear();
|
|
57
|
+
if (config.isolate === false) {
|
|
58
|
+
const iframe = this.createIframe(container, ID_ALL);
|
|
59
|
+
await setIframeViewport(iframe, width, height);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
for (const file of testFiles) {
|
|
63
|
+
if (this.cancelled) {
|
|
64
|
+
done();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const iframe = this.createIframe(container, file);
|
|
68
|
+
await setIframeViewport(iframe, width, height);
|
|
69
|
+
await new Promise((resolve) => {
|
|
70
|
+
channel.addEventListener(
|
|
71
|
+
"message",
|
|
72
|
+
function handler(e) {
|
|
73
|
+
if (e.data.type === "done" || e.data.type === "error") {
|
|
74
|
+
channel.removeEventListener("message", handler);
|
|
75
|
+
resolve();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
createIframe(container, file) {
|
|
83
|
+
if (this.iframes.has(file)) {
|
|
84
|
+
this.iframes.get(file).remove();
|
|
85
|
+
this.iframes.delete(file);
|
|
86
|
+
}
|
|
87
|
+
const iframe = document.createElement("iframe");
|
|
88
|
+
iframe.setAttribute("loading", "eager");
|
|
89
|
+
iframe.setAttribute(
|
|
90
|
+
"src",
|
|
91
|
+
`${url.pathname}__vitest_test__/__test__/${getBrowserState().contextId}/${encodeURIComponent(file)}`
|
|
92
|
+
);
|
|
93
|
+
iframe.setAttribute("data-vitest", "true");
|
|
94
|
+
iframe.style.border = "none";
|
|
95
|
+
iframe.style.width = "100%";
|
|
96
|
+
iframe.style.height = "100%";
|
|
97
|
+
iframe.setAttribute("allowfullscreen", "true");
|
|
98
|
+
iframe.setAttribute("allow", "clipboard-write;");
|
|
99
|
+
iframe.setAttribute("name", "vitest-iframe");
|
|
100
|
+
this.iframes.set(file, iframe);
|
|
101
|
+
container.appendChild(iframe);
|
|
102
|
+
return iframe;
|
|
103
|
+
}
|
|
104
|
+
async onGlobalChannelEvent(e) {
|
|
105
|
+
debug("global channel event", JSON.stringify(e.data));
|
|
106
|
+
switch (e.data.type) {
|
|
107
|
+
case "cancel": {
|
|
108
|
+
this.cancelled = true;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async onIframeEvent(e) {
|
|
114
|
+
var _a;
|
|
115
|
+
debug("iframe event", JSON.stringify(e.data));
|
|
116
|
+
switch (e.data.type) {
|
|
117
|
+
case "viewport": {
|
|
118
|
+
const { width, height, id } = e.data;
|
|
119
|
+
const iframe = this.iframes.get(id);
|
|
120
|
+
if (!iframe) {
|
|
121
|
+
const error = new Error(`Cannot find iframe with id ${id}`);
|
|
122
|
+
channel.postMessage({
|
|
123
|
+
type: "viewport:fail",
|
|
124
|
+
id,
|
|
125
|
+
error: error.message
|
|
126
|
+
});
|
|
127
|
+
await client.rpc.onUnhandledError(
|
|
128
|
+
{
|
|
129
|
+
name: "Teardown Error",
|
|
130
|
+
message: error.message
|
|
131
|
+
},
|
|
132
|
+
"Teardown Error"
|
|
133
|
+
);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
await setIframeViewport(iframe, width, height);
|
|
137
|
+
channel.postMessage({ type: "viewport:done", id });
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case "done": {
|
|
141
|
+
const filenames = e.data.filenames;
|
|
142
|
+
filenames.forEach((filename) => this.runningFiles.delete(filename));
|
|
143
|
+
if (!this.runningFiles.size) {
|
|
144
|
+
const ui = getUiAPI();
|
|
145
|
+
if (ui && filenames.length > 1) {
|
|
146
|
+
const id = generateFileId(filenames[filenames.length - 1]);
|
|
147
|
+
ui.setCurrentFileId(id);
|
|
148
|
+
}
|
|
149
|
+
await done();
|
|
150
|
+
} else {
|
|
151
|
+
const iframeId = e.data.id;
|
|
152
|
+
(_a = this.iframes.get(iframeId)) == null ? void 0 : _a.remove();
|
|
153
|
+
this.iframes.delete(iframeId);
|
|
154
|
+
}
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
case "error": {
|
|
158
|
+
const iframeId = e.data.id;
|
|
159
|
+
this.iframes.delete(iframeId);
|
|
160
|
+
await client.rpc.onUnhandledError(e.data.error, e.data.errorType);
|
|
161
|
+
if (iframeId === ID_ALL) {
|
|
162
|
+
this.runningFiles.clear();
|
|
163
|
+
} else {
|
|
164
|
+
this.runningFiles.delete(iframeId);
|
|
165
|
+
}
|
|
166
|
+
if (!this.runningFiles.size) {
|
|
167
|
+
await done();
|
|
168
|
+
}
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
default: {
|
|
172
|
+
e.data;
|
|
173
|
+
await client.rpc.onUnhandledError(
|
|
174
|
+
{
|
|
175
|
+
name: "Unexpected Event",
|
|
176
|
+
message: `Unexpected event: ${e.data.type}`
|
|
177
|
+
},
|
|
178
|
+
"Unexpected Event"
|
|
179
|
+
);
|
|
180
|
+
await done();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const orchestrator = new IframeOrchestrator();
|
|
186
|
+
let promiseTesters;
|
|
187
|
+
getBrowserState().createTesters = async (files) => {
|
|
188
|
+
await promiseTesters;
|
|
189
|
+
promiseTesters = orchestrator.createTesters(files).finally(() => {
|
|
190
|
+
promiseTesters = void 0;
|
|
191
|
+
});
|
|
192
|
+
await promiseTesters;
|
|
193
|
+
};
|
|
194
|
+
async function done() {
|
|
195
|
+
await client.rpc.finishBrowserTests(getBrowserState().contextId);
|
|
196
|
+
}
|
|
197
|
+
async function getContainer(config) {
|
|
198
|
+
if (config.browser.ui) {
|
|
199
|
+
const element = document.querySelector("#tester-ui");
|
|
200
|
+
if (!element) {
|
|
201
|
+
return new Promise((resolve) => {
|
|
202
|
+
setTimeout(() => {
|
|
203
|
+
resolve(getContainer(config));
|
|
204
|
+
}, 30);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return element;
|
|
208
|
+
}
|
|
209
|
+
return document.querySelector("#vitest-tester");
|
|
210
|
+
}
|
|
211
|
+
client.waitForConnection().then(async () => {
|
|
212
|
+
const testFiles = getBrowserState().files;
|
|
213
|
+
await orchestrator.init();
|
|
214
|
+
if (testFiles.length) {
|
|
215
|
+
await orchestrator.createTesters(testFiles);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
function generateFileId(file) {
|
|
219
|
+
const config = getConfig();
|
|
220
|
+
const project = config.name || "";
|
|
221
|
+
const path = relative(config.root, file);
|
|
222
|
+
return generateHash(`${path}${project}`);
|
|
223
|
+
}
|
|
224
|
+
async function setIframeViewport(iframe, width, height) {
|
|
225
|
+
const ui = getUiAPI();
|
|
226
|
+
if (ui) {
|
|
227
|
+
await ui.setIframeViewport(width, height);
|
|
228
|
+
} else {
|
|
229
|
+
iframe.style.width = `${width}px`;
|
|
230
|
+
iframe.style.height = `${height}px`;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function debug(...args) {
|
|
234
|
+
const debug2 = getConfig().env.VITEST_BROWSER_DEBUG;
|
|
235
|
+
if (debug2 && debug2 !== "false") {
|
|
236
|
+
client.rpc.debug(...args.map(String));
|
|
237
|
+
}
|
|
238
|
+
}
|