@cappa/plugin-storybook 0.1.3 → 0.2.1
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/.turbo/turbo-build.log +13 -13
- package/CHANGELOG.md +25 -0
- package/LICENSE +21 -0
- package/README.md +1 -91
- package/dist/index.d.mts +32 -1
- package/dist/index.d.ts +32 -1
- package/dist/index.js +243 -145
- package/dist/index.mjs +243 -145
- package/dist/preview.mjs +5 -3
- package/package.json +8 -8
- package/src/cappa/plugin.ts +287 -183
- package/src/cappa/storybook-url.test.ts +43 -0
- package/src/cappa/storybook-url.ts +55 -0
- package/src/index.ts +2 -0
- package/src/storybook/addon/decorator.ts +6 -4
- package/src/storybook/addon/preview.ts +1 -0
- package/src/storybook/util.ts +1 -1
- package/src/types.ts +29 -4
- package/tsconfig.json +13 -0
- package/src/tsconfig.json +0 -7
package/dist/index.js
CHANGED
|
@@ -37,7 +37,42 @@ module.exports = __toCommonJS(index_exports);
|
|
|
37
37
|
|
|
38
38
|
// src/cappa/plugin.ts
|
|
39
39
|
var import_logger = require("@cappa/logger");
|
|
40
|
-
|
|
40
|
+
|
|
41
|
+
// src/cappa/storybook-url.ts
|
|
42
|
+
function encodePairs(obj) {
|
|
43
|
+
if (!obj || !Object.keys(obj).length) return "";
|
|
44
|
+
return Object.entries(obj).filter(([, v]) => v !== void 0).map(([k, v]) => {
|
|
45
|
+
const val = typeof v === "string" ? encodeURIComponent(v) : typeof v === "number" || typeof v === "boolean" ? String(v) : encodeURIComponent(JSON.stringify(v));
|
|
46
|
+
return `${encodeURIComponent(k)}:${val}`;
|
|
47
|
+
}).join(";");
|
|
48
|
+
}
|
|
49
|
+
function buildStorybookIframeUrl({
|
|
50
|
+
baseUrl,
|
|
51
|
+
storyId,
|
|
52
|
+
viewMode = "story",
|
|
53
|
+
args,
|
|
54
|
+
globals,
|
|
55
|
+
query,
|
|
56
|
+
fullscreen = true,
|
|
57
|
+
singleStory = true
|
|
58
|
+
}) {
|
|
59
|
+
const u = new URL("/iframe.html", baseUrl);
|
|
60
|
+
u.searchParams.set("id", storyId);
|
|
61
|
+
u.searchParams.set("viewMode", viewMode);
|
|
62
|
+
const argPairs = encodePairs(args);
|
|
63
|
+
if (argPairs) u.searchParams.set("args", argPairs);
|
|
64
|
+
const globalPairs = encodePairs(globals);
|
|
65
|
+
if (globalPairs) u.searchParams.set("globals", globalPairs);
|
|
66
|
+
if (fullscreen) u.searchParams.set("full", "1");
|
|
67
|
+
if (singleStory) u.searchParams.set("singleStory", "true");
|
|
68
|
+
if (query) {
|
|
69
|
+
for (const [k, v] of Object.entries(query)) {
|
|
70
|
+
if (v === void 0) continue;
|
|
71
|
+
u.searchParams.set(k, String(v));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return u.toString();
|
|
75
|
+
}
|
|
41
76
|
|
|
42
77
|
// src/cappa/util.ts
|
|
43
78
|
var freezeUI = async (page) => {
|
|
@@ -114,6 +149,33 @@ var buildFilename = (story) => {
|
|
|
114
149
|
};
|
|
115
150
|
|
|
116
151
|
// src/cappa/plugin.ts
|
|
152
|
+
async function waitForPlayFunctionCompletion(page, storyName) {
|
|
153
|
+
const logger = (0, import_logger.getLogger)();
|
|
154
|
+
try {
|
|
155
|
+
const hasPlayFunction = await page.evaluate(
|
|
156
|
+
() => window.__cappa_has_play_function === true
|
|
157
|
+
);
|
|
158
|
+
if (!hasPlayFunction) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
logger.debug(
|
|
162
|
+
`Waiting for play function to complete for story: ${storyName}`
|
|
163
|
+
);
|
|
164
|
+
await page.waitForFunction(() => window.playAfterEach === true, {
|
|
165
|
+
timeout: 1e4
|
|
166
|
+
});
|
|
167
|
+
logger.debug(`Play function completed for story: ${storyName}`);
|
|
168
|
+
await page.evaluate(() => {
|
|
169
|
+
window.playAfterEach = false;
|
|
170
|
+
window.__cappa_has_play_function = false;
|
|
171
|
+
});
|
|
172
|
+
} catch {
|
|
173
|
+
await page.evaluate(() => {
|
|
174
|
+
window.playAfterEach = false;
|
|
175
|
+
window.__cappa_has_play_function = false;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
117
179
|
function createLatch() {
|
|
118
180
|
let resolve;
|
|
119
181
|
const p = new Promise((r) => resolve = r);
|
|
@@ -123,8 +185,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
123
185
|
return {
|
|
124
186
|
name: "StorybookPlugin",
|
|
125
187
|
description: "Takes screenshots of Storybook stories with configurable options",
|
|
126
|
-
|
|
127
|
-
execute: async (screenshotTool) => {
|
|
188
|
+
discover: async () => {
|
|
128
189
|
if (!options) {
|
|
129
190
|
throw new Error("Storybook plugin options are required");
|
|
130
191
|
}
|
|
@@ -135,7 +196,8 @@ var cappaPluginStorybook = (options) => {
|
|
|
135
196
|
const {
|
|
136
197
|
storybookUrl,
|
|
137
198
|
includeStories = [],
|
|
138
|
-
excludeStories = []
|
|
199
|
+
excludeStories = [],
|
|
200
|
+
storybook: defaultStorybookOptions
|
|
139
201
|
} = options;
|
|
140
202
|
const storiesUrl = `${storybookUrl}/index.json`;
|
|
141
203
|
logger.debug(`Fetching stories from: ${storiesUrl}`);
|
|
@@ -145,159 +207,195 @@ var cappaPluginStorybook = (options) => {
|
|
|
145
207
|
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
146
208
|
);
|
|
147
209
|
}
|
|
210
|
+
const data = await response.json();
|
|
211
|
+
const stories = Object.values(data.entries);
|
|
212
|
+
let filteredStories = stories;
|
|
213
|
+
if (includeStories.length > 0) {
|
|
214
|
+
filteredStories = filteredStories.filter(
|
|
215
|
+
(story) => includeStories.some(
|
|
216
|
+
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
217
|
+
)
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
if (excludeStories.length > 0) {
|
|
221
|
+
filteredStories = filteredStories.filter(
|
|
222
|
+
(story) => !excludeStories.some(
|
|
223
|
+
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
224
|
+
)
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
filteredStories = filteredStories.filter(
|
|
228
|
+
(story) => story.type === "story"
|
|
229
|
+
);
|
|
230
|
+
logger.start(`Found ${filteredStories.length} stories to screenshot...`);
|
|
231
|
+
return filteredStories.map((story) => ({
|
|
232
|
+
id: story.id,
|
|
233
|
+
url: buildStorybookIframeUrl({
|
|
234
|
+
baseUrl: storybookUrl,
|
|
235
|
+
storyId: story.id,
|
|
236
|
+
viewMode: defaultStorybookOptions?.viewMode,
|
|
237
|
+
args: defaultStorybookOptions?.args,
|
|
238
|
+
globals: defaultStorybookOptions?.globals,
|
|
239
|
+
query: defaultStorybookOptions?.query,
|
|
240
|
+
fullscreen: defaultStorybookOptions?.fullscreen,
|
|
241
|
+
singleStory: defaultStorybookOptions?.singleStory
|
|
242
|
+
}),
|
|
243
|
+
data: {
|
|
244
|
+
story
|
|
245
|
+
}
|
|
246
|
+
}));
|
|
247
|
+
},
|
|
248
|
+
initPage: async () => {
|
|
249
|
+
return {
|
|
250
|
+
latchMap: /* @__PURE__ */ new Map()
|
|
251
|
+
};
|
|
252
|
+
},
|
|
253
|
+
// Phase 2: Execute single story
|
|
254
|
+
execute: async (task, page, screenshotTool, context) => {
|
|
255
|
+
const { url, data } = task;
|
|
256
|
+
const { story } = data;
|
|
257
|
+
const logger = (0, import_logger.getLogger)();
|
|
258
|
+
const { storybook: defaultStorybookOptions } = options || {};
|
|
148
259
|
try {
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
)
|
|
260
|
+
const currentLatch = createLatch();
|
|
261
|
+
context.latchMap.set(story.id, currentLatch);
|
|
262
|
+
if (await page.evaluate(() => {
|
|
263
|
+
return window.__cappa_parameters === void 0;
|
|
264
|
+
})) {
|
|
265
|
+
await page.exposeFunction(
|
|
266
|
+
"__cappa_parameters",
|
|
267
|
+
async (id, payload) => {
|
|
268
|
+
context.latchMap.get(id)?.resolve?.(payload);
|
|
269
|
+
}
|
|
157
270
|
);
|
|
158
271
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
272
|
+
page.on("console", (message) => {
|
|
273
|
+
logger.debug("console", message.text());
|
|
274
|
+
});
|
|
275
|
+
page.on("pageerror", (error) => {
|
|
276
|
+
logger.debug("pageerror", error);
|
|
277
|
+
});
|
|
278
|
+
await page.goto(url);
|
|
279
|
+
const storyParameters = await context.latchMap.get(story.id)?.p;
|
|
280
|
+
context.latchMap.delete(story.id);
|
|
281
|
+
const variantParameters = storyParameters?.variants ?? [];
|
|
282
|
+
if (storyParameters?.skip) {
|
|
283
|
+
logger.warn(`Skipping story ${story.title} - ${story.name}`);
|
|
284
|
+
return {
|
|
285
|
+
storyId: story.id,
|
|
286
|
+
storyName: `${story.title} - ${story.name}`,
|
|
287
|
+
skipped: true
|
|
288
|
+
};
|
|
165
289
|
}
|
|
166
|
-
|
|
167
|
-
(
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
);
|
|
172
|
-
|
|
173
|
-
await
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
"__cappa_parameters",
|
|
177
|
-
async (payload) => {
|
|
178
|
-
currentLatch.resolve(payload);
|
|
179
|
-
}
|
|
290
|
+
if (storyParameters && Object.keys(storyParameters).length > 0) {
|
|
291
|
+
logger.debug("Taking screenshot with options", storyParameters);
|
|
292
|
+
}
|
|
293
|
+
const resolvedViewport = storyParameters?.viewport ?? screenshotTool.viewport;
|
|
294
|
+
await page.setViewportSize(resolvedViewport);
|
|
295
|
+
await freezeUI(page);
|
|
296
|
+
await waitForVisualIdle(page);
|
|
297
|
+
await waitForPlayFunctionCompletion(
|
|
298
|
+
page,
|
|
299
|
+
`${story.title} - ${story.name}`
|
|
180
300
|
);
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
301
|
+
const toLocatorMask = (mask) => mask?.map((selector) => page.locator(selector));
|
|
302
|
+
const filename = buildFilename(story);
|
|
303
|
+
const expectedExists = screenshotTool.filesystem.hasExpectedFile(filename);
|
|
304
|
+
if (!expectedExists) {
|
|
305
|
+
logger.info(
|
|
306
|
+
`Expected image not found for story ${story.title} - ${story.name}, taking screenshot`
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
const variantsWithUrls = variantParameters.map(
|
|
310
|
+
(variant) => {
|
|
311
|
+
const variantFilename = variant.filename || screenshotTool.getVariantFilename(filename, {
|
|
312
|
+
id: variant.id,
|
|
313
|
+
label: variant.label,
|
|
314
|
+
filename: variant.filename
|
|
193
315
|
});
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
continue;
|
|
205
|
-
}
|
|
206
|
-
if (options2 && Object.keys(options2).length > 0) {
|
|
207
|
-
logger.debug("Taking screenshot with options", options2);
|
|
208
|
-
}
|
|
209
|
-
await freezeUI(page);
|
|
210
|
-
await waitForVisualIdle(page);
|
|
211
|
-
if (options2.delay) {
|
|
212
|
-
await page.waitForTimeout(options2.delay);
|
|
213
|
-
}
|
|
214
|
-
const filename = buildFilename(story);
|
|
215
|
-
const expectedExists = screenshotTool.hasExpectedImage(filename);
|
|
216
|
-
let actualFilepath;
|
|
217
|
-
if (expectedExists) {
|
|
218
|
-
const { screenshotPath, comparisonResult } = await screenshotTool.takeScreenshotWithComparison(
|
|
219
|
-
page,
|
|
220
|
-
filename,
|
|
221
|
-
screenshotTool.getExpectedImageBuffer(filename),
|
|
222
|
-
{
|
|
223
|
-
fullPage: options2.fullPage,
|
|
224
|
-
mask: options2.mask?.map(
|
|
225
|
-
(selector) => page.locator(selector)
|
|
226
|
-
),
|
|
227
|
-
omitBackground: options2.omitBackground,
|
|
228
|
-
saveDiffImage: true
|
|
229
|
-
}
|
|
230
|
-
);
|
|
231
|
-
actualFilepath = screenshotPath;
|
|
232
|
-
results.push({
|
|
316
|
+
const mergedArgs = {
|
|
317
|
+
...defaultStorybookOptions?.args,
|
|
318
|
+
...variant.options?.args
|
|
319
|
+
};
|
|
320
|
+
return {
|
|
321
|
+
id: variant.id,
|
|
322
|
+
label: variant.label,
|
|
323
|
+
filename: variantFilename,
|
|
324
|
+
url: buildStorybookIframeUrl({
|
|
325
|
+
baseUrl: options?.storybookUrl || "",
|
|
233
326
|
storyId: story.id,
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
327
|
+
viewMode: defaultStorybookOptions?.viewMode,
|
|
328
|
+
args: mergedArgs,
|
|
329
|
+
globals: defaultStorybookOptions?.globals,
|
|
330
|
+
query: defaultStorybookOptions?.query,
|
|
331
|
+
fullscreen: defaultStorybookOptions?.fullscreen,
|
|
332
|
+
singleStory: defaultStorybookOptions?.singleStory
|
|
333
|
+
}),
|
|
334
|
+
options: variant.options ? {
|
|
335
|
+
...variant.options,
|
|
336
|
+
mask: toLocatorMask(variant.options.mask)
|
|
337
|
+
} : void 0
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
);
|
|
341
|
+
const baseOptions = {
|
|
342
|
+
fullPage: storyParameters?.fullPage,
|
|
343
|
+
delay: storyParameters?.delay,
|
|
344
|
+
omitBackground: storyParameters?.omitBackground,
|
|
345
|
+
skip: storyParameters?.skip,
|
|
346
|
+
viewport: storyParameters?.viewport ?? screenshotTool.viewport,
|
|
347
|
+
mask: toLocatorMask(storyParameters?.mask)
|
|
348
|
+
};
|
|
349
|
+
const variantFilenameMap = /* @__PURE__ */ new Map();
|
|
350
|
+
for (const variant of variantsWithUrls) {
|
|
351
|
+
const variantFilename = variant.filename;
|
|
352
|
+
if (!variantFilename) continue;
|
|
353
|
+
variantFilenameMap.set(variant.id, variantFilename);
|
|
354
|
+
if (!screenshotTool.filesystem.hasExpectedFile(variantFilename)) {
|
|
355
|
+
logger.info(
|
|
356
|
+
`Expected image not found for variant ${story.title} - ${story.name} [${variant.label ?? variant.id}], taking screenshot`
|
|
262
357
|
);
|
|
263
|
-
results.push({
|
|
264
|
-
storyId: story.id,
|
|
265
|
-
storyName: `${story.title} - ${story.name}`,
|
|
266
|
-
error: error?.message ?? "Unknown error"
|
|
267
|
-
});
|
|
268
358
|
}
|
|
269
359
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
`Skipped stories:
|
|
279
|
-
${results.filter((r) => r.skipped).map((r) => `${r.storyName}`).join("\n")}`
|
|
280
|
-
);
|
|
281
|
-
}
|
|
282
|
-
const skipped = results.filter((r) => r.skipped);
|
|
283
|
-
const failed = results.filter(
|
|
284
|
-
(r) => r.error || !r.skipped && !r.success
|
|
360
|
+
const variantExtrasEntries = variantsWithUrls.map(
|
|
361
|
+
(variant) => [
|
|
362
|
+
variant.id,
|
|
363
|
+
{
|
|
364
|
+
saveDiffImage: true,
|
|
365
|
+
diffImageFilename: variant.filename
|
|
366
|
+
}
|
|
367
|
+
]
|
|
285
368
|
);
|
|
286
|
-
const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
369
|
+
const captureExtras = {
|
|
370
|
+
saveDiffImage: true,
|
|
371
|
+
diffImageFilename: filename,
|
|
372
|
+
variants: variantExtrasEntries.length ? Object.fromEntries(variantExtrasEntries) : void 0
|
|
373
|
+
};
|
|
374
|
+
const captureResult = await screenshotTool.captureWithVariants(
|
|
375
|
+
page,
|
|
376
|
+
filename,
|
|
377
|
+
url,
|
|
378
|
+
baseOptions,
|
|
379
|
+
variantsWithUrls,
|
|
380
|
+
captureExtras
|
|
381
|
+
);
|
|
382
|
+
return {
|
|
383
|
+
storyId: story.id,
|
|
384
|
+
storyName: `${story.title} - ${story.name}`,
|
|
385
|
+
filepath: captureResult.base.filepath,
|
|
386
|
+
success: captureResult.base.skipped ? void 0 : captureResult.base.comparisonResult ? captureResult.base.comparisonResult.passed : Boolean(captureResult.base.filepath),
|
|
387
|
+
skipped: captureResult.base.skipped
|
|
388
|
+
};
|
|
298
389
|
} catch (error) {
|
|
299
|
-
logger.error(
|
|
300
|
-
|
|
390
|
+
logger.error(
|
|
391
|
+
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
392
|
+
error.message
|
|
393
|
+
);
|
|
394
|
+
return {
|
|
395
|
+
storyId: story.id,
|
|
396
|
+
storyName: `${story.title} - ${story.name}`,
|
|
397
|
+
error: error?.message ?? "Unknown error"
|
|
398
|
+
};
|
|
301
399
|
}
|
|
302
400
|
}
|
|
303
401
|
};
|