Skip to content

Commit 0a06dc0

Browse files
gregbergeclaude
andcommitted
refactor(storybook): share the browser suffix through @argos-ci/util
The rule that Chromium keeps the plain name and other browsers get ` browser-[<name>]` moves to `getBrowserSuffix` in `@argos-ci/util`, next to the viewport suffix, so `@argos-ci/vitest` can use the same definition. Storybook only puts its mode in front of it. Also turns the naming tests into tables and builds the plugin tests from one `configure()` helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b831868 commit 0a06dc0

6 files changed

Lines changed: 72 additions & 92 deletions

File tree

‎packages/storybook/src/utils/name.test.ts‎

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,15 @@ import { describe, expect, it } from "vitest";
22
import { getStoryScreenshotName } from "./name";
33

44
describe("getStoryScreenshotName", () => {
5-
it("keeps the plain name in Chromium", () => {
5+
it.each([
6+
[null, "chromium", "button--primary"],
7+
[null, null, "button--primary"],
8+
[null, "firefox", "button--primary browser-[firefox]"],
9+
["dark", "chromium", "button--primary mode-[dark]"],
10+
["dark", "webkit", "button--primary mode-[dark] browser-[webkit]"],
11+
])("mode %s in %s gives %j", (mode, browserName, expected) => {
612
expect(
7-
getStoryScreenshotName({
8-
name: "button--primary",
9-
mode: null,
10-
browserName: "chromium",
11-
}),
12-
).toBe("button--primary");
13-
});
14-
15-
it("keeps the plain name when the browser is unknown", () => {
16-
expect(
17-
getStoryScreenshotName({
18-
name: "button--primary",
19-
mode: null,
20-
browserName: null,
21-
}),
22-
).toBe("button--primary");
23-
});
24-
25-
it("names the browser outside Chromium", () => {
26-
expect(
27-
getStoryScreenshotName({
28-
name: "button--primary",
29-
mode: null,
30-
browserName: "firefox",
31-
}),
32-
).toBe("button--primary browser-[firefox]");
33-
});
34-
35-
it("names the mode", () => {
36-
expect(
37-
getStoryScreenshotName({
38-
name: "button--primary",
39-
mode: "dark",
40-
browserName: "chromium",
41-
}),
42-
).toBe("button--primary mode-[dark]");
43-
});
44-
45-
it("names the browser after the mode", () => {
46-
expect(
47-
getStoryScreenshotName({
48-
name: "button--primary/step",
49-
mode: "dark",
50-
browserName: "webkit",
51-
}),
52-
).toBe("button--primary/step mode-[dark] browser-[webkit]");
13+
getStoryScreenshotName({ name: "button--primary", mode, browserName }),
14+
).toBe(expected);
5315
});
5416
});
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
/**
2-
* Browser whose screenshots keep the plain story name. It is the default of
3-
* every Storybook runner, so existing baselines stay valid when another browser
4-
* is added to the run.
5-
*/
6-
const DEFAULT_BROWSER = "chromium";
1+
import { getBrowserSuffix } from "@argos-ci/util";
72

83
/**
94
* Name of a screenshot of a story. The mode and the browser are part of it, so
@@ -23,18 +18,11 @@ export function getStoryScreenshotName(input: {
2318
*/
2419
mode: string | null;
2520
/**
26-
* Browser running the story, like `chromium` or `firefox`, or `null` when it
27-
* can't be identified.
21+
* Browser running the story, or `null` when it can't be identified.
2822
*/
2923
browserName: string | null;
3024
}): string {
3125
const { name, mode, browserName } = input;
32-
let result = name;
33-
if (mode) {
34-
result += ` mode-[${mode}]`;
35-
}
36-
if (browserName && browserName !== DEFAULT_BROWSER) {
37-
result += ` browser-[${browserName}]`;
38-
}
39-
return result;
26+
const modeSuffix = mode ? ` mode-[${mode}]` : "";
27+
return `${name}${modeSuffix}${getBrowserSuffix(browserName)}`;
4028
}

‎packages/storybook/src/utils/screenshot.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ export type StorybookScreenshotContext<Handler extends Page | Frame> = {
2525
mode: "manual" | "automatic";
2626
name: string;
2727
/**
28-
* Browser running the story, like `chromium` or `firefox`, or `null` when it
29-
* can't be identified. Screenshots taken outside Chromium carry it in their
30-
* name, so each browser gets its own baselines.
28+
* Browser running the story, or `null` when it can't be identified.
3129
*/
3230
browserName: string | null;
3331
playwrightLibraries: string[];

‎packages/storybook/src/vitest-plugin.test.ts‎

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,55 @@ import { describe, expect, it } from "vitest";
22
import { ArgosReporter } from "@argos-ci/vitest/plugin";
33
import { argosVitestPlugin } from "./vitest-plugin";
44

5-
function configure(setupFiles: string[]) {
6-
const plugin = argosVitestPlugin({ uploadToArgos: false }) as any;
7-
const project = { config: { setupFiles } };
8-
plugin.configureVitest({ vitest: { config: { reporters: [] } }, project });
9-
return project.config.setupFiles as string[];
5+
const USER_SETUP_FILE = "/project/.storybook/vitest.setup.ts";
6+
7+
/**
8+
* Run the plugin's `configureVitest` hook for `projects` projects of the same
9+
* Vitest run, the way Vitest does for each browser of `browser.instances`.
10+
*/
11+
function configure(
12+
options: { uploadToArgos?: boolean; projects?: number } = {},
13+
) {
14+
const { uploadToArgos = false, projects = 1 } = options;
15+
const plugin = argosVitestPlugin({ uploadToArgos }) as any;
16+
const vitest = { config: { reporters: [] as unknown[] } };
17+
// Each project has its own setup files; all of them end up the same.
18+
let setupFiles: string[] = [];
19+
for (let index = 0; index < projects; index++) {
20+
setupFiles = [USER_SETUP_FILE];
21+
plugin.configureVitest({ vitest, project: { config: { setupFiles } } });
22+
}
23+
return { setupFiles, reporters: vitest.config.reporters };
1024
}
1125

1226
describe("argosVitestPlugin", () => {
1327
it("registers the channel setup ahead of the user's setup files", () => {
14-
const setupFiles = configure(["/project/.storybook/vitest.setup.ts"]);
28+
const { setupFiles } = configure();
1529

1630
// Addon preview modules capture a channel when the user's setup file
1731
// imports them, so ours has to install one first.
1832
expect(setupFiles[0]).toMatch(/vitest-setup-channel-file\.mjs$/);
19-
expect(setupFiles).toContain("/project/.storybook/vitest.setup.ts");
33+
expect(setupFiles).toContain(USER_SETUP_FILE);
2034
});
2135

2236
it("registers the screenshot setup after the user's setup files", () => {
23-
const setupFiles = configure(["/project/.storybook/vitest.setup.ts"]);
37+
const { setupFiles } = configure();
2438

2539
// `afterEach` hooks run in reverse registration order, so registering last
2640
// is what makes the screenshot happen before Storybook unmounts the story.
2741
expect(setupFiles.at(-1)).toMatch(/vitest-setup-file\.mjs$/);
2842
});
2943

3044
it("adds the Argos reporter once for all the browser instances", () => {
31-
const plugin = argosVitestPlugin({ uploadToArgos: true }) as any;
32-
const vitest = { config: { reporters: [] as unknown[] } };
33-
// Vitest runs each browser of `browser.instances` as a project of its own,
34-
// and calls `configureVitest` for each of them.
35-
for (const browser of ["chromium", "firefox", "webkit"]) {
36-
plugin.configureVitest({
37-
vitest,
38-
project: { name: browser, config: { setupFiles: [] } },
39-
});
40-
}
41-
42-
expect(vitest.config.reporters).toHaveLength(1);
43-
expect(vitest.config.reporters[0]).toBeInstanceOf(ArgosReporter);
45+
const { reporters } = configure({ uploadToArgos: true, projects: 3 });
46+
47+
expect(reporters).toHaveLength(1);
48+
expect(reporters[0]).toBeInstanceOf(ArgosReporter);
4449
});
4550

4651
it("adds no reporter when not uploading to Argos", () => {
47-
const plugin = argosVitestPlugin({ uploadToArgos: false }) as any;
48-
const vitest = { config: { reporters: [] as unknown[] } };
49-
plugin.configureVitest({ vitest, project: { config: { setupFiles: [] } } });
52+
const { reporters } = configure();
5053

51-
expect(vitest.config.reporters).toHaveLength(0);
54+
expect(reporters).toHaveLength(0);
5255
});
5356
});

‎packages/util/src/name.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { describe, expect, it } from "vitest";
2-
import { getScreenshotName } from "./name";
2+
import { getBrowserSuffix, getScreenshotName } from "./name";
3+
4+
describe("getBrowserSuffix", () => {
5+
it.each([
6+
["chromium", ""],
7+
[null, ""],
8+
[undefined, ""],
9+
["firefox", " browser-[firefox]"],
10+
["webkit", " browser-[webkit]"],
11+
])("returns %j for %s", (browserName, expected) => {
12+
expect(getBrowserSuffix(browserName)).toBe(expected);
13+
});
14+
});
315

416
describe("getScreenshotName", () => {
517
it("returns the name unchanged if no options are provided and name is safe", () => {

‎packages/util/src/name.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ export function getScreenshotName(
1212
);
1313
}
1414

15+
/**
16+
* Browser whose screenshots keep their plain name. It is the default of every
17+
* test runner, so existing baselines stay valid when another browser is added.
18+
*/
19+
const DEFAULT_BROWSER = "chromium";
20+
21+
/**
22+
* Suffix that keeps apart the screenshots of each browser, when the same tests
23+
* run in several of them: ` browser-[firefox]`, or `""` for Chromium or an
24+
* unknown browser.
25+
*/
26+
export function getBrowserSuffix(browserName: string | null | undefined) {
27+
return browserName && browserName !== DEFAULT_BROWSER
28+
? ` browser-[${browserName}]`
29+
: "";
30+
}
31+
1532
/**
1633
* Sanitize a path to be safe on all OSes.
1734
*/

0 commit comments

Comments
 (0)