@chromatic-com/vitest 0.0.2 → 0.0.3-0093252-20260502090528
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/plugin.d.ts +11 -0
- package/dist/plugin.js +33 -6
- package/dist/setupFile.js +25 -21
- package/package.json +1 -1
package/dist/plugin.d.ts
CHANGED
|
@@ -14,6 +14,12 @@ interface ChromaticConfig {
|
|
|
14
14
|
ignoreSelectors?: string[];
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
interface TitlePathFormatterContext {
|
|
18
|
+
filePath: string;
|
|
19
|
+
testPath: string[];
|
|
20
|
+
projectName?: string;
|
|
21
|
+
defaultTitlePath: string[];
|
|
22
|
+
}
|
|
17
23
|
/**
|
|
18
24
|
* Options for the Chromatic Vitest plugin
|
|
19
25
|
*/
|
|
@@ -40,6 +46,11 @@ interface Options extends ChromaticConfig {
|
|
|
40
46
|
* @default 100
|
|
41
47
|
*/
|
|
42
48
|
idleNetworkInterval?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Format the Storybook title path used for this test's snapshots.
|
|
51
|
+
* By default this is `[projectName?, filePath, ...testPath]`.
|
|
52
|
+
*/
|
|
53
|
+
formatTitlePath?: (context: TitlePathFormatterContext) => string[];
|
|
43
54
|
}
|
|
44
55
|
|
|
45
56
|
/**
|
package/dist/plugin.js
CHANGED
|
@@ -3938,7 +3938,7 @@ async function writeTestResult(e2eTestInfo, domSnapshots, archive, chromaticStor
|
|
|
3938
3938
|
// cypress: https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Spec-files
|
|
3939
3939
|
pathPart.replace(/\.(ts|js|mjs|cjs|tsx|jsx|cjsx|coffee)$/, "").replace(/\.(spec|test|cy)$/, "")
|
|
3940
3940
|
));
|
|
3941
|
-
const title = titlePathWithoutFileExtensions.join("/");
|
|
3941
|
+
const title = titlePathWithoutFileExtensions.join("/").replace(/\/([ ’–—―′¿'`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\]+)\//gi, "$1/");
|
|
3942
3942
|
const finalOutputDir = join(outputDir, "chromatic-archives");
|
|
3943
3943
|
const archiveDir = join(finalOutputDir, "archive");
|
|
3944
3944
|
await ensureDir(archiveDir);
|
|
@@ -4085,6 +4085,7 @@ var NetworkIdleTracker = class _NetworkIdleTracker {
|
|
|
4085
4085
|
|
|
4086
4086
|
// src/node/commands.ts
|
|
4087
4087
|
function createCommands(options) {
|
|
4088
|
+
const { formatTitlePath, ...browserOptions } = options;
|
|
4088
4089
|
const resourceArchivers = /* @__PURE__ */ new Map();
|
|
4089
4090
|
const networkIdleTrackers = /* @__PURE__ */ new Map();
|
|
4090
4091
|
const snapshots = /* @__PURE__ */ new Map();
|
|
@@ -4094,7 +4095,7 @@ function createCommands(options) {
|
|
|
4094
4095
|
* All options must be serializable at this point.
|
|
4095
4096
|
*/
|
|
4096
4097
|
async __chromatic_getOptions() {
|
|
4097
|
-
return
|
|
4098
|
+
return browserOptions;
|
|
4098
4099
|
},
|
|
4099
4100
|
/**
|
|
4100
4101
|
* Store a `@rrweb` generated DOM snapshot for the test.
|
|
@@ -4155,9 +4156,9 @@ function createCommands(options) {
|
|
|
4155
4156
|
};
|
|
4156
4157
|
}
|
|
4157
4158
|
await writeTestResult({
|
|
4158
|
-
outputDir: resolve(context.project.config.root, options.outputDirectory),
|
|
4159
|
+
outputDir: resolve(context.project.vitest.config.root, options.outputDirectory),
|
|
4159
4160
|
pageUrl: context.page.url(),
|
|
4160
|
-
titlePath:
|
|
4161
|
+
titlePath: getTitlePath(entity, formatTitlePath)
|
|
4161
4162
|
}, snapshotBuffers, archive, {
|
|
4162
4163
|
delay: options.delay,
|
|
4163
4164
|
diffIncludeAntiAliasing: options.diffIncludeAntiAliasing,
|
|
@@ -4209,6 +4210,20 @@ function createCommands(options) {
|
|
|
4209
4210
|
__name(onTestCleanup, "onTestCleanup");
|
|
4210
4211
|
}
|
|
4211
4212
|
__name(createCommands, "createCommands");
|
|
4213
|
+
function getTitlePath(test, formatTitlePath) {
|
|
4214
|
+
const defaultTitlePath = getNames(test);
|
|
4215
|
+
if (!formatTitlePath)
|
|
4216
|
+
return defaultTitlePath;
|
|
4217
|
+
const projectName = test.project.vitest.projects.length > 1 && test.project.name ? test.project.name : void 0;
|
|
4218
|
+
const projectOffset = projectName ? 1 : 0;
|
|
4219
|
+
return formatTitlePath({
|
|
4220
|
+
filePath: defaultTitlePath[projectOffset] ?? "",
|
|
4221
|
+
testPath: defaultTitlePath.slice(projectOffset + 1),
|
|
4222
|
+
projectName,
|
|
4223
|
+
defaultTitlePath
|
|
4224
|
+
});
|
|
4225
|
+
}
|
|
4226
|
+
__name(getTitlePath, "getTitlePath");
|
|
4212
4227
|
function getNames(test) {
|
|
4213
4228
|
const names = [
|
|
4214
4229
|
test.name
|
|
@@ -4223,6 +4238,10 @@ function getNames(test) {
|
|
|
4223
4238
|
if (current.type === "module") {
|
|
4224
4239
|
names.unshift(current.relativeModuleId);
|
|
4225
4240
|
}
|
|
4241
|
+
const hasManyProjects = test.project.vitest.projects.length > 1;
|
|
4242
|
+
if (hasManyProjects && test.project.name) {
|
|
4243
|
+
names.unshift(test.project.name);
|
|
4244
|
+
}
|
|
4226
4245
|
return names;
|
|
4227
4246
|
}
|
|
4228
4247
|
__name(getNames, "getNames");
|
|
@@ -4261,10 +4280,18 @@ function chromaticPlugin(userOptions = {}) {
|
|
|
4261
4280
|
},
|
|
4262
4281
|
configureVitest(context) {
|
|
4263
4282
|
const project = context.project;
|
|
4283
|
+
const sequence = context.vitest.config.sequence;
|
|
4264
4284
|
if (!project.config.browser.enabled) {
|
|
4265
4285
|
return;
|
|
4266
4286
|
}
|
|
4267
|
-
|
|
4287
|
+
if (sequence.hooks === "stack") {
|
|
4288
|
+
project.config.setupFiles.push(setupFile);
|
|
4289
|
+
} else if (sequence.hooks === "list") {
|
|
4290
|
+
project.config.setupFiles.unshift(setupFile);
|
|
4291
|
+
} else {
|
|
4292
|
+
project.config.setupFiles.push(setupFile);
|
|
4293
|
+
context.vitest.logger.warn(colors.bgYellow(colors.black(" chromatic ")), colors.yellow(`Using { sequence.hooks: 'parallel' } may cause unstable snapshots. Please set 'sequence.hooks' to 'list' or 'stack' to ensure reliable snapshot ordering.`));
|
|
4294
|
+
}
|
|
4268
4295
|
if (options.tags && context.vitest.version.startsWith("4.0")) {
|
|
4269
4296
|
context.vitest.logger.warn(colors.bgYellow(colors.black(" chromatic ")), colors.yellow(`Tags cannot be used with Vitest ${context.vitest.version}. Please upgrade to Vitest 4.1 or later to use this feature.`));
|
|
4270
4297
|
}
|
|
@@ -4288,7 +4315,7 @@ function chromaticPlugin(userOptions = {}) {
|
|
|
4288
4315
|
await project.browser?.triggerCommand("__chromatic_reset", {});
|
|
4289
4316
|
});
|
|
4290
4317
|
function clean() {
|
|
4291
|
-
rmSync(resolve2(project.config.root, options.outputDirectory, "chromatic-archives"), {
|
|
4318
|
+
rmSync(resolve2(project.vitest.config.root, options.outputDirectory, "chromatic-archives"), {
|
|
4292
4319
|
recursive: true,
|
|
4293
4320
|
force: true
|
|
4294
4321
|
});
|
package/dist/setupFile.js
CHANGED
|
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
4
|
// src/browser/setupFile.ts
|
|
5
|
-
import { beforeEach } from "vitest";
|
|
5
|
+
import { afterEach, beforeEach } from "vitest";
|
|
6
6
|
import { commands as commands3 } from "vitest/browser";
|
|
7
7
|
|
|
8
8
|
// src/browser/public/takeSnapshot.ts
|
|
@@ -111,33 +111,37 @@ beforeEach(async ({ task }) => {
|
|
|
111
111
|
const hasMatchingTag = task.tags?.some((tag) => options.tags?.includes(tag));
|
|
112
112
|
if (!hasMatchingTag) {
|
|
113
113
|
task.meta.__chromatic_isRegistered = false;
|
|
114
|
-
return
|
|
114
|
+
return;
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
117
|
task.meta.__chromatic_isRegistered = true;
|
|
118
118
|
task.meta.__chromatic_isTakeSnapshotCalled = false;
|
|
119
119
|
task.meta.__chromatic_autoSnapshot ??= !options.disableAutoSnapshot;
|
|
120
120
|
await commands3.__chromatic_interceptFetch(task.id);
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
if (autoSnapshot) {
|
|
134
|
-
await takeSnapshot(void 0, {
|
|
135
|
-
ignoreUnawaited: true
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
await commands3.__chromatic_writeTestResult(task.id);
|
|
121
|
+
});
|
|
122
|
+
afterEach(async ({ task }) => {
|
|
123
|
+
const { __chromatic_autoSnapshot: autoSnapshot, __chromatic_isTakeSnapshotCalled: isTakeSnapshotCalled, __chromatic_pendingTakeSnapshots: pendingTakeSnapshots, __chromatic_isRegistered: isRegistered } = task.meta;
|
|
124
|
+
if (!isRegistered) {
|
|
125
|
+
return cleanup();
|
|
126
|
+
}
|
|
127
|
+
if (pendingTakeSnapshots?.length) {
|
|
128
|
+
throw new AggregateError(pendingTakeSnapshots.map((call) => call.error), `${pendingTakeSnapshots.length} unawaited takeSnapshot() call(s)`);
|
|
129
|
+
}
|
|
130
|
+
if (!autoSnapshot && !isTakeSnapshotCalled) {
|
|
131
|
+
await commands3.__chromatic_stopWithoutSnapshots(task.id);
|
|
139
132
|
return cleanup();
|
|
140
|
-
}
|
|
133
|
+
}
|
|
134
|
+
const options = await commands3.__chromatic_getOptions();
|
|
135
|
+
if (options.resourceArchiveTimeout !== 0) {
|
|
136
|
+
await waitForIdleNetwork(options.resourceArchiveTimeout);
|
|
137
|
+
}
|
|
138
|
+
if (autoSnapshot) {
|
|
139
|
+
await takeSnapshot(void 0, {
|
|
140
|
+
ignoreUnawaited: true
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
await commands3.__chromatic_writeTestResult(task.id);
|
|
144
|
+
return cleanup();
|
|
141
145
|
function cleanup() {
|
|
142
146
|
task.meta.__chromatic_autoSnapshot = void 0;
|
|
143
147
|
task.meta.__chromatic_isTakeSnapshotCalled = void 0;
|