@chromatic-com/vitest 1.0.0 → 1.0.2
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/archive-storybook-Bqn7D3Ez.mjs +55 -0
- package/dist/bin/archive-storybook.mjs +47 -0
- package/dist/bin/build-archive-storybook.mjs +65 -0
- package/dist/constants-Dos4OOMt.mjs +68 -0
- package/dist/{index.d.ts → index.d.mts} +48 -42
- package/dist/index.mjs +44 -0
- package/dist/plugin.d.mts +102 -0
- package/dist/plugin.mjs +2604 -0
- package/dist/setupFile.mjs +66 -0
- package/dist/storybook-config/main.mjs +62 -0
- package/dist/storybook-config/manager.mjs +12 -0
- package/dist/storybook-config/preview.mjs +113 -0
- package/dist/telemetry-DOABfuU5.mjs +320 -0
- package/dist/telemetry-sender.mjs +14 -0
- package/dist/waitForIdleNetwork-CEON19Zg.mjs +193 -0
- package/package.json +31 -26
- package/dist/bin/archive-storybook.d.ts +0 -1
- package/dist/bin/archive-storybook.js +0 -368
- package/dist/bin/build-archive-storybook.d.ts +0 -1
- package/dist/bin/build-archive-storybook.js +0 -383
- package/dist/index.js +0 -250
- package/dist/plugin.d.ts +0 -99
- package/dist/plugin.js +0 -2148
- package/dist/setupFile.d.ts +0 -2
- package/dist/setupFile.js +0 -287
- package/dist/storybook-config/main.d.ts +0 -5
- package/dist/storybook-config/main.js +0 -88
- package/dist/storybook-config/manager.d.ts +0 -2
- package/dist/storybook-config/manager.js +0 -14
- package/dist/storybook-config/preview.d.ts +0 -17
- package/dist/storybook-config/preview.js +0 -138
|
@@ -1,383 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
var __require = /* @__PURE__ */ ((x2) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x2, {
|
|
3
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
|
-
}) : x2)(function(x2) {
|
|
5
|
-
if (typeof require !== "undefined")
|
|
6
|
-
return require.apply(this, arguments);
|
|
7
|
-
throw Error('Dynamic require of "' + x2 + '" is not supported');
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
// ../shared/src/archive-storybook/index.ts
|
|
11
|
-
import { spawn } from "child_process";
|
|
12
|
-
import { resolve, dirname } from "path";
|
|
13
|
-
import { readFileSync } from "fs";
|
|
14
|
-
import { createRequire } from "module";
|
|
15
|
-
|
|
16
|
-
// ../shared/src/utils/filePaths.ts
|
|
17
|
-
import { existsSync, mkdirSync } from "fs";
|
|
18
|
-
import path from "path";
|
|
19
|
-
function rootDir() {
|
|
20
|
-
return process.cwd();
|
|
21
|
-
}
|
|
22
|
-
function outputDirOverride() {
|
|
23
|
-
const { CHROMATIC_ARCHIVE_LOCATION } = process.env;
|
|
24
|
-
return CHROMATIC_ARCHIVE_LOCATION;
|
|
25
|
-
}
|
|
26
|
-
function archivesDir(defaultOutputDir) {
|
|
27
|
-
const outputDir = outputDirOverride() || defaultOutputDir;
|
|
28
|
-
return path.resolve(rootDir(), outputDir, "chromatic-archives");
|
|
29
|
-
}
|
|
30
|
-
function checkArchivesDirExists(defaultOutputDir) {
|
|
31
|
-
const dir = archivesDir(defaultOutputDir);
|
|
32
|
-
if (!existsSync(dir)) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
`Chromatic archives directory cannot be found: ${dir}
|
|
35
|
-
|
|
36
|
-
Please make sure that you have run your E2E tests, or have set the CHROMATIC_ARCHIVE_LOCATION env var if the output directory for the tests is not in the standard location.`
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// ../shared/src/archive-storybook/index.ts
|
|
42
|
-
var req = __require.resolve ? __require : createRequire(import.meta.url);
|
|
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
|
-
});
|
|
72
|
-
}
|
|
73
|
-
function binPath() {
|
|
74
|
-
const filename = req.resolve("storybook/package.json");
|
|
75
|
-
const packageJson = JSON.parse(readFileSync(filename, "utf8"));
|
|
76
|
-
return resolve(dirname(filename), packageJson.bin);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// src/bin/build-archive-storybook.ts
|
|
80
|
-
import path2 from "path";
|
|
81
|
-
|
|
82
|
-
// src/constants.ts
|
|
83
|
-
var DEFAULT_OUTPUT_DIR = ".vitest/chromatic";
|
|
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 = "1.0.0";
|
|
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
|
-
|
|
334
|
-
// src/bin/build-archive-storybook.ts
|
|
335
|
-
var args = process.argv.slice(2);
|
|
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();
|
|
340
|
-
try {
|
|
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
|
-
});
|
|
352
|
-
} catch (err) {
|
|
353
|
-
trackEvent2({
|
|
354
|
-
eventType: "storybook_build_completed",
|
|
355
|
-
level: "error",
|
|
356
|
-
payload: { success: false, error: err, chromaticProjectId }
|
|
357
|
-
});
|
|
358
|
-
console.error(err.message);
|
|
359
|
-
process.exitCode = 1;
|
|
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
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
// src/browser/public/takeSnapshot.ts
|
|
2
|
-
import { assert } from "vitest";
|
|
3
|
-
import { commands as commands2 } from "vitest/browser";
|
|
4
|
-
import { snapshot as rrwebSnapshot, createMirror } from "@chromaui/rrweb-snapshot";
|
|
5
|
-
|
|
6
|
-
// src/browser/getCurrentTest.ts
|
|
7
|
-
import * as vitest from "vitest";
|
|
8
|
-
var hooks = await resolveHooks();
|
|
9
|
-
function getCurrentTest() {
|
|
10
|
-
return hooks.getCurrentTest();
|
|
11
|
-
}
|
|
12
|
-
function getCurrentSuite() {
|
|
13
|
-
return hooks.getCurrentSuite();
|
|
14
|
-
}
|
|
15
|
-
async function resolveHooks() {
|
|
16
|
-
if (vitest.TestRunner) {
|
|
17
|
-
return vitest.TestRunner;
|
|
18
|
-
}
|
|
19
|
-
return await import("vitest/suite");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// src/browser/isChromium.ts
|
|
23
|
-
import { server } from "vitest/browser";
|
|
24
|
-
function isChromium() {
|
|
25
|
-
return server.browser === "chromium";
|
|
26
|
-
}
|
|
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
|
-
|
|
39
|
-
// src/browser/public/takeSnapshot.ts
|
|
40
|
-
async function takeSnapshot(name, options) {
|
|
41
|
-
if (!isChromium()) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
const test = getCurrentTest();
|
|
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
|
-
});
|
|
51
|
-
throw new TypeError("takeSnapshot() must be called within a test()");
|
|
52
|
-
}
|
|
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
|
-
});
|
|
59
|
-
throw new TypeError(
|
|
60
|
-
`takeSnapshot() cannot be called in a test that is not registered for Chromatic plugin.
|
|
61
|
-
Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabled.`
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
const mirror = createMirror();
|
|
65
|
-
const domSnapshot = snapshot({ document, mirror, onError: trackSnapshotError("capture") });
|
|
66
|
-
const pseudoClassIds = {};
|
|
67
|
-
for (const className of [":hover", ":focus", ":focus-visible", ":active"]) {
|
|
68
|
-
const elements = document.querySelectorAll(className);
|
|
69
|
-
const ids = Array.from(elements, (el) => mirror.getId(el)).filter((id) => id !== -1);
|
|
70
|
-
pseudoClassIds[className] = ids;
|
|
71
|
-
}
|
|
72
|
-
const save = async () => {
|
|
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;
|
|
83
|
-
};
|
|
84
|
-
if (options?.isAutomaticSnapshot) {
|
|
85
|
-
return await save();
|
|
86
|
-
}
|
|
87
|
-
const error = new Error("takeSnapshot() call was not awaited!");
|
|
88
|
-
Error.captureStackTrace?.(error, takeSnapshot);
|
|
89
|
-
const pendingCall = { promise: save(), error };
|
|
90
|
-
test.meta.__chromatic_pendingTakeSnapshots ||= [];
|
|
91
|
-
test.meta.__chromatic_pendingTakeSnapshots.push(pendingCall);
|
|
92
|
-
await pendingCall.promise.finally(() => {
|
|
93
|
-
const index = test.meta.__chromatic_pendingTakeSnapshots?.indexOf(pendingCall);
|
|
94
|
-
if (index !== void 0 && index !== -1) {
|
|
95
|
-
test.meta.__chromatic_pendingTakeSnapshots?.splice(index, 1);
|
|
96
|
-
}
|
|
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
|
-
}
|
|
125
|
-
}
|
|
126
|
-
async function replaceBlobUrls(node) {
|
|
127
|
-
if (!("childNodes" in node)) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
await Promise.all(
|
|
131
|
-
node.childNodes.map(async (childNode) => {
|
|
132
|
-
if ("tagName" in childNode && childNode.tagName === "img" && typeof childNode.attributes.src === "string" && childNode.attributes.src?.startsWith("blob:")) {
|
|
133
|
-
const base64Url = await toDataURL(childNode.attributes.src);
|
|
134
|
-
childNode.attributes.src = base64Url;
|
|
135
|
-
}
|
|
136
|
-
if ("childNodes" in childNode && childNode.childNodes?.length) {
|
|
137
|
-
await replaceBlobUrls(childNode);
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
async function toDataURL(url) {
|
|
143
|
-
const blob = await fetch(url).then((res) => res.blob());
|
|
144
|
-
return new Promise((resolveFileRead, reject) => {
|
|
145
|
-
const reader = new FileReader();
|
|
146
|
-
reader.onloadend = () => resolveFileRead(reader.result?.toString() || "");
|
|
147
|
-
reader.onerror = reject;
|
|
148
|
-
reader.readAsDataURL(blob);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// src/browser/public/configure.ts
|
|
153
|
-
import { beforeAll } from "vitest";
|
|
154
|
-
function configure(options) {
|
|
155
|
-
if (!isChromium()) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
const test = getCurrentTest();
|
|
159
|
-
if (test) {
|
|
160
|
-
test.meta.__chromatic_options = {
|
|
161
|
-
...test.meta.__chromatic_options,
|
|
162
|
-
...options
|
|
163
|
-
};
|
|
164
|
-
return trackConfigure("test");
|
|
165
|
-
}
|
|
166
|
-
const scope = getCurrentSuite()?.suite ? "suite" : "file";
|
|
167
|
-
beforeAll(({}, suite) => {
|
|
168
|
-
trackConfigure(scope);
|
|
169
|
-
traverseTests(suite);
|
|
170
|
-
function traverseTests(task) {
|
|
171
|
-
if (task.type === "test") {
|
|
172
|
-
const test2 = task;
|
|
173
|
-
test2.meta.__chromatic_options = {
|
|
174
|
-
...test2.meta.__chromatic_options,
|
|
175
|
-
...options
|
|
176
|
-
};
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
task.tasks.forEach(traverseTests);
|
|
180
|
-
}
|
|
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
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// src/browser/public/waitForIdleNetwork.ts
|
|
195
|
-
import { commands as commands3 } from "vitest/browser";
|
|
196
|
-
async function waitForIdleNetwork(timeout, options) {
|
|
197
|
-
if (!isChromium()) {
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
const isCalledByUser = options?._internal !== true;
|
|
201
|
-
const test = getCurrentTest();
|
|
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
|
-
});
|
|
212
|
-
throw new TypeError("waitForIdleNetwork() must be called within a test()");
|
|
213
|
-
}
|
|
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
|
-
});
|
|
224
|
-
throw new TypeError(
|
|
225
|
-
`waitForIdleNetwork() cannot be called in a test that is not registered for Chromatic plugin.
|
|
226
|
-
Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabled.`
|
|
227
|
-
);
|
|
228
|
-
}
|
|
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
|
-
}
|
|
245
|
-
}
|
|
246
|
-
export {
|
|
247
|
-
configure,
|
|
248
|
-
takeSnapshot,
|
|
249
|
-
waitForIdleNetwork
|
|
250
|
-
};
|