@vitest/browser 4.0.15 → 4.0.17

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.
@@ -23,7 +23,7 @@
23
23
  })();
24
24
  </script>
25
25
  <!-- !LOAD_METADATA! -->
26
- <script type="module" src="./assets/index-CfDzoXo3.js"></script>
26
+ <script type="module" src="./assets/index-BUCFJtth.js"></script>
27
27
  <link rel="stylesheet" href="./assets/index-DlhE0rqZ.css">
28
28
  </head>
29
29
  <body>
@@ -1,5 +1,6 @@
1
1
  import { g as getBrowserState, a as getConfig, r as relative, b as generateFileHash } from "./utils-uxqdqUz8.js";
2
2
  import { channel, globalChannel, client } from "@vitest/browser/client";
3
+ import { Traces } from "vitest/internal/browser";
3
4
  // @__NO_SIDE_EFFECTS__
4
5
  function getUiAPI() {
5
6
  return window.__vitest_ui_api__;
@@ -10,8 +11,14 @@ class IframeOrchestrator {
10
11
  recreateNonIsolatedIframe = false;
11
12
  iframes = /* @__PURE__ */ new Map();
12
13
  eventTarget = new EventTarget();
14
+ traces;
13
15
  constructor() {
14
16
  debug("init orchestrator", getBrowserState().sessionId);
17
+ const otelConfig = getBrowserState().config.experimental.openTelemetry;
18
+ this.traces = new Traces({
19
+ enabled: !!((otelConfig == null ? void 0 : otelConfig.enabled) && otelConfig.browserSdkPath),
20
+ sdkPath: `/@fs/${otelConfig == null ? void 0 : otelConfig.browserSdkPath}`
21
+ });
15
22
  channel.addEventListener(
16
23
  "message",
17
24
  (e) => this.onIframeEvent(e)
@@ -22,6 +29,21 @@ class IframeOrchestrator {
22
29
  );
23
30
  }
24
31
  async createTesters(options) {
32
+ await this.traces.waitInit();
33
+ this.traces.recordInitSpan(
34
+ this.traces.getContextFromCarrier(getBrowserState().otelCarrier)
35
+ );
36
+ const orchestratorSpan = this.traces.startContextSpan(
37
+ "vitest.browser.orchestrator.run",
38
+ this.traces.getContextFromCarrier(options.otelCarrier)
39
+ );
40
+ orchestratorSpan.span.setAttributes({
41
+ "vitest.browser.files": options.files.map((f) => f.filepath)
42
+ });
43
+ const endSpan = async () => {
44
+ orchestratorSpan.span.end();
45
+ await this.traces.flush();
46
+ };
25
47
  const startTime = performance.now();
26
48
  this.cancelled = false;
27
49
  const config = getConfig();
@@ -35,13 +57,15 @@ class IframeOrchestrator {
35
57
  }
36
58
  }
37
59
  if (config.browser.isolate === false) {
38
- await this.runNonIsolatedTests(container, options, startTime);
60
+ await this.runNonIsolatedTests(container, options, startTime, orchestratorSpan.context);
61
+ await endSpan();
39
62
  return;
40
63
  }
41
64
  this.iframes.forEach((iframe) => iframe.remove());
42
65
  this.iframes.clear();
43
66
  for (let i = 0; i < options.files.length; i++) {
44
67
  if (this.cancelled) {
68
+ await endSpan();
45
69
  return;
46
70
  }
47
71
  const file = options.files[i];
@@ -50,9 +74,11 @@ class IframeOrchestrator {
50
74
  container,
51
75
  file,
52
76
  options,
53
- startTime
77
+ startTime,
78
+ orchestratorSpan.context
54
79
  );
55
80
  }
81
+ await endSpan();
56
82
  }
57
83
  async cleanupTesters() {
58
84
  const config = getConfig();
@@ -75,7 +101,7 @@ class IframeOrchestrator {
75
101
  });
76
102
  this.recreateNonIsolatedIframe = true;
77
103
  }
78
- async runNonIsolatedTests(container, options, startTime) {
104
+ async runNonIsolatedTests(container, options, startTime, otelContext) {
79
105
  if (this.recreateNonIsolatedIframe) {
80
106
  this.recreateNonIsolatedIframe = false;
81
107
  this.iframes.get(ID_ALL).remove();
@@ -84,7 +110,7 @@ class IframeOrchestrator {
84
110
  }
85
111
  if (!this.iframes.has(ID_ALL)) {
86
112
  debug("preparing non-isolated iframe");
87
- await this.prepareIframe(container, ID_ALL, startTime);
113
+ await this.prepareIframe(container, ID_ALL, startTime, otelContext);
88
114
  }
89
115
  const config = getConfig();
90
116
  const { width, height } = config.browser.viewport;
@@ -100,7 +126,7 @@ class IframeOrchestrator {
100
126
  });
101
127
  debug("finished running tests", options.files.join(", "));
102
128
  }
103
- async runIsolatedTestInIframe(container, spec, options, startTime) {
129
+ async runIsolatedTestInIframe(container, spec, options, startTime, otelContext) {
104
130
  const config = getConfig();
105
131
  const { width, height } = config.browser.viewport;
106
132
  const file = spec.filepath;
@@ -108,7 +134,12 @@ class IframeOrchestrator {
108
134
  this.iframes.get(file).remove();
109
135
  this.iframes.delete(file);
110
136
  }
111
- const iframe = await this.prepareIframe(container, file, startTime);
137
+ const iframe = await this.prepareIframe(
138
+ container,
139
+ file,
140
+ startTime,
141
+ otelContext
142
+ );
112
143
  await setIframeViewport(iframe, width, height);
113
144
  await sendEventToIframe({
114
145
  event: "execute",
@@ -127,7 +158,7 @@ class IframeOrchestrator {
127
158
  this.eventTarget.dispatchEvent(event);
128
159
  return error;
129
160
  }
130
- async prepareIframe(container, iframeId, startTime) {
161
+ async prepareIframe(container, iframeId, startTime, otelContext) {
131
162
  const iframe = this.createTestIframe(iframeId);
132
163
  container.appendChild(iframe);
133
164
  await new Promise((resolve, reject) => {
@@ -146,7 +177,8 @@ Expected: ${iframe.src}`
146
177
  sendEventToIframe({
147
178
  event: "prepare",
148
179
  iframeId,
149
- startTime
180
+ startTime,
181
+ otelCarrier: this.traces.getContextCarrier(otelContext)
150
182
  }).then(resolve, (error) => reject(this.dispatchIframeError(error)));
151
183
  }
152
184
  };
@@ -1,7 +1,7 @@
1
1
  import { c as resolve, m as moduleRunner, d as getWorkerState, g as getBrowserState, e as getTestName, a as getConfig } from "./utils-uxqdqUz8.js";
2
2
  import { onCancel, globalChannel, channel, client } from "@vitest/browser/client";
3
3
  import { userEvent, page, server } from "vitest/browser";
4
- import { getSafeTimers, DecodedMap as DecodedMap$1, getOriginalPosition as getOriginalPosition$1, loadDiffConfig, loadSnapshotSerializers, takeCoverageInsideWorker, stringify, format, setupCommonEnv, startCoverageInsideWorker, stopCoverageInsideWorker, startTests, collectTests, SpyModule } from "vitest/internal/browser";
4
+ import { getSafeTimers, DecodedMap as DecodedMap$1, getOriginalPosition as getOriginalPosition$1, loadDiffConfig, loadSnapshotSerializers, takeCoverageInsideWorker, browserFormat, Traces, setupCommonEnv, startCoverageInsideWorker, stopCoverageInsideWorker, startTests, collectTests, SpyModule } from "vitest/internal/browser";
5
5
  import { VitestTestRunner, NodeBenchmarkRunner } from "vitest/runners";
6
6
  const scriptRel = "modulepreload";
7
7
  const assetsURL = function(dep) {
@@ -811,11 +811,13 @@ function createBrowserRunner(runnerClass, mocker, state, coverageModule) {
811
811
  sourceMapCache = /* @__PURE__ */ new Map();
812
812
  method = "run";
813
813
  commands;
814
+ _otel;
814
815
  constructor(options) {
815
816
  super(options.config);
816
817
  this.config = options.config;
817
818
  this.commands = getBrowserState().commands;
818
819
  this.viteEnvironment = "__browser__";
820
+ this._otel = getBrowserState().traces;
819
821
  }
820
822
  setMethod(method) {
821
823
  this.method = method;
@@ -865,28 +867,28 @@ function createBrowserRunner(runnerClass, mocker, state, coverageModule) {
865
867
  if (!this.traces.has(test.id)) {
866
868
  this.traces.set(test.id, []);
867
869
  }
868
- const traces = this.traces.get(test.id);
870
+ const traces2 = this.traces.get(test.id);
869
871
  const { tracePath } = await this.commands.triggerCommand(
870
872
  "__vitest_stopChunkTrace",
871
873
  [{ name }]
872
874
  );
873
- traces.push(tracePath);
875
+ traces2.push(tracePath);
874
876
  };
875
877
  onAfterRunTask = async (task) => {
876
878
  var _a, _b, _c;
877
879
  await ((_a = super.onAfterRunTask) == null ? void 0 : _a.call(this, task));
878
880
  const trace = this.config.browser.trace;
879
- const traces = this.traces.get(task.id) || [];
880
- if (traces.length) {
881
+ const traces2 = this.traces.get(task.id) || [];
882
+ if (traces2.length) {
881
883
  if (trace === "retain-on-failure" && ((_b = task.result) == null ? void 0 : _b.state) === "pass") {
882
884
  await this.commands.triggerCommand(
883
885
  "__vitest_deleteTracing",
884
- [{ traces }]
886
+ [{ traces: traces2 }]
885
887
  );
886
888
  } else {
887
889
  await this.commands.triggerCommand(
888
890
  "__vitest_annotateTraces",
889
- [{ testId: task.id, traces }]
891
+ [{ testId: task.id, traces: traces2 }]
890
892
  );
891
893
  }
892
894
  }
@@ -1024,9 +1026,10 @@ function createBrowserRunner(runnerClass, mocker, state, coverageModule) {
1024
1026
  throw new Error(`Failed to import test file ${filepath}`, { cause: err });
1025
1027
  }
1026
1028
  };
1027
- // disable tracing in the browser for now
1028
- trace = void 0;
1029
- __setTraces = void 0;
1029
+ trace = (name, attributes, cb) => {
1030
+ const options = typeof attributes === "object" ? { attributes } : {};
1031
+ return this._otel.$(`vitest.test.runner.${name}`, options, cb || attributes);
1032
+ };
1030
1033
  };
1031
1034
  }
1032
1035
  let cachedRunner = null;
@@ -1128,7 +1131,7 @@ function setupConsoleLogSpy() {
1128
1131
  console$1.warn = stderr(warn);
1129
1132
  console$1.dir = (item, options) => {
1130
1133
  dir(item, options);
1131
- sendLog("stdout", formatInput(item));
1134
+ sendLog("stdout", browserFormat(item));
1132
1135
  };
1133
1136
  console$1.dirxml = (...args) => {
1134
1137
  dirxml(...args);
@@ -1198,17 +1201,8 @@ function stderr(base) {
1198
1201
  sendLog("stderr", processLog(args));
1199
1202
  };
1200
1203
  }
1201
- function formatInput(input) {
1202
- if (typeof input === "object") {
1203
- return stringify(input, void 0, {
1204
- printBasicPrototype: false,
1205
- escapeString: false
1206
- });
1207
- }
1208
- return format(input);
1209
- }
1210
1204
  function processLog(args) {
1211
- return args.map(formatInput).join(" ");
1205
+ return browserFormat(...args);
1212
1206
  }
1213
1207
  function sendLog(type, content, disableStack) {
1214
1208
  var _a, _b, _c;
@@ -1940,27 +1934,42 @@ class CommandsManager {
1940
1934
  var _a, _b;
1941
1935
  const state = getWorkerState();
1942
1936
  const rpc2 = state.rpc;
1943
- const { sessionId } = getBrowserState();
1937
+ const { sessionId, traces: traces2 } = getBrowserState();
1944
1938
  const filepath = state.filepath || ((_b = (_a = state.current) == null ? void 0 : _a.file) == null ? void 0 : _b.filepath);
1945
1939
  args = args.filter((arg) => arg !== void 0);
1946
1940
  if (this._listeners.length) {
1947
1941
  await Promise.all(this._listeners.map((listener) => listener(command, args)));
1948
1942
  }
1949
- return rpc2.triggerCommand(sessionId, command, filepath, args).catch((err) => {
1950
- var _a2;
1951
- clientError.message = err.message;
1952
- clientError.name = err.name;
1953
- clientError.stack = (_a2 = clientError.stack) == null ? void 0 : _a2.replace(clientError.message, err.message);
1954
- throw clientError;
1955
- });
1943
+ return traces2.$(
1944
+ "vitest.browser.tester.command",
1945
+ {
1946
+ attributes: {
1947
+ "vitest.browser.command": command,
1948
+ "code.file.path": filepath
1949
+ }
1950
+ },
1951
+ () => rpc2.triggerCommand(sessionId, command, filepath, args).catch((err) => {
1952
+ var _a2;
1953
+ clientError.message = err.message;
1954
+ clientError.name = err.name;
1955
+ clientError.stack = (_a2 = clientError.stack) == null ? void 0 : _a2.replace(clientError.message, err.message);
1956
+ throw clientError;
1957
+ })
1958
+ );
1956
1959
  }
1957
1960
  }
1958
- const __vite_import_meta_env__ = { "BASE_URL": "/", "DEV": false, "MODE": "production", "PROD": true, "SSR": false, "VITE_TEST_WATCHER_DEBUG": "false" };
1959
1961
  const debugVar = getConfig().env.VITEST_BROWSER_DEBUG;
1960
1962
  const debug = debugVar && debugVar !== "false" ? (...args) => {
1961
1963
  var _a, _b;
1962
1964
  return (_b = (_a = client.rpc).debug) == null ? void 0 : _b.call(_a, ...args.map(String));
1963
1965
  } : void 0;
1966
+ const otelConfig = getConfig().experimental.openTelemetry;
1967
+ const traces = new Traces({
1968
+ enabled: !!((otelConfig == null ? void 0 : otelConfig.enabled) && (otelConfig == null ? void 0 : otelConfig.browserSdkPath)),
1969
+ sdkPath: `/@fs/${otelConfig == null ? void 0 : otelConfig.browserSdkPath}`
1970
+ });
1971
+ let rootTesterSpan;
1972
+ getBrowserState().traces = traces;
1964
1973
  channel.addEventListener("message", async (e) => {
1965
1974
  await client.waitForConnection();
1966
1975
  const data = e.data;
@@ -1989,9 +1998,19 @@ channel.addEventListener("message", async (e) => {
1989
1998
  }
1990
1999
  case "cleanup": {
1991
2000
  await cleanup().catch((err) => unhandledError(err, "Cleanup Error"));
2001
+ rootTesterSpan == null ? void 0 : rootTesterSpan.span.end();
2002
+ await traces.finish();
1992
2003
  break;
1993
2004
  }
1994
2005
  case "prepare": {
2006
+ await traces.waitInit();
2007
+ const tracesContext = traces.getContextFromCarrier(data.otelCarrier);
2008
+ traces.recordInitSpan(tracesContext);
2009
+ rootTesterSpan = traces.startContextSpan(
2010
+ `vitest.browser.tester.run`,
2011
+ tracesContext
2012
+ );
2013
+ traces.bind(rootTesterSpan.context);
1995
2014
  await prepare(data).catch((err) => unhandledError(err, "Prepare Error"));
1996
2015
  break;
1997
2016
  }
@@ -2021,7 +2040,7 @@ async function prepareTestEnvironment(options) {
2021
2040
  const config = getConfig();
2022
2041
  const rpc2 = createSafeRpc(client);
2023
2042
  const state = getWorkerState();
2024
- state.metaEnv = __vite_import_meta_env__;
2043
+ state.metaEnv = __vitest_browser_import_meta_env_init__;
2025
2044
  state.onCancel = onCancel;
2026
2045
  state.ctx.rpc = rpc2;
2027
2046
  state.rpc = rpc2;
@@ -2080,11 +2099,19 @@ async function executeTests(method, specifications) {
2080
2099
  for (const file of specifications) {
2081
2100
  state.filepath = file.filepath;
2082
2101
  debug == null ? void 0 : debug("running test file", file.filepath);
2083
- if (method === "run") {
2084
- await startTests([file], runner);
2085
- } else {
2086
- await collectTests([file], runner);
2087
- }
2102
+ await traces.$(
2103
+ `vitest.test.runner.${method}.module`,
2104
+ {
2105
+ attributes: { "code.file.path": file.filepath }
2106
+ },
2107
+ async () => {
2108
+ if (method === "run") {
2109
+ await startTests([file], runner);
2110
+ } else {
2111
+ await collectTests([file], runner);
2112
+ }
2113
+ }
2114
+ );
2088
2115
  }
2089
2116
  }
2090
2117
  async function prepare(options) {
@@ -31,7 +31,11 @@ function catchWindowErrors(errorEvent, prop, cb) {
31
31
  cb(e)
32
32
  }
33
33
  else {
34
- console.error(e[prop])
34
+ // `ErrorEvent` doesn't necessary have `ErrotEvent.error` defined
35
+ // but some has `ErrorEvent.message` defined, e.g. ResizeObserver error.
36
+ // https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent/error
37
+ // https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver#observation_errors
38
+ console.error(e.message ? new Error(e.message) : e)
35
39
  }
36
40
  }
37
41
  const addEventListener = window.addEventListener.bind(window)
@@ -9,7 +9,7 @@
9
9
  }
10
10
 
11
11
  const { evaluatedModules } = __vitest_worker__
12
- const moduleId = crypto.randomUUID()
12
+ const moduleId = `${Math.random()}`
13
13
  const viteModule = evaluatedModules.ensureModule(moduleId, moduleId)
14
14
 
15
15
  viteModule.evaluated = false
@@ -38,6 +38,7 @@
38
38
  type: { __VITEST_TYPE__ },
39
39
  sessionId: { __VITEST_SESSION_ID__ },
40
40
  testerId: { __VITEST_TESTER_ID__ },
41
+ otelCarrier: { __VITEST_OTEL_CARRIER__ },
41
42
  provider: { __VITEST_PROVIDER__ },
42
43
  method: { __VITEST_METHOD__ },
43
44
  providedContext: { __VITEST_PROVIDED_CONTEXT__ },
@@ -26,7 +26,7 @@
26
26
  {__VITEST_INJECTOR__}
27
27
  {__VITEST_ERROR_CATCHER__}
28
28
  {__VITEST_SCRIPTS__}
29
- <script type="module" crossorigin src="/__vitest_browser__/orchestrator-8U3FyXSU.js"></script>
29
+ <script type="module" crossorigin src="/__vitest_browser__/orchestrator-S_3e_uzt.js"></script>
30
30
  <link rel="modulepreload" crossorigin href="/__vitest_browser__/utils-uxqdqUz8.js">
31
31
  </head>
32
32
  <body>
@@ -5,7 +5,7 @@
5
5
  <link rel="icon" href="{__VITEST_FAVICON__}" type="image/svg+xml">
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
7
  <title>Vitest Browser Tester</title>
8
- <script type="module" crossorigin src="/__vitest_browser__/tester-CrGChK3u.js"></script>
8
+ <script type="module" crossorigin src="/__vitest_browser__/tester-k74mgIRa.js"></script>
9
9
  <link rel="modulepreload" crossorigin href="/__vitest_browser__/utils-uxqdqUz8.js">
10
10
  </head>
11
11
  <body>