@storysnap/cli 1.1.0 → 1.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/README.md +8 -2
- package/dist/cli/commands/init.js +3 -2
- package/dist/cli/commands/test.d.ts +3 -2
- package/dist/cli/commands/test.js +46 -52
- package/dist/cli/commands/update.d.ts +3 -2
- package/dist/cli/commands/update.js +88 -109
- package/dist/cli/index.js +23 -3
- package/dist/cli/select-stories.d.ts +25 -0
- package/dist/cli/select-stories.js +57 -0
- package/dist/config/loader.js +19 -13
- package/dist/config/schema.d.ts +5 -0
- package/dist/config/schema.js +4 -0
- package/dist/core/changed.d.ts +26 -0
- package/dist/core/changed.js +78 -0
- package/dist/core/compare.d.ts +2 -0
- package/dist/core/compare.js +17 -13
- package/dist/core/concurrency.d.ts +7 -0
- package/dist/core/concurrency.js +15 -0
- package/dist/core/screenshot.d.ts +5 -0
- package/dist/core/screenshot.js +35 -23
- package/dist/core/storybook.d.ts +1 -0
- package/dist/core/storybook.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,8 +112,11 @@ npx @storysnap/cli test [options]
|
|
|
112
112
|
|
|
113
113
|
Options:
|
|
114
114
|
-c, --config <path> Path to config file
|
|
115
|
-
-s, --story <pattern>
|
|
115
|
+
-s, --story <pattern> Only stories matching a pattern (repeatable)
|
|
116
|
+
-x, --exclude <pattern> Skip stories matching a pattern (repeatable)
|
|
117
|
+
--changed [ref] Only stories changed since branching off ref (default: main)
|
|
116
118
|
-b, --browser <browsers> Browsers to test (chromium,firefox,webkit)
|
|
119
|
+
--concurrency <n> Stories to capture at once (default: 4)
|
|
117
120
|
--ci CI mode - fail on any visual diff or new story
|
|
118
121
|
--local Force local-only mode without using the API
|
|
119
122
|
-v, --verbose Verbose output
|
|
@@ -128,8 +131,11 @@ npx @storysnap/cli update [options]
|
|
|
128
131
|
|
|
129
132
|
Options:
|
|
130
133
|
-c, --config <path> Path to config file
|
|
131
|
-
-s, --story <pattern>
|
|
134
|
+
-s, --story <pattern> Only stories matching a pattern (repeatable)
|
|
135
|
+
-x, --exclude <pattern> Skip stories matching a pattern (repeatable)
|
|
136
|
+
--changed [ref] Only stories changed since branching off ref (default: main)
|
|
132
137
|
-b, --browser <browsers> Browsers to test (chromium,firefox,webkit)
|
|
138
|
+
--concurrency <n> Stories to capture at once (default: 4)
|
|
133
139
|
--dry-run Show what would be updated without making changes
|
|
134
140
|
--local Force local-only mode without using the API
|
|
135
141
|
-v, --verbose Verbose output
|
|
@@ -40,9 +40,10 @@ export async function initCommand(options = {}) {
|
|
|
40
40
|
reporter.success(`Created ${fileName}`);
|
|
41
41
|
reporter.newLine();
|
|
42
42
|
reporter.info('Next steps:');
|
|
43
|
-
reporter.info(' 1.
|
|
44
|
-
reporter.info(' 2. Run "storysnap
|
|
43
|
+
reporter.info(' 1. Check the Storybook settings in the config');
|
|
44
|
+
reporter.info(' 2. Run "storysnap login" to use a StorySnap server, or skip it to keep baselines local');
|
|
45
45
|
reporter.info(' 3. Run "storysnap update" to save baselines');
|
|
46
|
+
reporter.info(' 4. Run "storysnap test" to compare against them');
|
|
46
47
|
}
|
|
47
48
|
catch (error) {
|
|
48
49
|
reporter.error(`Failed to create config: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { type StorySelectionOptions } from '../select-stories.js';
|
|
2
|
+
export interface TestOptions extends StorySelectionOptions {
|
|
2
3
|
config?: string;
|
|
3
|
-
story?: string;
|
|
4
4
|
browser?: string;
|
|
5
5
|
storybookUrl?: string;
|
|
6
|
+
concurrency?: number;
|
|
6
7
|
branch?: string;
|
|
7
8
|
commit?: string;
|
|
8
9
|
ci?: boolean;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { loadConfig } from '../../config/loader.js';
|
|
2
2
|
import { loadCredentials } from '../../config/credentials.js';
|
|
3
|
-
import { discoverStories } from '../../core/storybook.js';
|
|
4
3
|
import { captureScreenshots, getScreenshotKey, parseScreenshotKey } from '../../core/screenshot.js';
|
|
4
|
+
import { mapWithConcurrency } from '../../core/concurrency.js';
|
|
5
5
|
import { compareAllScreenshots, DEFAULT_MAX_DIFF_PERCENT } from '../../core/compare.js';
|
|
6
6
|
import { createStorage } from '../../storage/index.js';
|
|
7
7
|
import { createReporter } from '../reporter.js';
|
|
8
|
+
import { selectStories } from '../select-stories.js';
|
|
8
9
|
import { ApiClient } from '../../api/client.js';
|
|
9
10
|
function parseBrowsers(browserArg) {
|
|
10
11
|
if (!browserArg) {
|
|
@@ -39,6 +40,9 @@ export async function testCommand(options = {}) {
|
|
|
39
40
|
config.storybookUrl = options.storybookUrl;
|
|
40
41
|
config.storybookStaticDir = undefined;
|
|
41
42
|
}
|
|
43
|
+
if (options.concurrency) {
|
|
44
|
+
config.concurrency = options.concurrency;
|
|
45
|
+
}
|
|
42
46
|
reporter.succeedSpinner('Configuration loaded');
|
|
43
47
|
// Determine if we should use API mode
|
|
44
48
|
const credentials = await loadCredentials();
|
|
@@ -63,21 +67,13 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
63
67
|
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
64
68
|
reporter.newLine();
|
|
65
69
|
const client = new ApiClient({ apiUrl, apiToken });
|
|
66
|
-
// Apply story filter if provided
|
|
67
|
-
if (options.story) {
|
|
68
|
-
config.include = [options.story];
|
|
69
|
-
}
|
|
70
70
|
// Screenshots are captured here, not by the server: the worker has no route
|
|
71
71
|
// to a Storybook running on this machine or on a CI runner.
|
|
72
|
-
reporter
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
reporter.failSpinner('No stories found');
|
|
76
|
-
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
77
|
-
process.exit(1);
|
|
72
|
+
const stories = await selectStories(reporter, config, options, 'tests');
|
|
73
|
+
if (!stories) {
|
|
74
|
+
return;
|
|
78
75
|
}
|
|
79
76
|
const totalTests = stories.length * config.browsers.length;
|
|
80
|
-
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} tests across ${config.browsers.length} browser(s))`);
|
|
81
77
|
reporter.startSpinner('Capturing screenshots...');
|
|
82
78
|
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
83
79
|
reporter.progress(completed, total, `${story.title}/${story.name} (${browser})`);
|
|
@@ -119,7 +115,7 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
119
115
|
});
|
|
120
116
|
reporter.succeedSpinner('Tests complete');
|
|
121
117
|
// Report results
|
|
122
|
-
reportApiResults(reporter, completedTestRun, options);
|
|
118
|
+
reportApiResults(reporter, completedTestRun, options, totalTests - uploaded);
|
|
123
119
|
}
|
|
124
120
|
/**
|
|
125
121
|
* Registers each captured screenshot with the API and PUTs the PNG to the
|
|
@@ -129,32 +125,33 @@ async function uploadScreenshots(client, testRunId, config, stories, screenshots
|
|
|
129
125
|
const total = stories.length * config.browsers.length;
|
|
130
126
|
let uploaded = 0;
|
|
131
127
|
let attempted = 0;
|
|
132
|
-
|
|
133
|
-
|
|
128
|
+
// Two requests per story, so they share the capture concurrency.
|
|
129
|
+
const tasks = stories.flatMap((story) => config.browsers.map((browser) => ({ story, browser })));
|
|
130
|
+
await mapWithConcurrency(tasks, config.concurrency, async ({ story, browser }) => {
|
|
131
|
+
const screenshot = screenshots.get(getScreenshotKey(story.id, browser));
|
|
132
|
+
if (!screenshot || screenshot.buffer.length === 0) {
|
|
133
|
+
reporter.warning(`Failed to capture ${story.id} (${browser}): ${screenshot?.error ?? 'no screenshot'}`);
|
|
134
134
|
attempted++;
|
|
135
|
-
|
|
136
|
-
if (!screenshot || screenshot.buffer.length === 0) {
|
|
137
|
-
reporter.progress(attempted, total, `${story.title}/${story.name} (${browser}) - capture failed`);
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
reporter.progress(attempted, total, `${story.title}/${story.name} (${browser})`);
|
|
141
|
-
try {
|
|
142
|
-
const target = await client.createResult(testRunId, {
|
|
143
|
-
storyId: story.id,
|
|
144
|
-
storyTitle: story.title,
|
|
145
|
-
storyName: story.name,
|
|
146
|
-
browser,
|
|
147
|
-
viewportWidth: config.viewport.width,
|
|
148
|
-
viewportHeight: config.viewport.height,
|
|
149
|
-
});
|
|
150
|
-
await client.uploadImage(target.uploadUrl, screenshot.buffer);
|
|
151
|
-
uploaded++;
|
|
152
|
-
}
|
|
153
|
-
catch (error) {
|
|
154
|
-
reporter.warning(`Failed to upload ${story.id} (${browser}): ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
155
|
-
}
|
|
135
|
+
return;
|
|
156
136
|
}
|
|
157
|
-
|
|
137
|
+
try {
|
|
138
|
+
const target = await client.createResult(testRunId, {
|
|
139
|
+
storyId: story.id,
|
|
140
|
+
storyTitle: story.title,
|
|
141
|
+
storyName: story.name,
|
|
142
|
+
browser,
|
|
143
|
+
viewportWidth: config.viewport.width,
|
|
144
|
+
viewportHeight: config.viewport.height,
|
|
145
|
+
});
|
|
146
|
+
await client.uploadImage(target.uploadUrl, screenshot.buffer);
|
|
147
|
+
uploaded++;
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
reporter.warning(`Failed to upload ${story.id} (${browser}): ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
151
|
+
}
|
|
152
|
+
attempted++;
|
|
153
|
+
reporter.progress(attempted, total, `${story.title}/${story.name} (${browser})`);
|
|
154
|
+
});
|
|
158
155
|
return uploaded;
|
|
159
156
|
}
|
|
160
157
|
async function runLocalMode(reporter, config, options) {
|
|
@@ -163,22 +160,13 @@ async function runLocalMode(reporter, config, options) {
|
|
|
163
160
|
}
|
|
164
161
|
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
165
162
|
reporter.newLine();
|
|
166
|
-
// Apply story filter if provided
|
|
167
|
-
if (options.story) {
|
|
168
|
-
config.include = [options.story];
|
|
169
|
-
}
|
|
170
163
|
// Create storage
|
|
171
164
|
const storage = createStorage(config.storage);
|
|
172
165
|
// Discover stories
|
|
173
|
-
reporter
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
reporter.failSpinner('No stories found');
|
|
177
|
-
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
178
|
-
process.exit(1);
|
|
166
|
+
const stories = await selectStories(reporter, config, options, 'tests');
|
|
167
|
+
if (!stories) {
|
|
168
|
+
return;
|
|
179
169
|
}
|
|
180
|
-
const totalTests = stories.length * config.browsers.length;
|
|
181
|
-
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} tests across ${config.browsers.length} browser(s))`);
|
|
182
170
|
// Capture screenshots
|
|
183
171
|
reporter.startSpinner('Capturing screenshots...');
|
|
184
172
|
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
@@ -192,6 +180,7 @@ async function runLocalMode(reporter, config, options) {
|
|
|
192
180
|
maxDiffPercent: config.maxDiffPercent,
|
|
193
181
|
storage,
|
|
194
182
|
localSnapshotsDir: config.localSnapshotsDir,
|
|
183
|
+
concurrency: config.concurrency,
|
|
195
184
|
}, (completed, total, key) => {
|
|
196
185
|
const { storyId, browser } = parseScreenshotKey(key);
|
|
197
186
|
reporter.progress(completed, total, `${storyId} (${browser})`);
|
|
@@ -244,7 +233,7 @@ function reportLocalResult(story, comparison) {
|
|
|
244
233
|
break;
|
|
245
234
|
}
|
|
246
235
|
}
|
|
247
|
-
function reportApiResults(reporter, testRun, options) {
|
|
236
|
+
function reportApiResults(reporter, testRun, options, missing) {
|
|
248
237
|
reporter.header('Results');
|
|
249
238
|
for (const result of testRun.results) {
|
|
250
239
|
const storyName = result.storyName || result.storyId;
|
|
@@ -303,12 +292,17 @@ function reportApiResults(reporter, testRun, options) {
|
|
|
303
292
|
if (testRun.status === 'failed') {
|
|
304
293
|
console.log('\x1b[31mVisual regression tests failed.\x1b[0m', '\x1b[2mReview changes in the dashboard or run "storysnap update" to update baselines.\x1b[0m');
|
|
305
294
|
}
|
|
295
|
+
else if (missing > 0) {
|
|
296
|
+
console.log(`\x1b[31m${missing} screenshot(s) were not captured or uploaded.\x1b[0m`, '\x1b[2mSee the warnings above.\x1b[0m');
|
|
297
|
+
}
|
|
306
298
|
else {
|
|
307
299
|
console.log('\x1b[32mAll visual regression tests passed!\x1b[0m');
|
|
308
300
|
}
|
|
309
301
|
// Same rule as local mode: a failure is a failure regardless of --ci. See the
|
|
310
|
-
// note there for why this is not gated on the flag.
|
|
311
|
-
|
|
302
|
+
// note there for why this is not gated on the flag. Stories that never
|
|
303
|
+
// reached the server are absent from the run rather than failed in it, so
|
|
304
|
+
// the server's verdict alone would pass them.
|
|
305
|
+
if (testRun.failedStories > 0 || testRun.errorStories > 0 || missing > 0) {
|
|
312
306
|
process.exit(1);
|
|
313
307
|
}
|
|
314
308
|
if (options.ci && testRun.newStories > 0) {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { type StorySelectionOptions } from '../select-stories.js';
|
|
2
|
+
export interface UpdateOptions extends StorySelectionOptions {
|
|
2
3
|
config?: string;
|
|
3
|
-
story?: string;
|
|
4
4
|
browser?: string;
|
|
5
5
|
storybookUrl?: string;
|
|
6
|
+
concurrency?: number;
|
|
6
7
|
verbose?: boolean;
|
|
7
8
|
dryRun?: boolean;
|
|
8
9
|
local?: boolean;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { loadConfig } from '../../config/loader.js';
|
|
2
2
|
import { loadCredentials } from '../../config/credentials.js';
|
|
3
|
-
import {
|
|
3
|
+
import { getStoryKey } from '../../core/storybook.js';
|
|
4
4
|
import { captureScreenshots, getScreenshotKey } from '../../core/screenshot.js';
|
|
5
|
+
import { mapWithConcurrency } from '../../core/concurrency.js';
|
|
5
6
|
import { createStorage } from '../../storage/index.js';
|
|
6
7
|
import { createReporter } from '../reporter.js';
|
|
8
|
+
import { selectStories } from '../select-stories.js';
|
|
7
9
|
import { ApiClient } from '../../api/client.js';
|
|
8
10
|
function parseBrowsers(browserArg) {
|
|
9
11
|
if (!browserArg) {
|
|
@@ -42,6 +44,9 @@ export async function updateCommand(options = {}) {
|
|
|
42
44
|
config.storybookUrl = options.storybookUrl;
|
|
43
45
|
config.storybookStaticDir = undefined;
|
|
44
46
|
}
|
|
47
|
+
if (options.concurrency) {
|
|
48
|
+
config.concurrency = options.concurrency;
|
|
49
|
+
}
|
|
45
50
|
reporter.succeedSpinner('Configuration loaded');
|
|
46
51
|
// Determine if we should use API mode
|
|
47
52
|
const credentials = await loadCredentials();
|
|
@@ -66,20 +71,12 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
66
71
|
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
67
72
|
reporter.newLine();
|
|
68
73
|
const client = new ApiClient({ apiUrl, apiToken });
|
|
69
|
-
// Apply story filter if provided
|
|
70
|
-
if (options.story) {
|
|
71
|
-
config.include = [options.story];
|
|
72
|
-
}
|
|
73
74
|
// Discover stories
|
|
74
|
-
reporter
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
reporter.failSpinner('No stories found');
|
|
78
|
-
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
79
|
-
process.exit(1);
|
|
75
|
+
const stories = await selectStories(reporter, config, options, 'baselines');
|
|
76
|
+
if (!stories) {
|
|
77
|
+
return;
|
|
80
78
|
}
|
|
81
79
|
const totalTests = stories.length * config.browsers.length;
|
|
82
|
-
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} baselines across ${config.browsers.length} browser(s))`);
|
|
83
80
|
// Capture screenshots
|
|
84
81
|
reporter.startSpinner('Capturing screenshots...');
|
|
85
82
|
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
@@ -89,63 +86,43 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
89
86
|
// Upload baselines via API
|
|
90
87
|
reporter.startSpinner('Uploading baselines via API...');
|
|
91
88
|
let uploaded = 0;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
});
|
|
118
|
-
uploaded++;
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
failed++;
|
|
122
|
-
if (options.verbose) {
|
|
123
|
-
reporter.error(`Failed to upload ${story.id} (${browser}): ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
89
|
+
const failures = [];
|
|
90
|
+
// Uploads are network round trips, so they share the capture concurrency.
|
|
91
|
+
const tasks = stories.flatMap((story) => config.browsers.map((browser) => ({ story, browser })));
|
|
92
|
+
await mapWithConcurrency(tasks, config.concurrency, async ({ story, browser }) => {
|
|
93
|
+
const screenshotKey = getScreenshotKey(story.id, browser);
|
|
94
|
+
const screenshot = screenshots.get(screenshotKey);
|
|
95
|
+
if (!screenshot || screenshot.buffer.length === 0) {
|
|
96
|
+
failures.push(`${story.title}/${story.name} (${browser}): capture failed - ${screenshot?.error ?? 'no screenshot'}`);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (!options.dryRun) {
|
|
100
|
+
try {
|
|
101
|
+
// Get presigned upload URL from API
|
|
102
|
+
const uploadInfo = await client.getBaselineUploadUrl(story.id, browser, config.viewport.width, config.viewport.height);
|
|
103
|
+
// Upload directly to S3 using presigned URL
|
|
104
|
+
await client.uploadBaseline(uploadInfo.url, screenshot.buffer);
|
|
105
|
+
// Record the baseline. The upload alone leaves an orphaned object -
|
|
106
|
+
// comparisons look the baseline up in the database, not in S3.
|
|
107
|
+
await client.registerBaseline({
|
|
108
|
+
storyId: story.id,
|
|
109
|
+
browser,
|
|
110
|
+
viewportWidth: config.viewport.width,
|
|
111
|
+
viewportHeight: config.viewport.height,
|
|
112
|
+
storageKey: uploadInfo.storageKey,
|
|
113
|
+
});
|
|
128
114
|
uploaded++;
|
|
129
115
|
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
failures.push(`${story.title}/${story.name} (${browser}): upload failed - ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
118
|
+
}
|
|
130
119
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
// Report summary
|
|
139
|
-
reporter.reportUpdateSummary(uploaded, totalTests);
|
|
140
|
-
if (failed > 0) {
|
|
141
|
-
reporter.warning(`${failed} baselines could not be updated`);
|
|
142
|
-
}
|
|
143
|
-
if (options.dryRun) {
|
|
144
|
-
reporter.info('Dry run complete. No changes were made.');
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
reporter.success('Baselines have been updated successfully!');
|
|
148
|
-
}
|
|
120
|
+
else {
|
|
121
|
+
uploaded++;
|
|
122
|
+
}
|
|
123
|
+
reporter.progress(uploaded + failures.length, totalTests, `${story.title}/${story.name} (${browser})`);
|
|
124
|
+
});
|
|
125
|
+
finishUpdate(reporter, uploaded, totalTests, failures, options);
|
|
149
126
|
}
|
|
150
127
|
async function runLocalMode(reporter, config, options) {
|
|
151
128
|
if (options.local) {
|
|
@@ -153,22 +130,14 @@ async function runLocalMode(reporter, config, options) {
|
|
|
153
130
|
}
|
|
154
131
|
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
155
132
|
reporter.newLine();
|
|
156
|
-
// Apply story filter if provided
|
|
157
|
-
if (options.story) {
|
|
158
|
-
config.include = [options.story];
|
|
159
|
-
}
|
|
160
133
|
// Create storage
|
|
161
134
|
const storage = createStorage(config.storage);
|
|
162
135
|
// Discover stories
|
|
163
|
-
reporter
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
reporter.failSpinner('No stories found');
|
|
167
|
-
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
168
|
-
process.exit(1);
|
|
136
|
+
const stories = await selectStories(reporter, config, options, 'baselines');
|
|
137
|
+
if (!stories) {
|
|
138
|
+
return;
|
|
169
139
|
}
|
|
170
140
|
const totalTests = stories.length * config.browsers.length;
|
|
171
|
-
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} baselines across ${config.browsers.length} browser(s))`);
|
|
172
141
|
// Capture screenshots
|
|
173
142
|
reporter.startSpinner('Capturing screenshots...');
|
|
174
143
|
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
@@ -178,46 +147,56 @@ async function runLocalMode(reporter, config, options) {
|
|
|
178
147
|
// Upload baselines
|
|
179
148
|
reporter.startSpinner('Uploading baselines...');
|
|
180
149
|
let uploaded = 0;
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
uploaded++;
|
|
198
|
-
}
|
|
199
|
-
catch (error) {
|
|
200
|
-
failed++;
|
|
201
|
-
if (options.verbose) {
|
|
202
|
-
reporter.error(`Failed to upload ${key}: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
150
|
+
const failures = [];
|
|
151
|
+
// Writes to S3 are network round trips, so they share the capture
|
|
152
|
+
// concurrency; local writes simply finish sooner.
|
|
153
|
+
const tasks = stories.flatMap((story) => config.browsers.map((browser) => ({ story, browser })));
|
|
154
|
+
await mapWithConcurrency(tasks, config.concurrency, async ({ story, browser }) => {
|
|
155
|
+
const screenshotKey = getScreenshotKey(story.id, browser);
|
|
156
|
+
const screenshot = screenshots.get(screenshotKey);
|
|
157
|
+
if (!screenshot || screenshot.buffer.length === 0) {
|
|
158
|
+
failures.push(`${story.title}/${story.name} (${browser}): capture failed - ${screenshot?.error ?? 'no screenshot'}`);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// Use browser-aware storage key
|
|
162
|
+
const key = getStoryKey(story.id, browser);
|
|
163
|
+
if (!options.dryRun) {
|
|
164
|
+
try {
|
|
165
|
+
await storage.upload(key, screenshot.buffer);
|
|
207
166
|
uploaded++;
|
|
208
167
|
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
failures.push(`${story.title}/${story.name} (${browser}): upload failed - ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
170
|
+
}
|
|
209
171
|
}
|
|
210
|
-
|
|
211
|
-
|
|
172
|
+
else {
|
|
173
|
+
uploaded++;
|
|
174
|
+
}
|
|
175
|
+
reporter.progress(uploaded + failures.length, totalTests, `${story.title}/${story.name} (${browser})`);
|
|
176
|
+
});
|
|
177
|
+
finishUpdate(reporter, uploaded, totalTests, failures, options);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Reports the outcome and exits non-zero if any baseline was not saved. A run
|
|
181
|
+
* that quietly drops baselines and exits 0 lets a pipeline go green with an
|
|
182
|
+
* incomplete set, which every later test run is then judged against.
|
|
183
|
+
*/
|
|
184
|
+
function finishUpdate(reporter, uploaded, totalTests, failures, options) {
|
|
185
|
+
if (failures.length === 0) {
|
|
212
186
|
reporter.succeedSpinner('Baselines uploaded');
|
|
213
187
|
}
|
|
214
188
|
else {
|
|
215
|
-
reporter.
|
|
189
|
+
reporter.failSpinner(`Baselines uploaded (${failures.length} failed)`);
|
|
216
190
|
}
|
|
217
|
-
// Report summary
|
|
218
191
|
reporter.reportUpdateSummary(uploaded, totalTests);
|
|
219
|
-
if (
|
|
220
|
-
reporter.
|
|
192
|
+
if (failures.length > 0) {
|
|
193
|
+
reporter.newLine();
|
|
194
|
+
for (const failure of failures) {
|
|
195
|
+
reporter.error(failure);
|
|
196
|
+
}
|
|
197
|
+
reporter.newLine();
|
|
198
|
+
reporter.error(`${failures.length} baseline(s) could not be updated`);
|
|
199
|
+
process.exit(1);
|
|
221
200
|
}
|
|
222
201
|
if (options.dryRun) {
|
|
223
202
|
reporter.info('Dry run complete. No changes were made.');
|
package/dist/cli/index.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { Command } from 'commander';
|
|
2
|
+
import { Command, InvalidArgumentError } from 'commander';
|
|
3
3
|
import { testCommand } from './commands/test.js';
|
|
4
4
|
import { updateCommand } from './commands/update.js';
|
|
5
5
|
import { initCommand } from './commands/init.js';
|
|
6
6
|
import { loginCommand } from './commands/login.js';
|
|
7
7
|
import { logoutCommand } from './commands/logout.js';
|
|
8
|
+
import { collectPatterns } from './select-stories.js';
|
|
9
|
+
function parseConcurrency(value) {
|
|
10
|
+
const n = Number(value);
|
|
11
|
+
if (!Number.isInteger(n) || n < 1) {
|
|
12
|
+
throw new InvalidArgumentError('Must be a whole number of at least 1.');
|
|
13
|
+
}
|
|
14
|
+
return n;
|
|
15
|
+
}
|
|
8
16
|
const program = new Command();
|
|
9
17
|
program
|
|
10
18
|
.name('storysnap')
|
|
@@ -47,9 +55,12 @@ program
|
|
|
47
55
|
.command('test')
|
|
48
56
|
.description('Run visual regression tests')
|
|
49
57
|
.option('-c, --config <path>', 'Path to config file')
|
|
50
|
-
.option('-s, --story <pattern>', '
|
|
58
|
+
.option('-s, --story <pattern>', 'Only stories matching a pattern (* wildcards; repeat or comma-separate for several)', collectPatterns)
|
|
59
|
+
.option('-x, --exclude <pattern>', 'Skip stories matching a pattern (* wildcards; repeat or comma-separate for several)', collectPatterns)
|
|
60
|
+
.option('--changed [ref]', 'Only stories whose files changed since branching off <ref> (default: main, then origin/main)')
|
|
51
61
|
.option('-b, --browser <browsers>', 'Browsers to test (chromium,firefox,webkit)')
|
|
52
62
|
.option('--storybook-url <url>', 'Storybook URL, overriding the config file')
|
|
63
|
+
.option('--concurrency <n>', 'Stories to capture at once (default: 4)', parseConcurrency)
|
|
53
64
|
.option('--branch <branch>', 'Branch name to record on the test run (API mode)')
|
|
54
65
|
.option('--commit <sha>', 'Commit SHA to record on the test run (API mode)')
|
|
55
66
|
.option('--ci', 'CI mode - fail on any visual diff or new story')
|
|
@@ -59,8 +70,11 @@ program
|
|
|
59
70
|
await testCommand({
|
|
60
71
|
config: options.config,
|
|
61
72
|
story: options.story,
|
|
73
|
+
exclude: options.exclude,
|
|
74
|
+
changed: options.changed,
|
|
62
75
|
browser: options.browser,
|
|
63
76
|
storybookUrl: options.storybookUrl,
|
|
77
|
+
concurrency: options.concurrency,
|
|
64
78
|
branch: options.branch,
|
|
65
79
|
commit: options.commit,
|
|
66
80
|
ci: options.ci,
|
|
@@ -72,9 +86,12 @@ program
|
|
|
72
86
|
.command('update')
|
|
73
87
|
.description('Update baseline screenshots')
|
|
74
88
|
.option('-c, --config <path>', 'Path to config file')
|
|
75
|
-
.option('-s, --story <pattern>', '
|
|
89
|
+
.option('-s, --story <pattern>', 'Only stories matching a pattern (* wildcards; repeat or comma-separate for several)', collectPatterns)
|
|
90
|
+
.option('-x, --exclude <pattern>', 'Skip stories matching a pattern (* wildcards; repeat or comma-separate for several)', collectPatterns)
|
|
91
|
+
.option('--changed [ref]', 'Only stories whose files changed since branching off <ref> (default: main, then origin/main)')
|
|
76
92
|
.option('-b, --browser <browsers>', 'Browsers to test (chromium,firefox,webkit)')
|
|
77
93
|
.option('--storybook-url <url>', 'Storybook URL, overriding the config file')
|
|
94
|
+
.option('--concurrency <n>', 'Stories to capture at once (default: 4)', parseConcurrency)
|
|
78
95
|
.option('-v, --verbose', 'Verbose output')
|
|
79
96
|
.option('--dry-run', 'Show what would be updated without making changes')
|
|
80
97
|
.option('--local', 'Force local-only mode without using the API')
|
|
@@ -82,8 +99,11 @@ program
|
|
|
82
99
|
await updateCommand({
|
|
83
100
|
config: options.config,
|
|
84
101
|
story: options.story,
|
|
102
|
+
exclude: options.exclude,
|
|
103
|
+
changed: options.changed,
|
|
85
104
|
browser: options.browser,
|
|
86
105
|
storybookUrl: options.storybookUrl,
|
|
106
|
+
concurrency: options.concurrency,
|
|
87
107
|
verbose: options.verbose,
|
|
88
108
|
dryRun: options.dryRun,
|
|
89
109
|
local: options.local,
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Story, StorySnapConfig } from '@storysnap/shared';
|
|
2
|
+
import type { Reporter } from './reporter.js';
|
|
3
|
+
export interface StorySelectionOptions {
|
|
4
|
+
/** --story patterns. They narrow the config's include list rather than replacing it. */
|
|
5
|
+
story?: string[];
|
|
6
|
+
/** --exclude patterns, applied on top of the config's exclude list. */
|
|
7
|
+
exclude?: string[];
|
|
8
|
+
/** --changed: true for the default ref, or the ref that was named. */
|
|
9
|
+
changed?: string | boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Commander collector for options that may be repeated or comma-separated,
|
|
13
|
+
* e.g. `--story "Button*" --story "Form/*"` or `--story "Button*,Form/*"`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function collectPatterns(value: string, previous?: string[]): string[];
|
|
16
|
+
/**
|
|
17
|
+
* Discovers stories and narrows them by the command-line filters, reporting the
|
|
18
|
+
* result on the spinner. `unit` names what each story-browser pair produces.
|
|
19
|
+
*
|
|
20
|
+
* Returns null when --changed leaves nothing to test, which is a normal outcome
|
|
21
|
+
* for a change that touches no stories. An empty result from the patterns is
|
|
22
|
+
* a mistake instead, so that exits like an empty Storybook does.
|
|
23
|
+
*/
|
|
24
|
+
export declare function selectStories(reporter: Reporter, config: StorySnapConfig, options: StorySelectionOptions, unit: 'tests' | 'baselines'): Promise<Story[] | null>;
|
|
25
|
+
//# sourceMappingURL=select-stories.d.ts.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { discoverStories, filterStories } from '../core/storybook.js';
|
|
2
|
+
import { getChangedFiles, filterChangedStories } from '../core/changed.js';
|
|
3
|
+
/**
|
|
4
|
+
* Commander collector for options that may be repeated or comma-separated,
|
|
5
|
+
* e.g. `--story "Button*" --story "Form/*"` or `--story "Button*,Form/*"`.
|
|
6
|
+
*/
|
|
7
|
+
export function collectPatterns(value, previous = []) {
|
|
8
|
+
return previous.concat(value
|
|
9
|
+
.split(',')
|
|
10
|
+
.map((pattern) => pattern.trim())
|
|
11
|
+
.filter(Boolean));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Discovers stories and narrows them by the command-line filters, reporting the
|
|
15
|
+
* result on the spinner. `unit` names what each story-browser pair produces.
|
|
16
|
+
*
|
|
17
|
+
* Returns null when --changed leaves nothing to test, which is a normal outcome
|
|
18
|
+
* for a change that touches no stories. An empty result from the patterns is
|
|
19
|
+
* a mistake instead, so that exits like an empty Storybook does.
|
|
20
|
+
*/
|
|
21
|
+
export async function selectStories(reporter, config, options, unit) {
|
|
22
|
+
reporter.startSpinner('Discovering stories...');
|
|
23
|
+
const discovered = await discoverStories(config);
|
|
24
|
+
if (discovered.length === 0) {
|
|
25
|
+
reporter.failSpinner('No stories found');
|
|
26
|
+
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
let stories = filterStories(discovered, options.story, options.exclude);
|
|
30
|
+
if (stories.length === 0) {
|
|
31
|
+
reporter.failSpinner('No stories match the --story/--exclude filters');
|
|
32
|
+
reporter.info(`Patterns are matched against story IDs, titles and names, e.g. "button--primary" or "Components/Button/*".`);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
let changedSuffix = '';
|
|
36
|
+
if (options.changed) {
|
|
37
|
+
reporter.updateSpinner('Finding changed files...');
|
|
38
|
+
const { ref, files } = await getChangedFiles(typeof options.changed === 'string' ? options.changed : undefined);
|
|
39
|
+
const result = filterChangedStories(stories, files);
|
|
40
|
+
if (result.stories.length === 0) {
|
|
41
|
+
reporter.succeedSpinner(`No stories affected by changes since ${ref} - nothing to do`);
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
if (result.unknown > 0) {
|
|
45
|
+
reporter.warning(`${reporter.formatStoryCount(result.unknown)} have no file path in the Storybook index, so --changed included them`);
|
|
46
|
+
}
|
|
47
|
+
stories = result.stories;
|
|
48
|
+
changedSuffix = ` changed since ${ref}`;
|
|
49
|
+
}
|
|
50
|
+
const total = stories.length * config.browsers.length;
|
|
51
|
+
const counted = stories.length === discovered.length
|
|
52
|
+
? `Found ${reporter.formatStoryCount(stories.length)}`
|
|
53
|
+
: `Selected ${stories.length} of ${reporter.formatStoryCount(discovered.length)}${changedSuffix}`;
|
|
54
|
+
reporter.succeedSpinner(`${counted} (${total} ${unit} across ${config.browsers.length} browser(s))`);
|
|
55
|
+
return stories;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=select-stories.js.map
|
package/dist/config/loader.js
CHANGED
|
@@ -58,13 +58,25 @@ const config: StorySnapConfig = {
|
|
|
58
58
|
storybookUrl: 'http://localhost:6006',
|
|
59
59
|
// storybookStaticDir: './storybook-static',
|
|
60
60
|
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
// Where baselines live depends on the mode the CLI runs in:
|
|
62
|
+
//
|
|
63
|
+
// - API mode: used when an API URL and token are available, from
|
|
64
|
+
// "storysnap login", STORYSNAP_API_URL/STORYSNAP_API_TOKEN, or apiUrl and
|
|
65
|
+
// apiToken here. The server stores everything and the storage setting
|
|
66
|
+
// below is ignored. Runs show up in the dashboard.
|
|
67
|
+
// - Local mode: used otherwise, or with --local. Baselines are written to
|
|
68
|
+
// .storysnap/baselines unless you set storage yourself, for example:
|
|
69
|
+
//
|
|
70
|
+
// storage: {
|
|
71
|
+
// type: 's3',
|
|
72
|
+
// bucket: 'my-snapshots',
|
|
73
|
+
// region: 'us-east-1',
|
|
74
|
+
// prefix: 'storysnap/',
|
|
75
|
+
// // For S3-compatible storage such as MinIO:
|
|
76
|
+
// // endpoint: 'http://localhost:9000',
|
|
77
|
+
// // accessKeyId: '...',
|
|
78
|
+
// // secretAccessKey: '...',
|
|
79
|
+
// },
|
|
68
80
|
|
|
69
81
|
// Screenshot settings
|
|
70
82
|
viewport: { width: 1280, height: 720 },
|
|
@@ -97,12 +109,6 @@ export function getDefaultConfigJs() {
|
|
|
97
109
|
export function getDefaultConfigJson() {
|
|
98
110
|
return JSON.stringify({
|
|
99
111
|
storybookUrl: 'http://localhost:6006',
|
|
100
|
-
storage: {
|
|
101
|
-
type: 's3',
|
|
102
|
-
bucket: 'my-snapshots',
|
|
103
|
-
region: 'us-east-1',
|
|
104
|
-
prefix: 'storysnap/',
|
|
105
|
-
},
|
|
106
112
|
viewport: { width: 1280, height: 720 },
|
|
107
113
|
threshold: 0.1,
|
|
108
114
|
maxDiffPercent: 0.1,
|
package/dist/config/schema.d.ts
CHANGED
|
@@ -56,6 +56,7 @@ export declare const configSchema: z.ZodEffects<z.ZodObject<{
|
|
|
56
56
|
exclude: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
57
57
|
waitForSelector: z.ZodDefault<z.ZodString>;
|
|
58
58
|
waitTimeout: z.ZodDefault<z.ZodNumber>;
|
|
59
|
+
concurrency: z.ZodDefault<z.ZodNumber>;
|
|
59
60
|
apiUrl: z.ZodOptional<z.ZodString>;
|
|
60
61
|
apiToken: z.ZodOptional<z.ZodString>;
|
|
61
62
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -78,6 +79,7 @@ export declare const configSchema: z.ZodEffects<z.ZodObject<{
|
|
|
78
79
|
browsers: ("chromium" | "firefox" | "webkit")[];
|
|
79
80
|
waitForSelector: string;
|
|
80
81
|
waitTimeout: number;
|
|
82
|
+
concurrency: number;
|
|
81
83
|
storybookUrl?: string | undefined;
|
|
82
84
|
storybookStaticDir?: string | undefined;
|
|
83
85
|
threshold?: number | undefined;
|
|
@@ -114,6 +116,7 @@ export declare const configSchema: z.ZodEffects<z.ZodObject<{
|
|
|
114
116
|
exclude?: string[] | undefined;
|
|
115
117
|
waitForSelector?: string | undefined;
|
|
116
118
|
waitTimeout?: number | undefined;
|
|
119
|
+
concurrency?: number | undefined;
|
|
117
120
|
apiUrl?: string | undefined;
|
|
118
121
|
apiToken?: string | undefined;
|
|
119
122
|
}>, {
|
|
@@ -136,6 +139,7 @@ export declare const configSchema: z.ZodEffects<z.ZodObject<{
|
|
|
136
139
|
browsers: ("chromium" | "firefox" | "webkit")[];
|
|
137
140
|
waitForSelector: string;
|
|
138
141
|
waitTimeout: number;
|
|
142
|
+
concurrency: number;
|
|
139
143
|
storybookUrl?: string | undefined;
|
|
140
144
|
storybookStaticDir?: string | undefined;
|
|
141
145
|
threshold?: number | undefined;
|
|
@@ -172,6 +176,7 @@ export declare const configSchema: z.ZodEffects<z.ZodObject<{
|
|
|
172
176
|
exclude?: string[] | undefined;
|
|
173
177
|
waitForSelector?: string | undefined;
|
|
174
178
|
waitTimeout?: number | undefined;
|
|
179
|
+
concurrency?: number | undefined;
|
|
175
180
|
apiUrl?: string | undefined;
|
|
176
181
|
apiToken?: string | undefined;
|
|
177
182
|
}>;
|
package/dist/config/schema.js
CHANGED
|
@@ -49,6 +49,10 @@ export const configSchema = z.object({
|
|
|
49
49
|
exclude: z.array(z.string()).optional(),
|
|
50
50
|
waitForSelector: z.string().default('#storybook-root'),
|
|
51
51
|
waitTimeout: z.number().min(0).default(30000),
|
|
52
|
+
// How many stories are captured at once. Each capture mostly waits on the
|
|
53
|
+
// page settling, so a few in parallel cut the run time almost linearly; the
|
|
54
|
+
// ceiling is the machine running the browsers and the Storybook serving them.
|
|
55
|
+
concurrency: z.number().int().min(1).default(4),
|
|
52
56
|
// API configuration (optional - can also use ~/.storysnap/credentials)
|
|
53
57
|
apiUrl: z.string().url().optional(),
|
|
54
58
|
apiToken: z.string().optional(),
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Story } from '@storysnap/shared';
|
|
2
|
+
/**
|
|
3
|
+
* Files that differ from the point where HEAD branched off `ref`: committed,
|
|
4
|
+
* staged, unstaged and untracked. Comparing against the merge base rather than
|
|
5
|
+
* `ref` itself keeps commits that landed on `ref` since then out of the list.
|
|
6
|
+
*
|
|
7
|
+
* Paths are relative to `cwd`, which matches Storybook's importPath as long as
|
|
8
|
+
* the CLI runs from the directory Storybook runs from.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getChangedFiles(ref?: string, cwd?: string): Promise<{
|
|
11
|
+
ref: string;
|
|
12
|
+
files: string[];
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* Keeps the stories whose story file, or a file next to it sharing its name,
|
|
16
|
+
* is among `changedFiles`. That catches edits to the component a story file
|
|
17
|
+
* covers, but not to anything it imports from further away.
|
|
18
|
+
*
|
|
19
|
+
* Stories the index gives no file path for are kept, since there is no way to
|
|
20
|
+
* tell whether they changed; `unknown` counts them.
|
|
21
|
+
*/
|
|
22
|
+
export declare function filterChangedStories(stories: Story[], changedFiles: string[]): {
|
|
23
|
+
stories: Story[];
|
|
24
|
+
unknown: number;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=changed.d.ts.map
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { posix } from 'node:path';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
const execFileAsync = promisify(execFile);
|
|
5
|
+
/** Used by `--changed` with no ref. CI checkouts often only have the remote one. */
|
|
6
|
+
const DEFAULT_REFS = ['main', 'origin/main'];
|
|
7
|
+
async function git(args, cwd) {
|
|
8
|
+
const { stdout } = await execFileAsync('git', args, { cwd, maxBuffer: 64 * 1024 * 1024 });
|
|
9
|
+
return stdout;
|
|
10
|
+
}
|
|
11
|
+
/** Parses `-z` output, which leaves unusual file names unquoted. */
|
|
12
|
+
function splitNul(output) {
|
|
13
|
+
return output.split('\0').filter(Boolean);
|
|
14
|
+
}
|
|
15
|
+
async function findMergeBase(refs, cwd) {
|
|
16
|
+
for (const ref of refs) {
|
|
17
|
+
try {
|
|
18
|
+
const base = (await git(['merge-base', ref, 'HEAD'], cwd)).trim();
|
|
19
|
+
return { ref, base };
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// Try the next candidate
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const tried = refs.map((ref) => `"${ref}"`).join(' or ');
|
|
26
|
+
throw new Error(`Could not compare against ${tried}. Make sure this is a git repository and the ref exists - ` +
|
|
27
|
+
'CI checkouts are often shallow, so fetch it first (e.g. "git fetch origin main") ' +
|
|
28
|
+
'or name one explicitly with --changed <ref>.');
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Files that differ from the point where HEAD branched off `ref`: committed,
|
|
32
|
+
* staged, unstaged and untracked. Comparing against the merge base rather than
|
|
33
|
+
* `ref` itself keeps commits that landed on `ref` since then out of the list.
|
|
34
|
+
*
|
|
35
|
+
* Paths are relative to `cwd`, which matches Storybook's importPath as long as
|
|
36
|
+
* the CLI runs from the directory Storybook runs from.
|
|
37
|
+
*/
|
|
38
|
+
export async function getChangedFiles(ref, cwd = process.cwd()) {
|
|
39
|
+
const { ref: resolvedRef, base } = await findMergeBase(ref ? [ref] : DEFAULT_REFS, cwd);
|
|
40
|
+
const [tracked, untracked] = await Promise.all([
|
|
41
|
+
git(['diff', '--name-only', '--relative', '-z', base], cwd),
|
|
42
|
+
git(['ls-files', '--others', '--exclude-standard', '-z'], cwd),
|
|
43
|
+
]);
|
|
44
|
+
return { ref: resolvedRef, files: [...splitNul(tracked), ...splitNul(untracked)] };
|
|
45
|
+
}
|
|
46
|
+
function normalizePath(path) {
|
|
47
|
+
return path.replace(/\\/g, '/').replace(/^\.\//, '');
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Groups a file with its siblings by directory and the name before the first
|
|
51
|
+
* dot, so Button.stories.tsx, Button.tsx and Button.module.css share a key.
|
|
52
|
+
*/
|
|
53
|
+
function siblingKey(path) {
|
|
54
|
+
const normalized = normalizePath(path);
|
|
55
|
+
const name = posix.basename(normalized).split('.')[0];
|
|
56
|
+
return `${posix.dirname(normalized)}/${name}`;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Keeps the stories whose story file, or a file next to it sharing its name,
|
|
60
|
+
* is among `changedFiles`. That catches edits to the component a story file
|
|
61
|
+
* covers, but not to anything it imports from further away.
|
|
62
|
+
*
|
|
63
|
+
* Stories the index gives no file path for are kept, since there is no way to
|
|
64
|
+
* tell whether they changed; `unknown` counts them.
|
|
65
|
+
*/
|
|
66
|
+
export function filterChangedStories(stories, changedFiles) {
|
|
67
|
+
const changed = new Set(changedFiles.map(siblingKey));
|
|
68
|
+
let unknown = 0;
|
|
69
|
+
const selected = stories.filter((story) => {
|
|
70
|
+
if (!story.componentPath) {
|
|
71
|
+
unknown++;
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return changed.has(siblingKey(story.componentPath));
|
|
75
|
+
});
|
|
76
|
+
return { stories: selected, unknown };
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=changed.js.map
|
package/dist/core/compare.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ export interface CompareOptions {
|
|
|
17
17
|
maxDiffPercent?: number;
|
|
18
18
|
storage: StorageProvider;
|
|
19
19
|
localSnapshotsDir?: string;
|
|
20
|
+
/** How many comparisons run at once; each downloads a baseline first. */
|
|
21
|
+
concurrency?: number;
|
|
20
22
|
}
|
|
21
23
|
export declare function compareScreenshot(screenshot: Screenshot, options: CompareOptions): Promise<ComparisonResult>;
|
|
22
24
|
export declare function compareAllScreenshots(screenshots: Map<string, Screenshot>, options: CompareOptions, onProgress?: (completed: number, total: number, key: string) => void): Promise<Map<string, ComparisonResult>>;
|
package/dist/core/compare.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import pixelmatch from 'pixelmatch';
|
|
2
2
|
import { PNG } from 'pngjs';
|
|
3
3
|
import { getStoryKey } from './storybook.js';
|
|
4
|
+
import { DEFAULT_CONCURRENCY, mapWithConcurrency } from './concurrency.js';
|
|
4
5
|
import { ensureDir } from '../storage/local.js';
|
|
5
6
|
import { writeFile } from 'node:fs/promises';
|
|
6
7
|
import { join } from 'node:path';
|
|
@@ -20,22 +21,23 @@ export async function compareScreenshot(screenshot, options) {
|
|
|
20
21
|
storyId: screenshot.storyId,
|
|
21
22
|
browser: screenshot.browser,
|
|
22
23
|
status: 'error',
|
|
23
|
-
errorMessage: 'Failed to capture screenshot',
|
|
24
|
+
errorMessage: screenshot.error ? `Failed to capture screenshot: ${screenshot.error}` : 'Failed to capture screenshot',
|
|
24
25
|
};
|
|
25
26
|
}
|
|
26
27
|
const threshold = options.threshold ?? DEFAULT_THRESHOLD;
|
|
27
28
|
const maxDiffPercent = options.maxDiffPercent ?? DEFAULT_MAX_DIFF_PERCENT;
|
|
28
|
-
// Try to fetch baseline from storage
|
|
29
|
-
const baselineBuffer = await options.storage.download(storyKey);
|
|
30
|
-
if (!baselineBuffer) {
|
|
31
|
-
// No baseline exists - this is a new story
|
|
32
|
-
return {
|
|
33
|
-
storyId: screenshot.storyId,
|
|
34
|
-
browser: screenshot.browser,
|
|
35
|
-
status: 'new',
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
29
|
try {
|
|
30
|
+
// Inside the try so a storage failure - an S3 timeout, say - errors this
|
|
31
|
+
// one story rather than rejecting every comparison in the run.
|
|
32
|
+
const baselineBuffer = await options.storage.download(storyKey);
|
|
33
|
+
if (!baselineBuffer) {
|
|
34
|
+
// No baseline exists - this is a new story
|
|
35
|
+
return {
|
|
36
|
+
storyId: screenshot.storyId,
|
|
37
|
+
browser: screenshot.browser,
|
|
38
|
+
status: 'new',
|
|
39
|
+
};
|
|
40
|
+
}
|
|
39
41
|
// Parse PNG images
|
|
40
42
|
const currentPng = PNG.sync.read(screenshot.buffer);
|
|
41
43
|
const baselinePng = PNG.sync.read(baselineBuffer);
|
|
@@ -126,14 +128,16 @@ export async function compareAllScreenshots(screenshots, options, onProgress) {
|
|
|
126
128
|
const results = new Map();
|
|
127
129
|
let completed = 0;
|
|
128
130
|
const total = screenshots.size;
|
|
129
|
-
|
|
131
|
+
// compareScreenshot turns its own failures into 'error' results, as the
|
|
132
|
+
// pool requires. Results are keyed, so finishing out of order is harmless.
|
|
133
|
+
await mapWithConcurrency([...screenshots], options.concurrency ?? DEFAULT_CONCURRENCY, async ([key, screenshot]) => {
|
|
130
134
|
const result = await compareScreenshot(screenshot, options);
|
|
131
135
|
results.set(key, result);
|
|
132
136
|
completed++;
|
|
133
137
|
if (onProgress) {
|
|
134
138
|
onProgress(completed, total, key);
|
|
135
139
|
}
|
|
136
|
-
}
|
|
140
|
+
});
|
|
137
141
|
return results;
|
|
138
142
|
}
|
|
139
143
|
//# sourceMappingURL=compare.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const DEFAULT_CONCURRENCY = 4;
|
|
2
|
+
/**
|
|
3
|
+
* Runs `fn` over every item with at most `limit` calls in flight, resolving
|
|
4
|
+
* once all have settled. `fn` is expected to handle its own errors.
|
|
5
|
+
*/
|
|
6
|
+
export declare function mapWithConcurrency<T>(items: readonly T[], limit: number, fn: (item: T) => Promise<void>): Promise<void>;
|
|
7
|
+
//# sourceMappingURL=concurrency.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const DEFAULT_CONCURRENCY = 4;
|
|
2
|
+
/**
|
|
3
|
+
* Runs `fn` over every item with at most `limit` calls in flight, resolving
|
|
4
|
+
* once all have settled. `fn` is expected to handle its own errors.
|
|
5
|
+
*/
|
|
6
|
+
export async function mapWithConcurrency(items, limit, fn) {
|
|
7
|
+
let next = 0;
|
|
8
|
+
const worker = async () => {
|
|
9
|
+
while (next < items.length) {
|
|
10
|
+
await fn(items[next++]);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
await Promise.all(Array.from({ length: Math.min(Math.max(1, limit), items.length) }, worker));
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=concurrency.js.map
|
|
@@ -10,6 +10,11 @@ export declare class ScreenshotEngine {
|
|
|
10
10
|
captureStories(stories: Story[], onProgress?: (completed: number, total: number, story: Story, browser: Browser) => void): Promise<Map<string, Screenshot>>;
|
|
11
11
|
private waitForStability;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* The first line of a capture error. Playwright appends a multi-line call log
|
|
15
|
+
* to its messages, which buries the actual reason when printed per story.
|
|
16
|
+
*/
|
|
17
|
+
export declare function describeCaptureError(error: unknown): string;
|
|
13
18
|
export declare function getScreenshotKey(storyId: string, browser: Browser): string;
|
|
14
19
|
export declare function parseScreenshotKey(key: string): {
|
|
15
20
|
storyId: string;
|
package/dist/core/screenshot.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { chromium, firefox, webkit } from 'playwright';
|
|
2
2
|
import { getStoryUrl } from './storybook.js';
|
|
3
|
+
import { DEFAULT_CONCURRENCY, mapWithConcurrency } from './concurrency.js';
|
|
3
4
|
const BROWSER_MAP = {
|
|
4
5
|
chromium,
|
|
5
6
|
firefox,
|
|
@@ -69,31 +70,34 @@ export class ScreenshotEngine {
|
|
|
69
70
|
}
|
|
70
71
|
async captureStories(stories, onProgress) {
|
|
71
72
|
const screenshots = new Map();
|
|
72
|
-
const
|
|
73
|
+
const tasks = stories.flatMap((story) => this.config.browsers.map((browserName) => ({ story, browserName })));
|
|
73
74
|
let completed = 0;
|
|
74
|
-
for
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
// Store error indicator - will be handled by comparison
|
|
83
|
-
screenshots.set(screenshotKey, {
|
|
84
|
-
storyId: story.id,
|
|
85
|
-
browser: browserName,
|
|
86
|
-
buffer: Buffer.alloc(0),
|
|
87
|
-
width: 0,
|
|
88
|
-
height: 0,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
completed++;
|
|
92
|
-
if (onProgress) {
|
|
93
|
-
onProgress(completed, totalTests, story, browserName);
|
|
94
|
-
}
|
|
75
|
+
// Most of a capture is spent waiting - for the network to go idle, for
|
|
76
|
+
// the settle delay - so several pages run at once. Each still gets a fresh
|
|
77
|
+
// page, keeping stories as isolated from one another as before.
|
|
78
|
+
await mapWithConcurrency(tasks, this.config.concurrency ?? DEFAULT_CONCURRENCY, async ({ story, browserName }) => {
|
|
79
|
+
const screenshotKey = getScreenshotKey(story.id, browserName);
|
|
80
|
+
try {
|
|
81
|
+
const screenshot = await this.captureStory(story, browserName);
|
|
82
|
+
screenshots.set(screenshotKey, screenshot);
|
|
95
83
|
}
|
|
96
|
-
|
|
84
|
+
catch (error) {
|
|
85
|
+
// An empty buffer marks the failure for the callers; the reason travels
|
|
86
|
+
// with it so they can say why rather than just that it failed.
|
|
87
|
+
screenshots.set(screenshotKey, {
|
|
88
|
+
storyId: story.id,
|
|
89
|
+
browser: browserName,
|
|
90
|
+
buffer: Buffer.alloc(0),
|
|
91
|
+
width: 0,
|
|
92
|
+
height: 0,
|
|
93
|
+
error: describeCaptureError(error),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
completed++;
|
|
97
|
+
if (onProgress) {
|
|
98
|
+
onProgress(completed, tasks.length, story, browserName);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
97
101
|
return screenshots;
|
|
98
102
|
}
|
|
99
103
|
async waitForStability(page) {
|
|
@@ -103,6 +107,14 @@ export class ScreenshotEngine {
|
|
|
103
107
|
await page.waitForTimeout(200);
|
|
104
108
|
}
|
|
105
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* The first line of a capture error. Playwright appends a multi-line call log
|
|
112
|
+
* to its messages, which buries the actual reason when printed per story.
|
|
113
|
+
*/
|
|
114
|
+
export function describeCaptureError(error) {
|
|
115
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
116
|
+
return message.split('\n')[0].trim() || 'Unknown capture error';
|
|
117
|
+
}
|
|
106
118
|
export function getScreenshotKey(storyId, browser) {
|
|
107
119
|
return `${storyId}::${browser}`;
|
|
108
120
|
}
|
package/dist/core/storybook.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Story, StorySnapConfig, Browser } from '@storysnap/shared';
|
|
2
2
|
export declare function discoverStories(config: StorySnapConfig): Promise<Story[]>;
|
|
3
|
+
export declare function filterStories(stories: Story[], include?: string[], exclude?: string[]): Story[];
|
|
3
4
|
export declare function getStoryUrl(baseUrl: string, storyId: string): string;
|
|
4
5
|
export declare function getStoryKey(storyId: string, browser?: Browser): string;
|
|
5
6
|
//# sourceMappingURL=storybook.d.ts.map
|
package/dist/core/storybook.js
CHANGED
|
@@ -85,7 +85,7 @@ function parseIndex(index) {
|
|
|
85
85
|
}
|
|
86
86
|
return stories;
|
|
87
87
|
}
|
|
88
|
-
function filterStories(stories, include, exclude) {
|
|
88
|
+
export function filterStories(stories, include, exclude) {
|
|
89
89
|
let filtered = stories;
|
|
90
90
|
if (include && include.length > 0) {
|
|
91
91
|
filtered = filtered.filter((story) => include.some((pattern) => matchPattern(story, pattern)));
|
package/package.json
CHANGED