@chromatic-com/vitest 0.1.9 → 0.1.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.
package/dist/setupFile.js CHANGED
@@ -1,11 +1,11 @@
1
1
  // src/browser/setupFile.ts
2
- import { afterEach, beforeEach, inject } from "vitest";
3
- import { commands as commands3 } from "vitest/browser";
2
+ import { afterEach, beforeEach, inject as inject2 } from "vitest";
3
+ import { commands as commands4 } from "vitest/browser";
4
4
 
5
5
  // src/browser/public/takeSnapshot.ts
6
6
  import { assert } from "vitest";
7
- import { commands } from "vitest/browser";
8
- import { snapshot, createMirror } from "@chromaui/rrweb-snapshot";
7
+ import { commands as commands2 } from "vitest/browser";
8
+ import { snapshot as rrwebSnapshot, createMirror } from "@chromaui/rrweb-snapshot";
9
9
 
10
10
  // src/browser/getCurrentTest.ts
11
11
  import * as vitest from "vitest";
@@ -26,6 +26,17 @@ function isChromium() {
26
26
  return server.browser === "chromium";
27
27
  }
28
28
 
29
+ // src/browser/telemetry.ts
30
+ import { commands } from "vitest/browser";
31
+ import { inject } from "vitest";
32
+ function trackEvent(event) {
33
+ const enabled = inject("__chromatic_options")?.telemetry.enabled ?? false;
34
+ if (enabled) {
35
+ void commands.__chromatic_telemetry(event).catch(() => {
36
+ });
37
+ }
38
+ }
39
+
29
40
  // src/browser/public/takeSnapshot.ts
30
41
  async function takeSnapshot(name, options) {
31
42
  if (!isChromium()) {
@@ -33,18 +44,26 @@ async function takeSnapshot(name, options) {
33
44
  }
34
45
  const test = getCurrentTest();
35
46
  if (!test) {
47
+ trackEvent({
48
+ eventType: "take_snapshot_invalid_call",
49
+ level: "error",
50
+ payload: { isInsideTest: false, isRegisteredTest: void 0, isAwaited: void 0 }
51
+ });
36
52
  throw new TypeError("takeSnapshot() must be called within a test()");
37
53
  }
38
54
  if (!test.meta.__chromatic_isRegistered) {
55
+ trackEvent({
56
+ eventType: "take_snapshot_invalid_call",
57
+ level: "error",
58
+ payload: { isInsideTest: true, isRegisteredTest: false, isAwaited: void 0 }
59
+ });
39
60
  throw new TypeError(
40
61
  `takeSnapshot() cannot be called in a test that is not registered for Chromatic plugin.
41
62
  Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabled.`
42
63
  );
43
64
  }
44
- test.meta.__chromatic_isTakeSnapshotCalled = true;
45
65
  const mirror = createMirror();
46
- const domSnapshot = snapshot(document, { recordCanvas: true, mirror });
47
- assert(domSnapshot, "Failed to capture DOM snapshot");
66
+ const domSnapshot = snapshot({ document, mirror, onError: trackSnapshotError("capture") });
48
67
  const pseudoClassIds = {};
49
68
  for (const className of [":hover", ":focus", ":focus-visible", ":active"]) {
50
69
  const elements = document.querySelectorAll(className);
@@ -52,10 +71,18 @@ Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabl
52
71
  pseudoClassIds[className] = ids;
53
72
  }
54
73
  const save = async () => {
55
- await replaceBlobUrls(domSnapshot);
56
- await commands.__chromatic_uploadDOMSnapshot(test.id, domSnapshot, pseudoClassIds, name);
74
+ await replaceBlobUrls(domSnapshot).catch(trackSnapshotError("replace-blob-urls"));
75
+ await commands2.__chromatic_uploadDOMSnapshot(
76
+ test.id,
77
+ domSnapshot,
78
+ pseudoClassIds,
79
+ name ?? null,
80
+ // Convert undefined to null to avoid https://github.com/vitest-dev/vitest/issues/10864
81
+ options
82
+ ).catch(trackSnapshotError("upload"));
83
+ test.meta.__chromatic_isTakeSnapshotCalled = true;
57
84
  };
58
- if (options?.ignoreUnawaited) {
85
+ if (options?.isAutomaticSnapshot) {
59
86
  return await save();
60
87
  }
61
88
  const error = new Error("takeSnapshot() call was not awaited!");
@@ -69,6 +96,33 @@ Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabl
69
96
  test.meta.__chromatic_pendingTakeSnapshots?.splice(index, 1);
70
97
  }
71
98
  });
99
+ function trackSnapshotError(operation) {
100
+ return function onError(error2) {
101
+ trackEvent({
102
+ eventType: "snapshot_error",
103
+ level: "error",
104
+ payload: {
105
+ operation,
106
+ isAutomaticSnapshot: options?.isAutomaticSnapshot ?? false,
107
+ error: error2 instanceof Error ? error2.stack ?? error2.message : String(error2)
108
+ }
109
+ });
110
+ throw error2;
111
+ };
112
+ }
113
+ }
114
+ function snapshot(options) {
115
+ try {
116
+ const domSnapshot = rrwebSnapshot(options.document, {
117
+ recordCanvas: true,
118
+ mirror: options.mirror
119
+ });
120
+ assert(domSnapshot, "Failed to capture DOM snapshot");
121
+ return domSnapshot;
122
+ } catch (error) {
123
+ options.onError(error);
124
+ throw error;
125
+ }
72
126
  }
73
127
  async function replaceBlobUrls(node) {
74
128
  if (!("childNodes" in node)) {
@@ -97,27 +151,61 @@ async function toDataURL(url) {
97
151
  }
98
152
 
99
153
  // src/browser/public/waitForIdleNetwork.ts
100
- import { commands as commands2 } from "vitest/browser";
101
- async function waitForIdleNetwork(timeout) {
154
+ import { commands as commands3 } from "vitest/browser";
155
+ async function waitForIdleNetwork(timeout, options) {
102
156
  if (!isChromium()) {
103
157
  return;
104
158
  }
159
+ const isCalledByUser = options?._internal !== true;
105
160
  const test = getCurrentTest();
106
161
  if (!test) {
162
+ trackEvent({
163
+ eventType: "wait_for_idle_network_invalid_call",
164
+ level: "error",
165
+ payload: {
166
+ isInsideTest: false,
167
+ isRegisteredTest: void 0,
168
+ isCalledByUser
169
+ }
170
+ });
107
171
  throw new TypeError("waitForIdleNetwork() must be called within a test()");
108
172
  }
109
173
  if (!test.meta.__chromatic_isRegistered) {
174
+ trackEvent({
175
+ eventType: "wait_for_idle_network_invalid_call",
176
+ level: "error",
177
+ payload: {
178
+ isInsideTest: true,
179
+ isRegisteredTest: false,
180
+ isCalledByUser
181
+ }
182
+ });
110
183
  throw new TypeError(
111
184
  `waitForIdleNetwork() cannot be called in a test that is not registered for Chromatic plugin.
112
185
  Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabled.`
113
186
  );
114
187
  }
115
- return await commands2.__chromatic_waitForIdleNetwork(timeout);
188
+ if (isCalledByUser) {
189
+ trackEvent({ eventType: "wait_for_idle_network_called", level: "info", payload: { timeout } });
190
+ }
191
+ try {
192
+ await commands3.__chromatic_waitForIdleNetwork(timeout);
193
+ } catch (error) {
194
+ const isTimeoutError = error instanceof Error && error.message.includes("Timed out waiting for network to be idle");
195
+ if (isTimeoutError) {
196
+ trackEvent({
197
+ eventType: "wait_for_idle_network_timeout",
198
+ level: "error",
199
+ payload: { timeout, isCalledByUser }
200
+ });
201
+ }
202
+ throw error;
203
+ }
116
204
  }
117
205
 
118
206
  // src/browser/setupFile.ts
119
207
  beforeEach(async ({ task }) => {
120
- const options = inject("__chromatic_options");
208
+ const options = inject2("__chromatic_options");
121
209
  if (options.tags) {
122
210
  const hasMatchingTag = task.tags?.some((tag) => options.tags?.includes(tag));
123
211
  if (!hasMatchingTag) {
@@ -130,7 +218,7 @@ beforeEach(async ({ task }) => {
130
218
  task.meta.__chromatic_options ||= {};
131
219
  task.meta.__chromatic_options.disableAutoSnapshot ??= options.disableAutoSnapshot;
132
220
  task.meta.__chromatic_options.resourceArchiveTimeout ??= options.resourceArchiveTimeout;
133
- await commands3.__chromatic_interceptFetch();
221
+ await commands4.__chromatic_interceptFetch();
134
222
  return async function beforeEachCleanup() {
135
223
  const {
136
224
  __chromatic_isTakeSnapshotCalled: isTakeSnapshotCalled,
@@ -144,10 +232,10 @@ beforeEach(async ({ task }) => {
144
232
  throw new PendingSnapshotsError(pendingTakeSnapshots);
145
233
  }
146
234
  if (!isTakeSnapshotCalled) {
147
- await commands3.__chromatic_stopWithoutSnapshots(task.id);
235
+ await commands4.__chromatic_stopWithoutSnapshots(task.id);
148
236
  return cleanup();
149
237
  }
150
- await commands3.__chromatic_writeTestResult(task.id, task.meta.__chromatic_options);
238
+ await commands4.__chromatic_writeTestResult(task.id, task.meta.__chromatic_options);
151
239
  return cleanup();
152
240
  };
153
241
  function cleanup() {
@@ -172,10 +260,10 @@ afterEach(async ({ task }) => {
172
260
  }
173
261
  if (testOptions.resourceArchiveTimeout !== 0) {
174
262
  await document.fonts.ready;
175
- await waitForIdleNetwork(testOptions.resourceArchiveTimeout);
263
+ await waitForIdleNetwork(testOptions.resourceArchiveTimeout, { _internal: true });
176
264
  }
177
265
  if (!options.disableAutoSnapshot) {
178
- await takeSnapshot(void 0, { ignoreUnawaited: true });
266
+ await takeSnapshot(void 0, { isAutomaticSnapshot: true });
179
267
  }
180
268
  });
181
269
  var PendingSnapshotsError = class extends AggregateError {
@@ -186,5 +274,14 @@ var PendingSnapshotsError = class extends AggregateError {
186
274
  );
187
275
  this.pendingCalls = pendingCalls;
188
276
  this.name = "PendingSnapshotsError";
277
+ trackEvent({
278
+ eventType: "take_snapshot_invalid_call",
279
+ level: "error",
280
+ payload: {
281
+ isAwaited: false,
282
+ isInsideTest: true,
283
+ isRegisteredTest: true
284
+ }
285
+ });
189
286
  }
190
287
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromatic-com/vitest",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "Chromatic Visual Regression Testing for Vitest",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,6 +33,8 @@
33
33
  "dependencies": {
34
34
  "@chromaui/rrweb-snapshot": "2.0.0-alpha.19-noAbsolute",
35
35
  "storybook": "10.2.13",
36
+ "std-env": "^4.2.0",
37
+ "tinyexec": "^1.2.4",
36
38
  "tinyrainbow": "^3.1.0"
37
39
  },
38
40
  "devDependencies": {