@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/bin/archive-storybook.js +309 -8
- package/dist/bin/build-archive-storybook.js +324 -8
- package/dist/index.js +115 -12
- package/dist/plugin.js +505 -54
- package/dist/setupFile.js +116 -19
- package/package.json +3 -1
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var __require = /* @__PURE__ */ ((
|
|
2
|
+
var __require = /* @__PURE__ */ ((x2) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x2, {
|
|
3
3
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
|
-
}) :
|
|
4
|
+
}) : x2)(function(x2) {
|
|
5
5
|
if (typeof require !== "undefined")
|
|
6
6
|
return require.apply(this, arguments);
|
|
7
|
-
throw Error('Dynamic require of "' +
|
|
7
|
+
throw Error('Dynamic require of "' + x2 + '" is not supported');
|
|
8
8
|
});
|
|
9
9
|
|
|
10
10
|
// ../shared/src/archive-storybook/index.ts
|
|
11
|
-
import {
|
|
11
|
+
import { spawn } from "child_process";
|
|
12
12
|
import { resolve, dirname } from "path";
|
|
13
13
|
import { readFileSync } from "fs";
|
|
14
14
|
import { createRequire } from "module";
|
|
@@ -40,9 +40,35 @@ Please make sure that you have run your E2E tests, or have set the CHROMATIC_ARC
|
|
|
40
40
|
|
|
41
41
|
// ../shared/src/archive-storybook/index.ts
|
|
42
42
|
var req = __require.resolve ? __require : createRequire(import.meta.url);
|
|
43
|
-
function buildArchiveStorybook(processArgs, configDir2, defaultOutputDir) {
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
async function buildArchiveStorybook(processArgs, configDir2, defaultOutputDir, callbacks) {
|
|
44
|
+
assertOutputDirectory(defaultOutputDir, callbacks);
|
|
45
|
+
await runBin([binPath(), "build", ...processArgs, "-c", configDir2]);
|
|
46
|
+
}
|
|
47
|
+
function assertOutputDirectory(outputDirectory, callbacks) {
|
|
48
|
+
try {
|
|
49
|
+
checkArchivesDirExists(outputDirectory);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
callbacks?.onArchivesCheck(error);
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
callbacks?.onArchivesCheck();
|
|
55
|
+
}
|
|
56
|
+
function runBin(args2) {
|
|
57
|
+
return new Promise((resolvePromise, reject) => {
|
|
58
|
+
const child = spawn("node", args2, { stdio: "inherit" });
|
|
59
|
+
child.on("error", reject);
|
|
60
|
+
child.on("close", (code, signal) => {
|
|
61
|
+
if (code === 0 || signal === "SIGINT" || signal === "SIGTERM") {
|
|
62
|
+
resolvePromise();
|
|
63
|
+
} else {
|
|
64
|
+
reject(
|
|
65
|
+
Object.assign(new Error(`Storybook exited with ${signal ?? `code ${code}`}`), {
|
|
66
|
+
status: code
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
|
46
72
|
}
|
|
47
73
|
function binPath() {
|
|
48
74
|
const filename = req.resolve("storybook/package.json");
|
|
@@ -56,12 +82,302 @@ import path2 from "path";
|
|
|
56
82
|
// src/constants.ts
|
|
57
83
|
var DEFAULT_OUTPUT_DIR = ".vitest/chromatic";
|
|
58
84
|
|
|
85
|
+
// src/node/telemetry/constants.ts
|
|
86
|
+
var EVENT_TYPE_PREFIX = "vitest_";
|
|
87
|
+
var TELEMETRY_FETCH_TIMEOUT_MS = 5e3;
|
|
88
|
+
var TELEMETRY_URL = "https://analytics.chromatic.com";
|
|
89
|
+
var TELEMETRY_LOG_FILE = "telemetry.jsonl";
|
|
90
|
+
var TELEMETRY_METADATA_FILE = "telemetry-metadata.json";
|
|
91
|
+
|
|
92
|
+
// src/node/telemetry/env.ts
|
|
93
|
+
import { readFileSync as readFileSync2 } from "node:fs";
|
|
94
|
+
import { resolve as resolve2 } from "node:path";
|
|
95
|
+
import util from "node:util";
|
|
96
|
+
import { env } from "std-env";
|
|
97
|
+
var dotEnv = void 0;
|
|
98
|
+
function getEnv(name) {
|
|
99
|
+
if (dotEnv === void 0) {
|
|
100
|
+
if (typeof util.parseEnv !== "function") {
|
|
101
|
+
dotEnv = {};
|
|
102
|
+
} else {
|
|
103
|
+
try {
|
|
104
|
+
dotEnv = util.parseEnv(readFileSync2(resolve2(process.cwd(), ".env"), "utf8"));
|
|
105
|
+
} catch {
|
|
106
|
+
dotEnv = {};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return env[name] ?? dotEnv[name];
|
|
111
|
+
}
|
|
112
|
+
function resolveTelemetryOptions() {
|
|
113
|
+
return {
|
|
114
|
+
// Enabled by default, disabled by env variables
|
|
115
|
+
enabled: isTruthyEnv(getEnv("CHROMATIC_DISABLE_TELEMETRY")) === false && isTruthyEnv(getEnv("DO_NOT_TRACK")) === false,
|
|
116
|
+
// Disabled by default, enabled by env variable
|
|
117
|
+
logToFile: isTruthyEnv(getEnv("CHROMATIC_TELEMETRY_LOG_TO_FILE"))
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function isTruthyEnv(value) {
|
|
121
|
+
return value !== void 0 && value !== "" && value !== "0" && value !== "false";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// src/node/telemetry/track.ts
|
|
125
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
126
|
+
import { appendFile, mkdir } from "node:fs/promises";
|
|
127
|
+
import { resolve as resolve4 } from "node:path";
|
|
128
|
+
import { isCI, nodeVersion } from "std-env";
|
|
129
|
+
|
|
130
|
+
// package.json
|
|
131
|
+
var version = "0.1.10";
|
|
132
|
+
|
|
133
|
+
// src/node/telemetry/metadata.ts
|
|
134
|
+
import { createHash } from "node:crypto";
|
|
135
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
136
|
+
import { writeFile, readFile } from "node:fs/promises";
|
|
137
|
+
import { createRequire as createRequire2 } from "node:module";
|
|
138
|
+
import { join, resolve as resolve3 } from "node:path";
|
|
139
|
+
import { x } from "tinyexec";
|
|
140
|
+
import { env as env2 } from "std-env";
|
|
141
|
+
var userAgentMatch = env2.npm_config_user_agent?.match(/^([^/\s]+)\/(\S+)/);
|
|
142
|
+
var packageManager = {
|
|
143
|
+
name: userAgentMatch?.[1] || "unknown",
|
|
144
|
+
version: userAgentMatch?.[2] || "unknown"
|
|
145
|
+
};
|
|
146
|
+
async function createProjectId(root) {
|
|
147
|
+
let remote = void 0;
|
|
148
|
+
try {
|
|
149
|
+
const result = await x("git", ["remote", "get-url", "origin"], { nodeOptions: { cwd: root } });
|
|
150
|
+
remote = result.exitCode === 0 ? result.stdout.trim().toLowerCase() : void 0;
|
|
151
|
+
} catch {
|
|
152
|
+
}
|
|
153
|
+
if (!remote) {
|
|
154
|
+
return hash(root);
|
|
155
|
+
}
|
|
156
|
+
const hasProtocol = remote.includes("://");
|
|
157
|
+
remote = remote.replace(/^[a-z+]+:\/\//, "").replace(/^[^@/]+@/, "");
|
|
158
|
+
if (!hasProtocol) {
|
|
159
|
+
remote = remote.replace(":", "/");
|
|
160
|
+
}
|
|
161
|
+
remote = remote.replace(/\.git$/, "").replace(/\/+$/, "");
|
|
162
|
+
return hash(remote);
|
|
163
|
+
}
|
|
164
|
+
function hash(value) {
|
|
165
|
+
return createHash("sha256").update(`chromatic-vitest-telemetry-v1:${value}`).digest("hex");
|
|
166
|
+
}
|
|
167
|
+
function getChromaticVersion(root) {
|
|
168
|
+
try {
|
|
169
|
+
const require2 = createRequire2(join(root, "package.json"));
|
|
170
|
+
return require2("chromatic/package.json").version || "unknown";
|
|
171
|
+
} catch {
|
|
172
|
+
return "unknown";
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
async function writeTelemetryMetadata(outputDirectory, data) {
|
|
176
|
+
await writeFile(
|
|
177
|
+
resolve3(outputDirectory, TELEMETRY_METADATA_FILE),
|
|
178
|
+
JSON.stringify(data, null, 2),
|
|
179
|
+
"utf8"
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
async function readTelemetryMetadata(outputDirectory) {
|
|
183
|
+
const defaults = {
|
|
184
|
+
sessionId: "unknown",
|
|
185
|
+
projectId: "unknown",
|
|
186
|
+
chromaticVersion: "unknown",
|
|
187
|
+
vitestVersion: "unknown",
|
|
188
|
+
isVitestProjects: false
|
|
189
|
+
};
|
|
190
|
+
try {
|
|
191
|
+
const filename = resolve3(outputDirectory, TELEMETRY_METADATA_FILE);
|
|
192
|
+
if (!existsSync2(filename)) {
|
|
193
|
+
return defaults;
|
|
194
|
+
}
|
|
195
|
+
const content = await readFile(filename, "utf8");
|
|
196
|
+
const json = JSON.parse(content);
|
|
197
|
+
return { ...defaults, ...json };
|
|
198
|
+
} catch {
|
|
199
|
+
return defaults;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/node/telemetry/sanitize.ts
|
|
204
|
+
import { sep } from "node:path";
|
|
205
|
+
import { homedir } from "node:os";
|
|
206
|
+
function sanitizeError(error) {
|
|
207
|
+
if (error instanceof Error && error.stack) {
|
|
208
|
+
const raw = error.stack.includes(error.message) ? error.stack : `${error.message}
|
|
209
|
+
Stack: ${error.stack}`;
|
|
210
|
+
return sanitizeString(raw).slice(0, 2e3);
|
|
211
|
+
}
|
|
212
|
+
return sanitizeString(error instanceof Error ? error.message : String(error)).slice(0, 1e3);
|
|
213
|
+
}
|
|
214
|
+
function sanitizeString(value) {
|
|
215
|
+
return value.replaceAll(sep, "/").replace(toPathRegExp(process.cwd()), "<process-cwd>").replace(toPathRegExp(homedir()), "<homedir>");
|
|
216
|
+
}
|
|
217
|
+
function toPathRegExp(path3) {
|
|
218
|
+
const normalized = path3.replaceAll(sep, "/");
|
|
219
|
+
const escaped = normalized.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
220
|
+
return new RegExp(escaped, "gi");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/node/telemetry/session.ts
|
|
224
|
+
import { randomUUID } from "node:crypto";
|
|
225
|
+
var session = {
|
|
226
|
+
id: randomUUID(),
|
|
227
|
+
projectId: void 0,
|
|
228
|
+
chromaticVersion: void 0,
|
|
229
|
+
/**
|
|
230
|
+
* Directories used by `telemetry.logToFile`.
|
|
231
|
+
* Held in session to avoid race conditions when Vitest projects are used.
|
|
232
|
+
* Multiple projects could try to clean up the same directory while other one is writing there already.
|
|
233
|
+
*/
|
|
234
|
+
cleanedLogFileDirectories: /* @__PURE__ */ new Set(),
|
|
235
|
+
/** Promises to wait before Vitest exists */
|
|
236
|
+
cleanups: /* @__PURE__ */ new Set(),
|
|
237
|
+
/** Whether cleanup hooks were already registered for this session */
|
|
238
|
+
cleanupRegistered: false,
|
|
239
|
+
/** Whether internal `.vitest/chromatic/telemetry-metadata.json` has been written */
|
|
240
|
+
isMetadataWritten: false,
|
|
241
|
+
/** Contents of `.vitest/chromatic/telemetry-metadata.json` */
|
|
242
|
+
telemetryMetadata: void 0
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// src/node/telemetry/track.ts
|
|
246
|
+
async function trackCliEvent(event, options) {
|
|
247
|
+
const telemetry = resolveTelemetryOptions();
|
|
248
|
+
if (!telemetry.enabled) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
let telemetryMetadata = session.telemetryMetadata;
|
|
252
|
+
if (session.telemetryMetadata === void 0) {
|
|
253
|
+
telemetryMetadata = await readTelemetryMetadata(options.outputDirectory);
|
|
254
|
+
session.telemetryMetadata = telemetryMetadata;
|
|
255
|
+
if (!("disabled" in telemetryMetadata)) {
|
|
256
|
+
session.id = telemetryMetadata.sessionId;
|
|
257
|
+
session.projectId = telemetryMetadata.projectId;
|
|
258
|
+
session.chromaticVersion = telemetryMetadata.chromaticVersion;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
await _trackEvent(
|
|
262
|
+
event,
|
|
263
|
+
{
|
|
264
|
+
version: telemetryMetadata.vitestVersion,
|
|
265
|
+
config: { root: options.outputDirectory },
|
|
266
|
+
projects: telemetryMetadata.isVitestProjects ? [1, 2] : []
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
outputDirectory: options.outputDirectory,
|
|
270
|
+
telemetry
|
|
271
|
+
}
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
async function _trackEvent(event, vitest, options) {
|
|
275
|
+
const url = getEnv("CHROMATIC_TELEMETRY_URL") || TELEMETRY_URL;
|
|
276
|
+
const root = vitest.config.root;
|
|
277
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
278
|
+
const outputDirectory = resolve4(root, options.outputDirectory);
|
|
279
|
+
session.projectId ||= await createProjectId(root);
|
|
280
|
+
session.chromaticVersion ||= getChromaticVersion(root);
|
|
281
|
+
const wireEvent = {
|
|
282
|
+
id: randomUUID2(),
|
|
283
|
+
sessionId: session.id,
|
|
284
|
+
projectId: session.projectId,
|
|
285
|
+
timestamp,
|
|
286
|
+
eventType: `${EVENT_TYPE_PREFIX}${event.eventType}`,
|
|
287
|
+
level: event.level,
|
|
288
|
+
payload: event.payload,
|
|
289
|
+
metadata: {
|
|
290
|
+
isCI,
|
|
291
|
+
pluginVersion: version,
|
|
292
|
+
nodeVersion: nodeVersion || "unknown",
|
|
293
|
+
vitestVersion: vitest.version,
|
|
294
|
+
isVitestProjects: vitest.projects.length > 1,
|
|
295
|
+
packageManager: packageManager.name,
|
|
296
|
+
packageManagerVersion: packageManager.version,
|
|
297
|
+
chromaticVersion: session.chromaticVersion
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
if (wireEvent.level === "error" && "error" in wireEvent.payload) {
|
|
301
|
+
wireEvent.payload.error = sanitizeError(wireEvent.payload.error);
|
|
302
|
+
}
|
|
303
|
+
let error = void 0;
|
|
304
|
+
try {
|
|
305
|
+
await fetch(`${url}/telemetry/v1/vitest/events`, {
|
|
306
|
+
method: "POST",
|
|
307
|
+
headers: { "Content-Type": "application/json" },
|
|
308
|
+
body: JSON.stringify(wireEvent),
|
|
309
|
+
signal: AbortSignal.timeout(TELEMETRY_FETCH_TIMEOUT_MS)
|
|
310
|
+
});
|
|
311
|
+
} catch (err) {
|
|
312
|
+
error = err instanceof Error ? err.message : String(err);
|
|
313
|
+
}
|
|
314
|
+
if (options.telemetry.logToFile) {
|
|
315
|
+
const logFile = resolve4(outputDirectory, TELEMETRY_LOG_FILE);
|
|
316
|
+
await mkdir(outputDirectory, { recursive: true });
|
|
317
|
+
await appendFile(logFile, `${JSON.stringify(wireEvent)}
|
|
318
|
+
`, "utf8");
|
|
319
|
+
if (error) {
|
|
320
|
+
await appendFile(logFile, `${JSON.stringify({ telemetryPostError: error })}
|
|
321
|
+
`, "utf8");
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (!session.isMetadataWritten) {
|
|
325
|
+
session.isMetadataWritten = true;
|
|
326
|
+
await writeTelemetryMetadata(outputDirectory, {
|
|
327
|
+
...wireEvent.metadata,
|
|
328
|
+
sessionId: wireEvent.sessionId,
|
|
329
|
+
projectId: wireEvent.projectId
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
59
334
|
// src/bin/build-archive-storybook.ts
|
|
60
335
|
var args = process.argv.slice(2);
|
|
61
336
|
var configDir = path2.resolve(import.meta.dirname, "../storybook-config");
|
|
337
|
+
var isCalledFromCLI = process.env.STORYBOOK_INVOKED_BY === "chromatic";
|
|
338
|
+
var chromaticProjectId = process.env.CHROMATIC_PROJECT_ID || "unknown";
|
|
339
|
+
var queue = Promise.resolve();
|
|
62
340
|
try {
|
|
63
|
-
|
|
341
|
+
trackEvent2({
|
|
342
|
+
eventType: "storybook_build_started",
|
|
343
|
+
level: "info",
|
|
344
|
+
payload: { isCalledFromCLI, chromaticProjectId }
|
|
345
|
+
});
|
|
346
|
+
await buildArchiveStorybook(args, configDir, DEFAULT_OUTPUT_DIR, { onArchivesCheck });
|
|
347
|
+
trackEvent2({
|
|
348
|
+
eventType: "storybook_build_completed",
|
|
349
|
+
level: "info",
|
|
350
|
+
payload: { success: true, error: void 0, chromaticProjectId }
|
|
351
|
+
});
|
|
64
352
|
} catch (err) {
|
|
353
|
+
trackEvent2({
|
|
354
|
+
eventType: "storybook_build_completed",
|
|
355
|
+
level: "error",
|
|
356
|
+
payload: { success: false, error: err, chromaticProjectId }
|
|
357
|
+
});
|
|
65
358
|
console.error(err.message);
|
|
66
359
|
process.exitCode = 1;
|
|
67
360
|
}
|
|
361
|
+
await queue;
|
|
362
|
+
function trackEvent2(event) {
|
|
363
|
+
queue = queue.then(
|
|
364
|
+
() => trackCliEvent(event, { outputDirectory: path2.dirname(archivesDir(DEFAULT_OUTPUT_DIR)) }).catch(
|
|
365
|
+
() => {
|
|
366
|
+
}
|
|
367
|
+
// Ignore telemetry errors
|
|
368
|
+
)
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
function onArchivesCheck(error) {
|
|
372
|
+
trackEvent2({
|
|
373
|
+
eventType: "archives_resolved",
|
|
374
|
+
level: error ? "error" : "info",
|
|
375
|
+
payload: {
|
|
376
|
+
success: error == void 0,
|
|
377
|
+
error,
|
|
378
|
+
isCustomLocation: process.env.CHROMATIC_ARCHIVE_LOCATION != void 0,
|
|
379
|
+
command: "buildArchiveStorybook",
|
|
380
|
+
chromaticProjectId
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/browser/public/takeSnapshot.ts
|
|
2
2
|
import { assert } from "vitest";
|
|
3
|
-
import { commands } from "vitest/browser";
|
|
4
|
-
import { snapshot, createMirror } from "@chromaui/rrweb-snapshot";
|
|
3
|
+
import { commands as commands2 } from "vitest/browser";
|
|
4
|
+
import { snapshot as rrwebSnapshot, createMirror } from "@chromaui/rrweb-snapshot";
|
|
5
5
|
|
|
6
6
|
// src/browser/getCurrentTest.ts
|
|
7
7
|
import * as vitest from "vitest";
|
|
@@ -9,6 +9,9 @@ var hooks = await resolveHooks();
|
|
|
9
9
|
function getCurrentTest() {
|
|
10
10
|
return hooks.getCurrentTest();
|
|
11
11
|
}
|
|
12
|
+
function getCurrentSuite() {
|
|
13
|
+
return hooks.getCurrentSuite();
|
|
14
|
+
}
|
|
12
15
|
async function resolveHooks() {
|
|
13
16
|
if (vitest.TestRunner) {
|
|
14
17
|
return vitest.TestRunner;
|
|
@@ -22,6 +25,17 @@ function isChromium() {
|
|
|
22
25
|
return server.browser === "chromium";
|
|
23
26
|
}
|
|
24
27
|
|
|
28
|
+
// src/browser/telemetry.ts
|
|
29
|
+
import { commands } from "vitest/browser";
|
|
30
|
+
import { inject } from "vitest";
|
|
31
|
+
function trackEvent(event) {
|
|
32
|
+
const enabled = inject("__chromatic_options")?.telemetry.enabled ?? false;
|
|
33
|
+
if (enabled) {
|
|
34
|
+
void commands.__chromatic_telemetry(event).catch(() => {
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
25
39
|
// src/browser/public/takeSnapshot.ts
|
|
26
40
|
async function takeSnapshot(name, options) {
|
|
27
41
|
if (!isChromium()) {
|
|
@@ -29,18 +43,26 @@ async function takeSnapshot(name, options) {
|
|
|
29
43
|
}
|
|
30
44
|
const test = getCurrentTest();
|
|
31
45
|
if (!test) {
|
|
46
|
+
trackEvent({
|
|
47
|
+
eventType: "take_snapshot_invalid_call",
|
|
48
|
+
level: "error",
|
|
49
|
+
payload: { isInsideTest: false, isRegisteredTest: void 0, isAwaited: void 0 }
|
|
50
|
+
});
|
|
32
51
|
throw new TypeError("takeSnapshot() must be called within a test()");
|
|
33
52
|
}
|
|
34
53
|
if (!test.meta.__chromatic_isRegistered) {
|
|
54
|
+
trackEvent({
|
|
55
|
+
eventType: "take_snapshot_invalid_call",
|
|
56
|
+
level: "error",
|
|
57
|
+
payload: { isInsideTest: true, isRegisteredTest: false, isAwaited: void 0 }
|
|
58
|
+
});
|
|
35
59
|
throw new TypeError(
|
|
36
60
|
`takeSnapshot() cannot be called in a test that is not registered for Chromatic plugin.
|
|
37
61
|
Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabled.`
|
|
38
62
|
);
|
|
39
63
|
}
|
|
40
|
-
test.meta.__chromatic_isTakeSnapshotCalled = true;
|
|
41
64
|
const mirror = createMirror();
|
|
42
|
-
const domSnapshot = snapshot(document,
|
|
43
|
-
assert(domSnapshot, "Failed to capture DOM snapshot");
|
|
65
|
+
const domSnapshot = snapshot({ document, mirror, onError: trackSnapshotError("capture") });
|
|
44
66
|
const pseudoClassIds = {};
|
|
45
67
|
for (const className of [":hover", ":focus", ":focus-visible", ":active"]) {
|
|
46
68
|
const elements = document.querySelectorAll(className);
|
|
@@ -48,10 +70,18 @@ Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabl
|
|
|
48
70
|
pseudoClassIds[className] = ids;
|
|
49
71
|
}
|
|
50
72
|
const save = async () => {
|
|
51
|
-
await replaceBlobUrls(domSnapshot);
|
|
52
|
-
await
|
|
73
|
+
await replaceBlobUrls(domSnapshot).catch(trackSnapshotError("replace-blob-urls"));
|
|
74
|
+
await commands2.__chromatic_uploadDOMSnapshot(
|
|
75
|
+
test.id,
|
|
76
|
+
domSnapshot,
|
|
77
|
+
pseudoClassIds,
|
|
78
|
+
name ?? null,
|
|
79
|
+
// Convert undefined to null to avoid https://github.com/vitest-dev/vitest/issues/10864
|
|
80
|
+
options
|
|
81
|
+
).catch(trackSnapshotError("upload"));
|
|
82
|
+
test.meta.__chromatic_isTakeSnapshotCalled = true;
|
|
53
83
|
};
|
|
54
|
-
if (options?.
|
|
84
|
+
if (options?.isAutomaticSnapshot) {
|
|
55
85
|
return await save();
|
|
56
86
|
}
|
|
57
87
|
const error = new Error("takeSnapshot() call was not awaited!");
|
|
@@ -65,6 +95,33 @@ Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabl
|
|
|
65
95
|
test.meta.__chromatic_pendingTakeSnapshots?.splice(index, 1);
|
|
66
96
|
}
|
|
67
97
|
});
|
|
98
|
+
function trackSnapshotError(operation) {
|
|
99
|
+
return function onError(error2) {
|
|
100
|
+
trackEvent({
|
|
101
|
+
eventType: "snapshot_error",
|
|
102
|
+
level: "error",
|
|
103
|
+
payload: {
|
|
104
|
+
operation,
|
|
105
|
+
isAutomaticSnapshot: options?.isAutomaticSnapshot ?? false,
|
|
106
|
+
error: error2 instanceof Error ? error2.stack ?? error2.message : String(error2)
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
throw error2;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function snapshot(options) {
|
|
114
|
+
try {
|
|
115
|
+
const domSnapshot = rrwebSnapshot(options.document, {
|
|
116
|
+
recordCanvas: true,
|
|
117
|
+
mirror: options.mirror
|
|
118
|
+
});
|
|
119
|
+
assert(domSnapshot, "Failed to capture DOM snapshot");
|
|
120
|
+
return domSnapshot;
|
|
121
|
+
} catch (error) {
|
|
122
|
+
options.onError(error);
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
68
125
|
}
|
|
69
126
|
async function replaceBlobUrls(node) {
|
|
70
127
|
if (!("childNodes" in node)) {
|
|
@@ -104,9 +161,11 @@ function configure(options) {
|
|
|
104
161
|
...test.meta.__chromatic_options,
|
|
105
162
|
...options
|
|
106
163
|
};
|
|
107
|
-
return;
|
|
164
|
+
return trackConfigure("test");
|
|
108
165
|
}
|
|
166
|
+
const scope = getCurrentSuite()?.suite ? "suite" : "file";
|
|
109
167
|
beforeAll(({}, suite) => {
|
|
168
|
+
trackConfigure(scope);
|
|
110
169
|
traverseTests(suite);
|
|
111
170
|
function traverseTests(task) {
|
|
112
171
|
if (task.type === "test") {
|
|
@@ -120,25 +179,69 @@ function configure(options) {
|
|
|
120
179
|
task.tasks.forEach(traverseTests);
|
|
121
180
|
}
|
|
122
181
|
});
|
|
182
|
+
function trackConfigure(scope2) {
|
|
183
|
+
trackEvent({
|
|
184
|
+
eventType: "configure_called",
|
|
185
|
+
level: "info",
|
|
186
|
+
payload: {
|
|
187
|
+
options: Object.keys(options),
|
|
188
|
+
scope: scope2
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
123
192
|
}
|
|
124
193
|
|
|
125
194
|
// src/browser/public/waitForIdleNetwork.ts
|
|
126
|
-
import { commands as
|
|
127
|
-
async function waitForIdleNetwork(timeout) {
|
|
195
|
+
import { commands as commands3 } from "vitest/browser";
|
|
196
|
+
async function waitForIdleNetwork(timeout, options) {
|
|
128
197
|
if (!isChromium()) {
|
|
129
198
|
return;
|
|
130
199
|
}
|
|
200
|
+
const isCalledByUser = options?._internal !== true;
|
|
131
201
|
const test = getCurrentTest();
|
|
132
202
|
if (!test) {
|
|
203
|
+
trackEvent({
|
|
204
|
+
eventType: "wait_for_idle_network_invalid_call",
|
|
205
|
+
level: "error",
|
|
206
|
+
payload: {
|
|
207
|
+
isInsideTest: false,
|
|
208
|
+
isRegisteredTest: void 0,
|
|
209
|
+
isCalledByUser
|
|
210
|
+
}
|
|
211
|
+
});
|
|
133
212
|
throw new TypeError("waitForIdleNetwork() must be called within a test()");
|
|
134
213
|
}
|
|
135
214
|
if (!test.meta.__chromatic_isRegistered) {
|
|
215
|
+
trackEvent({
|
|
216
|
+
eventType: "wait_for_idle_network_invalid_call",
|
|
217
|
+
level: "error",
|
|
218
|
+
payload: {
|
|
219
|
+
isInsideTest: true,
|
|
220
|
+
isRegisteredTest: false,
|
|
221
|
+
isCalledByUser
|
|
222
|
+
}
|
|
223
|
+
});
|
|
136
224
|
throw new TypeError(
|
|
137
225
|
`waitForIdleNetwork() cannot be called in a test that is not registered for Chromatic plugin.
|
|
138
226
|
Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabled.`
|
|
139
227
|
);
|
|
140
228
|
}
|
|
141
|
-
|
|
229
|
+
if (isCalledByUser) {
|
|
230
|
+
trackEvent({ eventType: "wait_for_idle_network_called", level: "info", payload: { timeout } });
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
await commands3.__chromatic_waitForIdleNetwork(timeout);
|
|
234
|
+
} catch (error) {
|
|
235
|
+
const isTimeoutError = error instanceof Error && error.message.includes("Timed out waiting for network to be idle");
|
|
236
|
+
if (isTimeoutError) {
|
|
237
|
+
trackEvent({
|
|
238
|
+
eventType: "wait_for_idle_network_timeout",
|
|
239
|
+
level: "error",
|
|
240
|
+
payload: { timeout, isCalledByUser }
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
throw error;
|
|
244
|
+
}
|
|
142
245
|
}
|
|
143
246
|
export {
|
|
144
247
|
configure,
|