@storysnap/cli 2.1.0 → 2.2.0
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/api/client.d.ts +18 -1
- package/dist/api/client.js +2 -1
- package/dist/cli/commands/test.js +103 -59
- package/dist/cli/commands/update.js +55 -30
- package/dist/cli/environment-warning.d.ts +17 -0
- package/dist/cli/environment-warning.js +42 -0
- package/dist/cli/index.js +5 -1
- package/dist/cli/select-stories.d.ts +6 -1
- package/dist/cli/select-stories.js +16 -0
- package/dist/cli/verdict.d.ts +17 -0
- package/dist/cli/verdict.js +22 -0
- package/dist/config/schema.d.ts +11 -0
- package/dist/config/schema.js +18 -0
- package/dist/core/capture-environment.d.ts +19 -0
- package/dist/core/capture-environment.js +102 -0
- package/dist/core/compare.d.ts +3 -1
- package/dist/core/compare.js +30 -10
- package/dist/core/hash.d.ts +5 -1
- package/dist/core/hash.js +7 -2
- package/dist/core/modes.d.ts +50 -0
- package/dist/core/modes.js +124 -0
- package/dist/core/screenshot.d.ts +91 -4
- package/dist/core/screenshot.js +266 -47
- package/dist/core/story-parameters.d.ts +48 -0
- package/dist/core/story-parameters.js +122 -0
- package/dist/core/story-render.d.ts +31 -0
- package/dist/core/story-render.js +75 -0
- package/dist/core/storybook.d.ts +16 -2
- package/dist/core/storybook.js +18 -6
- package/dist/core/viewports.d.ts +14 -0
- package/dist/core/viewports.js +27 -0
- package/package.json +9 -10
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Page } from 'playwright';
|
|
2
|
+
/** What went wrong while Storybook rendered a story, as its preview reported it. */
|
|
3
|
+
export interface StoryRenderErrors {
|
|
4
|
+
/** The story's play function threw. */
|
|
5
|
+
play?: string;
|
|
6
|
+
/** Rendering the story threw. */
|
|
7
|
+
render?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Records the exceptions Storybook reports for the story on this page. Has to
|
|
11
|
+
* be installed before the page loads: the events are emitted once, during
|
|
12
|
+
* the render, on the preview's channel.
|
|
13
|
+
*
|
|
14
|
+
* Storybook 9 and later report a failed play function through this event
|
|
15
|
+
* only - the render still ends `finished` and nothing shows on the page - so
|
|
16
|
+
* listening is the one way to tell it apart from a play that succeeded.
|
|
17
|
+
*/
|
|
18
|
+
export declare function watchStoryErrors(page: Page): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Waits until Storybook has finished rendering the story, play function
|
|
21
|
+
* included, so the capture shows its end state rather than whatever the play
|
|
22
|
+
* function had got to. Throws if the play function or the render threw, so
|
|
23
|
+
* the story errors instead of being captured half-done.
|
|
24
|
+
*
|
|
25
|
+
* A page with no Storybook preview - a static stand-in - has nothing to wait
|
|
26
|
+
* for and passes straight through.
|
|
27
|
+
*/
|
|
28
|
+
export declare function waitForStoryToSettle(page: Page, timeoutMs: number): Promise<void>;
|
|
29
|
+
/** The error a story's render ended in, if any, as one the capture fails with. */
|
|
30
|
+
export declare function throwForRenderErrors(errors: StoryRenderErrors, phase: string | undefined): void;
|
|
31
|
+
//# sourceMappingURL=story-render.d.ts.map
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storybook render phases after which the story will not change by itself:
|
|
3
|
+
* `finished` from Storybook 8, `completed` in 7, and `errored`. The phases
|
|
4
|
+
* before them include `playing`, while a play function is still running.
|
|
5
|
+
*/
|
|
6
|
+
const SETTLED_PHASES = ['finished', 'completed', 'errored'];
|
|
7
|
+
/**
|
|
8
|
+
* Records the exceptions Storybook reports for the story on this page. Has to
|
|
9
|
+
* be installed before the page loads: the events are emitted once, during
|
|
10
|
+
* the render, on the preview's channel.
|
|
11
|
+
*
|
|
12
|
+
* Storybook 9 and later report a failed play function through this event
|
|
13
|
+
* only - the render still ends `finished` and nothing shows on the page - so
|
|
14
|
+
* listening is the one way to tell it apart from a play that succeeded.
|
|
15
|
+
*/
|
|
16
|
+
export async function watchStoryErrors(page) {
|
|
17
|
+
await page.addInitScript(() => {
|
|
18
|
+
const scope = globalThis;
|
|
19
|
+
const errors = {};
|
|
20
|
+
scope.__storysnapErrors = errors;
|
|
21
|
+
const describe = (error) => (error && typeof error === 'object' && 'message' in error ? String(error.message) : String(error)) ||
|
|
22
|
+
'Unknown error';
|
|
23
|
+
const timer = setInterval(() => {
|
|
24
|
+
const channel = scope.__STORYBOOK_ADDONS_CHANNEL__;
|
|
25
|
+
if (!channel) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
clearInterval(timer);
|
|
29
|
+
const emit = channel.emit.bind(channel);
|
|
30
|
+
channel.emit = (event, ...args) => {
|
|
31
|
+
if (event === 'playFunctionThrewException') {
|
|
32
|
+
errors.play ??= describe(args[0]);
|
|
33
|
+
}
|
|
34
|
+
else if (event === 'storyThrewException') {
|
|
35
|
+
errors.render ??= describe(args[0]);
|
|
36
|
+
}
|
|
37
|
+
return emit(event, ...args);
|
|
38
|
+
};
|
|
39
|
+
}, 1);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Waits until Storybook has finished rendering the story, play function
|
|
44
|
+
* included, so the capture shows its end state rather than whatever the play
|
|
45
|
+
* function had got to. Throws if the play function or the render threw, so
|
|
46
|
+
* the story errors instead of being captured half-done.
|
|
47
|
+
*
|
|
48
|
+
* A page with no Storybook preview - a static stand-in - has nothing to wait
|
|
49
|
+
* for and passes straight through.
|
|
50
|
+
*/
|
|
51
|
+
export async function waitForStoryToSettle(page, timeoutMs) {
|
|
52
|
+
await page.waitForFunction((settled) => {
|
|
53
|
+
const scope = globalThis;
|
|
54
|
+
const preview = scope.__STORYBOOK_PREVIEW__;
|
|
55
|
+
return !preview || settled.includes(preview.currentRender?.phase ?? '');
|
|
56
|
+
}, SETTLED_PHASES, { timeout: timeoutMs, polling: 50 });
|
|
57
|
+
const { errors, phase } = await page.evaluate(() => {
|
|
58
|
+
const scope = globalThis;
|
|
59
|
+
return { errors: scope.__storysnapErrors ?? {}, phase: scope.__STORYBOOK_PREVIEW__?.currentRender?.phase };
|
|
60
|
+
});
|
|
61
|
+
throwForRenderErrors(errors, phase);
|
|
62
|
+
}
|
|
63
|
+
/** The error a story's render ended in, if any, as one the capture fails with. */
|
|
64
|
+
export function throwForRenderErrors(errors, phase) {
|
|
65
|
+
if (errors.play) {
|
|
66
|
+
throw new Error(`Play function failed: ${errors.play}`);
|
|
67
|
+
}
|
|
68
|
+
if (errors.render) {
|
|
69
|
+
throw new Error(`Story failed to render: ${errors.render}`);
|
|
70
|
+
}
|
|
71
|
+
if (phase === 'errored') {
|
|
72
|
+
throw new Error('Story failed to render');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=story-render.js.map
|
package/dist/core/storybook.d.ts
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
|
-
import type { Story, StorySnapConfig, Browser } from '@storysnap/shared';
|
|
1
|
+
import type { Story, StorySnapConfig, Browser, Viewport } from '@storysnap/shared';
|
|
2
2
|
export declare function discoverStories(config: StorySnapConfig): Promise<Story[]>;
|
|
3
3
|
export declare function filterStories(stories: Story[], include?: string[], exclude?: string[]): Story[];
|
|
4
4
|
export declare function getStoryUrl(baseUrl: string, storyId: string): string;
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* A local baseline's file name: the story ID made filename-safe, then the
|
|
7
|
+
* browser, then the viewport. The viewport is only given for a capture at a
|
|
8
|
+
* viewport other than the configured `viewport`, so the baselines written
|
|
9
|
+
* before stories could have several keep their names.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getStoryKey(storyId: string, browser?: Browser, viewport?: Viewport, mode?: string): string;
|
|
12
|
+
/** The local baseline file name for a capture, given the configured viewport. */
|
|
13
|
+
export declare function getCaptureKey(capture: {
|
|
14
|
+
storyId: string;
|
|
15
|
+
browser: Browser;
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
mode?: string;
|
|
19
|
+
}, configured: Viewport | undefined): string;
|
|
6
20
|
//# sourceMappingURL=storybook.d.ts.map
|
package/dist/core/storybook.js
CHANGED
|
@@ -113,12 +113,24 @@ export function getStoryUrl(baseUrl, storyId) {
|
|
|
113
113
|
const cleanBaseUrl = baseUrl.replace(/\/$/, '');
|
|
114
114
|
return `${cleanBaseUrl}/iframe.html?id=${encodeURIComponent(storyId)}&viewMode=story`;
|
|
115
115
|
}
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
/**
|
|
117
|
+
* A local baseline's file name: the story ID made filename-safe, then the
|
|
118
|
+
* browser, then the viewport. The viewport is only given for a capture at a
|
|
119
|
+
* viewport other than the configured `viewport`, so the baselines written
|
|
120
|
+
* before stories could have several keep their names.
|
|
121
|
+
*/
|
|
122
|
+
export function getStoryKey(storyId, browser, viewport, mode) {
|
|
118
123
|
const safeId = storyId.replace(/[^a-zA-Z0-9-]/g, '-');
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
124
|
+
const browserPart = browser ? `-${browser}` : '';
|
|
125
|
+
const viewportPart = viewport ? `-${viewport.width}x${viewport.height}` : '';
|
|
126
|
+
// As on the server: '@' and the URI-encoded name, so any mode name is one
|
|
127
|
+
// safe file name, and nothing at all without a mode.
|
|
128
|
+
const modePart = mode ? `@${encodeURIComponent(mode)}` : '';
|
|
129
|
+
return `${safeId}${browserPart}${viewportPart}${modePart}.png`;
|
|
130
|
+
}
|
|
131
|
+
/** The local baseline file name for a capture, given the configured viewport. */
|
|
132
|
+
export function getCaptureKey(capture, configured) {
|
|
133
|
+
const atConfigured = !configured || (capture.width === configured.width && capture.height === configured.height);
|
|
134
|
+
return getStoryKey(capture.storyId, capture.browser, atConfigured ? undefined : { width: capture.width, height: capture.height }, capture.mode);
|
|
123
135
|
}
|
|
124
136
|
//# sourceMappingURL=storybook.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Viewport, ViewportInput } from '@storysnap/shared';
|
|
2
|
+
/**
|
|
3
|
+
* The viewports a story is captured at: its own from its parameters, else the
|
|
4
|
+
* configured list, else the one configured `viewport`. A bare number is a
|
|
5
|
+
* width at the configured viewport's height, as in Chromatic's `viewports`.
|
|
6
|
+
* Duplicates are dropped and the order is kept.
|
|
7
|
+
*/
|
|
8
|
+
export declare function resolveViewports(config: {
|
|
9
|
+
viewport: Viewport;
|
|
10
|
+
viewports?: ViewportInput[];
|
|
11
|
+
}, storyViewports?: ViewportInput[]): Viewport[];
|
|
12
|
+
export declare function sameViewport(a: Viewport, b: Viewport): boolean;
|
|
13
|
+
export declare function describeViewport(viewport: Viewport): string;
|
|
14
|
+
//# sourceMappingURL=viewports.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The viewports a story is captured at: its own from its parameters, else the
|
|
3
|
+
* configured list, else the one configured `viewport`. A bare number is a
|
|
4
|
+
* width at the configured viewport's height, as in Chromatic's `viewports`.
|
|
5
|
+
* Duplicates are dropped and the order is kept.
|
|
6
|
+
*/
|
|
7
|
+
export function resolveViewports(config, storyViewports) {
|
|
8
|
+
const list = storyViewports ?? config.viewports ?? [config.viewport];
|
|
9
|
+
const seen = new Set();
|
|
10
|
+
const viewports = [];
|
|
11
|
+
for (const entry of list) {
|
|
12
|
+
const viewport = typeof entry === 'number' ? { width: entry, height: config.viewport.height } : entry;
|
|
13
|
+
const label = describeViewport(viewport);
|
|
14
|
+
if (!seen.has(label)) {
|
|
15
|
+
seen.add(label);
|
|
16
|
+
viewports.push({ width: viewport.width, height: viewport.height });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return viewports;
|
|
20
|
+
}
|
|
21
|
+
export function sameViewport(a, b) {
|
|
22
|
+
return a.width === b.width && a.height === b.height;
|
|
23
|
+
}
|
|
24
|
+
export function describeViewport(viewport) {
|
|
25
|
+
return `${viewport.width}x${viewport.height}`;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=viewports.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storysnap/cli",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Visual regression testing for Storybook - capture screenshots, detect changes, and maintain baselines",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -53,23 +53,22 @@
|
|
|
53
53
|
},
|
|
54
54
|
"homepage": "https://github.com/Kosai106/storysnap#readme",
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@aws-sdk/client-s3": "^3.
|
|
56
|
+
"@aws-sdk/client-s3": "^3.1164.0",
|
|
57
57
|
"chalk": "^6.0.1",
|
|
58
|
-
"commander": "^15.
|
|
59
|
-
"
|
|
60
|
-
"ora": "^9.4.1",
|
|
58
|
+
"commander": "^15.1.0",
|
|
59
|
+
"ora": "^10.0.0",
|
|
61
60
|
"pixelmatch": "^8.0.0",
|
|
62
|
-
"playwright": "^1.
|
|
61
|
+
"playwright": "^1.65.0",
|
|
63
62
|
"pngjs": "^7.0.0",
|
|
64
|
-
"zod": "^4.6.
|
|
63
|
+
"zod": "^4.6.7"
|
|
65
64
|
},
|
|
66
65
|
"devDependencies": {
|
|
67
66
|
"@storysnap/shared": "1.0.0",
|
|
68
|
-
"@types/node": "^22.
|
|
67
|
+
"@types/node": "^22.20.5",
|
|
69
68
|
"@types/pngjs": "^6.0.5",
|
|
70
|
-
"@vitest/coverage-v8": "^5.0
|
|
69
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
71
70
|
"typescript": "^7.0.2",
|
|
72
|
-
"vitest": "^5.0
|
|
71
|
+
"vitest": "^5.1.0"
|
|
73
72
|
},
|
|
74
73
|
"engines": {
|
|
75
74
|
"node": ">=22.12.0"
|