@vitest/browser 1.5.3 → 1.6.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.
@@ -17,7 +17,7 @@
17
17
  })()
18
18
  </script>
19
19
  <!-- !LOAD_METADATA! -->
20
- <script type="module" crossorigin src="./assets/index-TejngHZE.js"></script>
20
+ <script type="module" crossorigin src="./assets/index-9agQl9q3.js"></script>
21
21
  <link rel="stylesheet" crossorigin href="./assets/index-fUmMsp0O.css">
22
22
  </head>
23
23
  <body>
@@ -1,4 +1,4 @@
1
- import { c as client, g as getConfig, a as getBrowserState, b as channel, r as rpcDone } from "./rpc-slP7oy1q.js";
1
+ import { c as client, g as getConfig, a as getBrowserState, b as channel, r as rpcDone } from "./rpc-mFMlTT5u.js";
2
2
  const url = new URL(location.href);
3
3
  const ID_ALL = "__vitest_all__";
4
4
  const iframes = /* @__PURE__ */ new Map();
@@ -575,7 +575,7 @@ function createClient(url, options = {}) {
575
575
  }
576
576
  const PORT = location.port;
577
577
  const HOST = [location.hostname, PORT].filter(Boolean).join(":");
578
- const ENTRY_URL = `${location.protocol === "https:" ? "wss:" : "ws:"}//${HOST}/__vitest_api__`;
578
+ const ENTRY_URL = `${location.protocol === "https:" ? "wss:" : "ws:"}//${HOST}/__vitest_api__?token=${window.VITEST_API_TOKEN}`;
579
579
  let setCancel = (_) => {
580
580
  };
581
581
  const onCancel = new Promise((resolve2) => {
@@ -10,7 +10,7 @@ var __publicField = (obj, key, value) => {
10
10
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
11
11
  return value;
12
12
  };
13
- import { i as importId, d as rpc, g as getConfig, _ as __vitePreload, b as channel, a as getBrowserState, c as client, l as loadSafeRpc, o as onCancel } from "./rpc-slP7oy1q.js";
13
+ import { i as importId, d as rpc$1, g as getConfig, _ as __vitePreload, b as channel, a as getBrowserState, c as client, l as loadSafeRpc, o as onCancel } from "./rpc-mFMlTT5u.js";
14
14
  function showPopupWarning(name, value, defaultValue) {
15
15
  return (...params) => {
16
16
  const formatedParams = params.map((p) => JSON.stringify(p)).join(", ");
@@ -48,7 +48,7 @@ async function setupConsoleLogSpy() {
48
48
  return;
49
49
  const unknownTestId = "__vitest__unknown_test__";
50
50
  const taskId = ((_b = (_a = globalThis.__vitest_worker__) == null ? void 0 : _a.current) == null ? void 0 : _b.id) ?? unknownTestId;
51
- rpc().sendLog({
51
+ rpc$1().sendLog({
52
52
  content,
53
53
  time: Date$1.now(),
54
54
  taskId,
@@ -121,7 +121,7 @@ ${stack}`);
121
121
  return countReset(label);
122
122
  };
123
123
  }
124
- class BrowserSnapshotEnvironment {
124
+ class VitestBrowserSnapshotEnvironment {
125
125
  getVersion() {
126
126
  return "1";
127
127
  }
@@ -144,6 +144,9 @@ class BrowserSnapshotEnvironment {
144
144
  return rpc().removeSnapshotFile(filepath);
145
145
  }
146
146
  }
147
+ function rpc() {
148
+ return globalThis.__vitest_worker__.rpc;
149
+ }
147
150
  const browserHashMap = /* @__PURE__ */ new Map();
148
151
  function createBrowserRunner(runnerClass, coverageModule) {
149
152
  return class BrowserTestRunner extends runnerClass {
@@ -155,10 +158,10 @@ function createBrowserRunner(runnerClass, coverageModule) {
155
158
  var _a, _b, _c;
156
159
  await ((_a = super.onAfterRunTask) == null ? void 0 : _a.call(this, task));
157
160
  if (this.config.bail && ((_b = task.result) == null ? void 0 : _b.state) === "fail") {
158
- const previousFailures = await rpc().getCountOfFailedTests();
161
+ const previousFailures = await rpc$1().getCountOfFailedTests();
159
162
  const currentFailures = 1 + previousFailures;
160
163
  if (currentFailures >= this.config.bail) {
161
- rpc().onCancel("test-failure");
164
+ rpc$1().onCancel("test-failure");
162
165
  (_c = this.onCancel) == null ? void 0 : _c.call(this, "test-failure");
163
166
  }
164
167
  }
@@ -168,7 +171,7 @@ function createBrowserRunner(runnerClass, coverageModule) {
168
171
  await ((_a = super.onAfterRunFiles) == null ? void 0 : _a.call(this, files));
169
172
  const coverage = await ((_b = coverageModule == null ? void 0 : coverageModule.takeCoverage) == null ? void 0 : _b.call(coverageModule));
170
173
  if (coverage) {
171
- await rpc().onAfterSuiteRun({
174
+ await rpc$1().onAfterSuiteRun({
172
175
  coverage,
173
176
  transformMode: "web",
174
177
  projectName: this.config.name
@@ -182,10 +185,10 @@ function createBrowserRunner(runnerClass, coverageModule) {
182
185
  } catch (_) {
183
186
  }
184
187
  }
185
- return rpc().onCollected(files);
188
+ return rpc$1().onCollected(files);
186
189
  });
187
190
  __publicField(this, "onTaskUpdate", (task) => {
188
- return rpc().onTaskUpdate(task);
191
+ return rpc$1().onTaskUpdate(task);
189
192
  });
190
193
  __publicField(this, "importFile", async (filepath) => {
191
194
  let [test, hash] = this.hashMap.get(filepath) ?? [false, ""];
@@ -208,7 +211,10 @@ async function initiateRunner() {
208
211
  if (cachedRunner)
209
212
  return cachedRunner;
210
213
  const config = getConfig();
211
- const [{ VitestTestRunner, NodeBenchmarkRunner }, { takeCoverageInsideWorker, loadDiffConfig, loadSnapshotSerializers }] = await Promise.all([
214
+ const [
215
+ { VitestTestRunner, NodeBenchmarkRunner },
216
+ { takeCoverageInsideWorker, loadDiffConfig, loadSnapshotSerializers }
217
+ ] = await Promise.all([
212
218
  importId("vitest/runners"),
213
219
  importId("vitest/browser")
214
220
  ]);
@@ -217,7 +223,7 @@ async function initiateRunner() {
217
223
  takeCoverage: () => takeCoverageInsideWorker(config.coverage, { executeId: importId })
218
224
  });
219
225
  if (!config.snapshotOptions.snapshotEnvironment)
220
- config.snapshotOptions.snapshotEnvironment = new BrowserSnapshotEnvironment();
226
+ config.snapshotOptions.snapshotEnvironment = new VitestBrowserSnapshotEnvironment();
221
227
  const runner = new BrowserRunner({
222
228
  config
223
229
  });
@@ -234,7 +240,7 @@ async function updateFilesLocations(files) {
234
240
  const { loadSourceMapUtils } = await importId("vitest/utils");
235
241
  const { TraceMap, originalPositionFor } = await loadSourceMapUtils();
236
242
  const promises = files.map(async (file) => {
237
- const result = await rpc().getBrowserFileSourceMap(file.filepath);
243
+ const result = await rpc$1().getBrowserFileSourceMap(file.filepath);
238
244
  if (!result)
239
245
  return null;
240
246
  const traceMap = new TraceMap(result);
@@ -38,6 +38,7 @@ window.__vitest_browser_runner__ = {
38
38
  config: { __VITEST_CONFIG__ },
39
39
  files: { __VITEST_FILES__ },
40
40
  }
41
+ window.VITEST_API_TOKEN = { __VITEST_API_TOKEN__ }
41
42
 
42
43
  const config = __vitest_browser_runner__.config
43
44
 
@@ -22,8 +22,9 @@
22
22
  }
23
23
  </style>
24
24
  <script>{__VITEST_INJECTOR__}</script>
25
- <script type="module" crossorigin src="/__vitest_browser__/main-v2hiOVax.js"></script>
26
- <link rel="modulepreload" crossorigin href="/__vitest_browser__/rpc-slP7oy1q.js">
25
+ {__VITEST_SCRIPTS__}
26
+ <script type="module" crossorigin src="/__vitest_browser__/main-_I3locjo.js"></script>
27
+ <link rel="modulepreload" crossorigin href="/__vitest_browser__/rpc-mFMlTT5u.js">
27
28
  </head>
28
29
  <body>
29
30
  <iframe id="vitest-ui" src=""></iframe>
@@ -16,8 +16,9 @@
16
16
  }
17
17
  </style>
18
18
  <script>{__VITEST_INJECTOR__}</script>
19
- <script type="module" crossorigin src="/__vitest_browser__/tester-2urZfMAh.js"></script>
20
- <link rel="modulepreload" crossorigin href="/__vitest_browser__/rpc-slP7oy1q.js">
19
+ {__VITEST_SCRIPTS__}
20
+ <script type="module" crossorigin src="/__vitest_browser__/tester-y7J4ROPm.js"></script>
21
+ <link rel="modulepreload" crossorigin href="/__vitest_browser__/rpc-mFMlTT5u.js">
21
22
  </head>
22
23
  <body>
23
24
  {__VITEST_APPEND__}
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ import { fileURLToPath } from 'node:url';
2
2
  import { readFile } from 'node:fs/promises';
3
3
  import sirv from 'sirv';
4
4
  import { coverageConfigDefaults } from 'vitest/config';
5
+ import { slash } from '@vitest/utils';
5
6
  import MagicString from 'magic-string';
6
7
  import { esmWalker } from '@vitest/utils/ast';
7
8
 
@@ -11,7 +12,58 @@ function normalizeWindowsPath(input = "") {
11
12
  }
12
13
  return input.replace(/\\/g, "/");
13
14
  }
15
+
16
+ const _UNC_REGEX = /^[/\\]{2}/;
14
17
  const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
18
+ const _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
19
+ const normalize = function(path) {
20
+ if (path.length === 0) {
21
+ return ".";
22
+ }
23
+ path = normalizeWindowsPath(path);
24
+ const isUNCPath = path.match(_UNC_REGEX);
25
+ const isPathAbsolute = isAbsolute(path);
26
+ const trailingSeparator = path[path.length - 1] === "/";
27
+ path = normalizeString(path, !isPathAbsolute);
28
+ if (path.length === 0) {
29
+ if (isPathAbsolute) {
30
+ return "/";
31
+ }
32
+ return trailingSeparator ? "./" : ".";
33
+ }
34
+ if (trailingSeparator) {
35
+ path += "/";
36
+ }
37
+ if (_DRIVE_LETTER_RE.test(path)) {
38
+ path += "/";
39
+ }
40
+ if (isUNCPath) {
41
+ if (!isPathAbsolute) {
42
+ return `//./${path}`;
43
+ }
44
+ return `//${path}`;
45
+ }
46
+ return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
47
+ };
48
+ const join = function(...arguments_) {
49
+ if (arguments_.length === 0) {
50
+ return ".";
51
+ }
52
+ let joined;
53
+ for (const argument of arguments_) {
54
+ if (argument && argument.length > 0) {
55
+ if (joined === void 0) {
56
+ joined = argument;
57
+ } else {
58
+ joined += `/${argument}`;
59
+ }
60
+ }
61
+ }
62
+ if (joined === void 0) {
63
+ return ".";
64
+ }
65
+ return normalize(joined.replace(/\/\/+/g, "/"));
66
+ };
15
67
  function cwd() {
16
68
  if (typeof process !== "undefined") {
17
69
  return process.cwd().replace(/\\/g, "/");
@@ -345,9 +397,6 @@ export { ${viInjectedKey} }`);
345
397
  };
346
398
  }
347
399
 
348
- function replacer(code, values) {
349
- return code.replace(/{\s*(\w+)\s*}/g, (_, key) => values[key] ?? "");
350
- }
351
400
  var index = (project, base = "/") => {
352
401
  const pkgRoot = resolve(fileURLToPath(import.meta.url), "../..");
353
402
  const distRoot = resolve(pkgRoot, "dist");
@@ -376,6 +425,8 @@ var index = (project, base = "/") => {
376
425
  }
377
426
  next();
378
427
  });
428
+ let indexScripts;
429
+ let testerScripts;
379
430
  server.middlewares.use(async (req, res, next) => {
380
431
  if (!req.url)
381
432
  return next();
@@ -390,12 +441,16 @@ var index = (project, base = "/") => {
390
441
  config.env.VITEST_BROWSER_DEBUG = process.env.VITEST_BROWSER_DEBUG || "";
391
442
  const injector = replacer(await injectorJs, {
392
443
  __VITEST_CONFIG__: JSON.stringify(config),
393
- __VITEST_FILES__: JSON.stringify(files)
444
+ __VITEST_FILES__: JSON.stringify(files),
445
+ __VITEST_API_TOKEN__: JSON.stringify(project.ctx.config.api.token)
394
446
  });
395
447
  if (url.pathname === base) {
448
+ if (!indexScripts)
449
+ indexScripts = await formatScripts(project.config.browser.indexScripts, server);
396
450
  const html2 = replacer(await runnerHtml, {
397
451
  __VITEST_FAVICON__: favicon,
398
452
  __VITEST_TITLE__: "Vitest Browser Runner",
453
+ __VITEST_SCRIPTS__: indexScripts,
399
454
  __VITEST_INJECTOR__: injector
400
455
  });
401
456
  res.write(html2, "utf-8");
@@ -404,9 +459,12 @@ var index = (project, base = "/") => {
404
459
  }
405
460
  const decodedTestFile = decodeURIComponent(url.pathname.slice(testerPrefix.length));
406
461
  const tests = decodedTestFile === "__vitest_all__" || !files.includes(decodedTestFile) ? "__vitest_browser_runner__.files" : JSON.stringify([decodedTestFile]);
462
+ if (!testerScripts)
463
+ testerScripts = await formatScripts(project.config.browser.testerScripts, server);
407
464
  const html = replacer(await testerHtml, {
408
465
  __VITEST_FAVICON__: favicon,
409
466
  __VITEST_TITLE__: "Vitest Browser Tester",
467
+ __VITEST_SCRIPTS__: testerScripts,
410
468
  __VITEST_INJECTOR__: injector,
411
469
  __VITEST_APPEND__: (
412
470
  // TODO: have only a single global variable to not pollute the global scope
@@ -541,5 +599,20 @@ function wrapConfig(config) {
541
599
  testNamePattern: config.testNamePattern ? config.testNamePattern.toString() : void 0
542
600
  };
543
601
  }
602
+ function replacer(code, values) {
603
+ return code.replace(/{\s*(\w+)\s*}/g, (_, key) => values[key] ?? "");
604
+ }
605
+ async function formatScripts(scripts, server) {
606
+ if (!scripts?.length)
607
+ return "";
608
+ const promises = scripts.map(async ({ content, src, async, id, type = "module" }, index) => {
609
+ const srcLink = (src ? (await server.pluginContainer.resolveId(src))?.id : void 0) || src;
610
+ const transformId = srcLink || join(server.config.root, `virtual__${id || `injected-${index}.js`}`);
611
+ await server.moduleGraph.ensureEntryFromUrl(transformId);
612
+ const contentProcessed = content && type === "module" ? (await server.pluginContainer.transform(content, transformId)).code : content;
613
+ return `<script type="${type}"${async ? " async" : ""}${srcLink ? ` src="${slash(`/@fs/${srcLink}`)}"` : ""}>${contentProcessed || ""}<\/script>`;
614
+ });
615
+ return (await Promise.all(promises)).join("\n");
616
+ }
544
617
 
545
618
  export { index as default };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/browser",
3
3
  "type": "module",
4
- "version": "1.5.3",
4
+ "version": "1.6.1",
5
5
  "description": "Browser running for Vitest",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -43,7 +43,7 @@
43
43
  "peerDependencies": {
44
44
  "playwright": "*",
45
45
  "webdriverio": "*",
46
- "vitest": "1.5.3"
46
+ "vitest": "1.6.1"
47
47
  },
48
48
  "peerDependenciesMeta": {
49
49
  "playwright": {
@@ -59,20 +59,20 @@
59
59
  "dependencies": {
60
60
  "magic-string": "^0.30.5",
61
61
  "sirv": "^2.0.4",
62
- "@vitest/utils": "1.5.3"
62
+ "@vitest/utils": "1.6.1"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@types/ws": "^8.5.9",
66
66
  "@wdio/protocols": "^8.29.7",
67
67
  "periscopic": "^4.0.2",
68
- "playwright": "^1.41.0",
68
+ "playwright": "^1.49.1",
69
69
  "playwright-core": "^1.41.0",
70
70
  "safaridriver": "^0.1.2",
71
71
  "webdriverio": "^8.32.2",
72
- "@vitest/ui": "1.5.3",
73
- "@vitest/runner": "1.5.3",
74
- "@vitest/ws-client": "1.5.3",
75
- "vitest": "1.5.3"
72
+ "@vitest/ui": "1.6.1",
73
+ "@vitest/ws-client": "1.6.1",
74
+ "@vitest/runner": "1.6.1",
75
+ "vitest": "1.6.1"
76
76
  },
77
77
  "scripts": {
78
78
  "build": "rimraf dist && pnpm build:node && pnpm build:client",