@vitest/browser 3.1.0-beta.2 → 3.1.1

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,3 +1,5 @@
1
+ import { createManualModuleSource } from '@vitest/mocker/node';
2
+
1
3
  const playwrightBrowsers = [
2
4
  "firefox",
3
5
  "webkit",
@@ -13,6 +15,7 @@ class PlaywrightBrowserProvider {
13
15
  contexts = new Map();
14
16
  pages = new Map();
15
17
  browserPromise = null;
18
+ mocker;
16
19
  getSupportedBrowsers() {
17
20
  return playwrightBrowsers;
18
21
  }
@@ -20,6 +23,7 @@ class PlaywrightBrowserProvider {
20
23
  this.project = project;
21
24
  this.browserName = browser;
22
25
  this.options = options;
26
+ this.mocker = this.createMocker();
23
27
  }
24
28
  async openBrowser() {
25
29
  if (this.browserPromise) {
@@ -58,6 +62,109 @@ class PlaywrightBrowserProvider {
58
62
  })();
59
63
  return this.browserPromise;
60
64
  }
65
+ createMocker() {
66
+ const idPreficates = new Map();
67
+ const sessionIds = new Map();
68
+ function createPredicate(sessionId, url) {
69
+ const moduleUrl = new URL(url, "http://localhost");
70
+ const predicate = (url) => {
71
+ if (url.searchParams.has("_vitest_original")) {
72
+ return false;
73
+ }
74
+ if (url.pathname !== moduleUrl.pathname) {
75
+ return false;
76
+ }
77
+ url.searchParams.delete("t");
78
+ url.searchParams.delete("v");
79
+ url.searchParams.delete("import");
80
+ if (url.searchParams.size !== moduleUrl.searchParams.size) {
81
+ return false;
82
+ }
83
+ for (const [param, value] of url.searchParams.entries()) {
84
+ if (moduleUrl.searchParams.get(param) !== value) {
85
+ return false;
86
+ }
87
+ }
88
+ return true;
89
+ };
90
+ const ids = sessionIds.get(sessionId) || [];
91
+ ids.push(moduleUrl.href);
92
+ sessionIds.set(sessionId, ids);
93
+ idPreficates.set(moduleUrl.href, predicate);
94
+ return predicate;
95
+ }
96
+ return {
97
+ register: async (sessionId, module) => {
98
+ const page = this.getPage(sessionId);
99
+ await page.route(createPredicate(sessionId, module.url), async (route) => {
100
+ if (module.type === "manual") {
101
+ const exports = Object.keys(await module.resolve());
102
+ const body = createManualModuleSource(module.url, exports);
103
+ return route.fulfill({
104
+ body,
105
+ headers: getHeaders(this.project.browser.vite.config)
106
+ });
107
+ }
108
+ const isWebkit = this.browserName === "webkit";
109
+ if (isWebkit) {
110
+ const url = module.type === "redirect" ? (() => {
111
+ const url = new URL(module.redirect);
112
+ return url.href.slice(url.origin.length);
113
+ })() : (() => {
114
+ const url = new URL(route.request().url());
115
+ url.searchParams.set("mock", module.type);
116
+ return url.href.slice(url.origin.length);
117
+ })();
118
+ const result = await this.project.browser.vite.transformRequest(url).catch(() => null);
119
+ if (!result) {
120
+ return route.continue();
121
+ }
122
+ let content = result.code;
123
+ if (result.map && "version" in result.map && result.map.mappings) {
124
+ const type = isDirectCSSRequest(url) ? "css" : "js";
125
+ content = getCodeWithSourcemap(type, content.toString(), result.map);
126
+ }
127
+ return route.fulfill({
128
+ body: content,
129
+ headers: getHeaders(this.project.browser.vite.config)
130
+ });
131
+ }
132
+ if (module.type === "redirect") {
133
+ return route.fulfill({
134
+ status: 302,
135
+ headers: { Location: module.redirect }
136
+ });
137
+ } else if (module.type === "automock" || module.type === "autospy") {
138
+ const url = new URL(route.request().url());
139
+ url.searchParams.set("mock", module.type);
140
+ return route.fulfill({
141
+ status: 302,
142
+ headers: { Location: url.href }
143
+ });
144
+ } else ;
145
+ });
146
+ },
147
+ delete: async (sessionId, id) => {
148
+ const page = this.getPage(sessionId);
149
+ const predicate = idPreficates.get(id);
150
+ if (predicate) {
151
+ await page.unroute(predicate).finally(() => idPreficates.delete(id));
152
+ }
153
+ },
154
+ clear: async (sessionId) => {
155
+ const page = this.getPage(sessionId);
156
+ const ids = sessionIds.get(sessionId) || [];
157
+ const promises = ids.map((id) => {
158
+ const predicate = idPreficates.get(id);
159
+ if (predicate) {
160
+ return page.unroute(predicate).finally(() => idPreficates.delete(id));
161
+ }
162
+ return null;
163
+ });
164
+ await Promise.all(promises).finally(() => sessionIds.delete(sessionId));
165
+ }
166
+ };
167
+ }
61
168
  async createContext(sessionId) {
62
169
  if (this.contexts.has(sessionId)) {
63
170
  return this.contexts.get(sessionId);
@@ -66,8 +173,7 @@ class PlaywrightBrowserProvider {
66
173
  const { actionTimeout,...contextOptions } = this.options?.context ?? {};
67
174
  const options = {
68
175
  ...contextOptions,
69
- ignoreHTTPSErrors: true,
70
- serviceWorkers: "allow"
176
+ ignoreHTTPSErrors: true
71
177
  };
72
178
  if (this.project.config.browser.ui) {
73
179
  options.viewport = null;
@@ -100,7 +206,7 @@ class PlaywrightBrowserProvider {
100
206
  const timeout = setTimeout(() => {
101
207
  const err = new Error(`Cannot find "vitest-iframe" on the page. This is a bug in Vitest, please report it.`);
102
208
  reject(err);
103
- }, 1e3);
209
+ }, 1e3).unref();
104
210
  page.on("frameattached", (frame) => {
105
211
  clearTimeout(timeout);
106
212
  resolve(frame);
@@ -166,6 +272,32 @@ class PlaywrightBrowserProvider {
166
272
  await browser?.close();
167
273
  }
168
274
  }
275
+ function getHeaders(config) {
276
+ const headers = { "Content-Type": "application/javascript" };
277
+ for (const name in config.server.headers) {
278
+ headers[name] = String(config.server.headers[name]);
279
+ }
280
+ return headers;
281
+ }
282
+ function getCodeWithSourcemap(type, code, map) {
283
+ if (type === "js") {
284
+ code += `\n//# sourceMappingURL=${genSourceMapUrl(map)}`;
285
+ } else if (type === "css") {
286
+ code += `\n/*# sourceMappingURL=${genSourceMapUrl(map)} */`;
287
+ }
288
+ return code;
289
+ }
290
+ function genSourceMapUrl(map) {
291
+ if (typeof map !== "string") {
292
+ map = JSON.stringify(map);
293
+ }
294
+ return `data:application/json;base64,${Buffer.from(map).toString("base64")}`;
295
+ }
296
+ const CSS_LANGS_RE = /\.(?:css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
297
+ const directRequestRE = /[?&]direct\b/;
298
+ function isDirectCSSRequest(request) {
299
+ return CSS_LANGS_RE.test(request) && directRequestRE.test(request);
300
+ }
169
301
 
170
302
  const webdriverBrowsers = [
171
303
  "firefox",