Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/storybook/src/test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export async function argosScreenshot(
{
mode: "automatic",
name: storyContext.id,
// `--browsers` runs every story once per browser. A persistent context
// has no browser to read it from.
browserName: page.context().browser()?.browserType().name() ?? null,
playwrightLibraries: ["@storybook/test-runner"],
beforeScreenshot: async ({ handler }) => {
await waitForPageReady(handler);
Expand Down
16 changes: 16 additions & 0 deletions packages/storybook/src/utils/name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { getStoryScreenshotName } from "./name";

describe("getStoryScreenshotName", () => {
it.each([
[null, "chromium", "button--primary"],
[null, null, "button--primary"],
[null, "firefox", "button--primary browser-[firefox]"],
["dark", "chromium", "button--primary mode-[dark]"],
["dark", "webkit", "button--primary mode-[dark] browser-[webkit]"],
])("mode %s in %s gives %j", (mode, browserName, expected) => {
expect(
getStoryScreenshotName({ name: "button--primary", mode, browserName }),
).toBe(expected);
});
});
28 changes: 28 additions & 0 deletions packages/storybook/src/utils/name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getBrowserSuffix } from "@argos-ci/util";

/**
* Name of a screenshot of a story. The mode and the browser are part of it, so
* each mode and each browser gets its own baselines.
*
* @example
* getStoryScreenshotName({ name: "button--primary", mode: "dark", browserName: "firefox" })
* // => "button--primary mode-[dark] browser-[firefox]"
*/
export function getStoryScreenshotName(input: {
/**
* Base name of the screenshot, like the story id.
*/
name: string;
/**
* Argos mode the screenshot is taken in, or `null` outside a mode.
*/
mode: string | null;
/**
* Browser running the story, or `null` when it can't be identified.
*/
browserName: string | null;
}): string {
const { name, mode, browserName } = input;
const modeSuffix = mode ? ` mode-[${mode}]` : "";
return `${name}${modeSuffix}${getBrowserSuffix(browserName)}`;
}
27 changes: 15 additions & 12 deletions packages/storybook/src/utils/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import type { Frame, Page, ViewportSize } from "playwright";
import { getArgosStorybookVersion } from "./metadata";
import { getStoryMetadata } from "./storyMetadata";
import { getStoryScreenshotName } from "./name";
import {
getArgosParameters,
getDefaultViewport,
Expand All @@ -23,6 +24,10 @@ export type StorybookScreenshotContext<Handler extends Page | Frame> = {
*/
mode: "manual" | "automatic";
name: string;
/**
* Browser running the story, or `null` when it can't be identified.
*/
browserName: string | null;
playwrightLibraries: string[];
test?: MetadataConfig["test"];
setViewportSize: (
Expand Down Expand Up @@ -110,7 +115,6 @@ export async function storybookArgosScreenshot<Handler extends Page | Frame>(
context,
metadata,
options: argosOptions,
suffix: getModeSuffix(name),
mode: name,
globals: {
...context.story.globals,
Expand Down Expand Up @@ -164,7 +168,11 @@ export async function storybookArgosScreenshot<Handler extends Page | Frame>(

await argosPlaywrightScreenshot(
handler,
composeName(context.name, getModeSuffix(currentMode)),
getStoryScreenshotName({
name: context.name,
mode: currentMode,
browserName: context.browserName,
}),
options,
);
}
Expand Down Expand Up @@ -283,7 +291,6 @@ async function runHooksAndScreenshot<Handler extends Page | Frame>(args: {
options: ArgosScreenshotOptions;
globals: StorybookGlobals;
mode: string | null;
suffix?: string;
metadata: MetadataConfig;
}) {
const { handler, context, options, globals, metadata, mode } = args;
Expand Down Expand Up @@ -313,15 +320,11 @@ async function runHooksAndScreenshot<Handler extends Page | Frame>(args: {

return argosPlaywrightScreenshot(
handler,
composeName(context.name, args.suffix),
getStoryScreenshotName({
name: context.name,
mode,
browserName: context.browserName,
}),
options,
);
}

function getModeSuffix(mode: string | null) {
return mode ? ` mode-[${mode}]` : "";
}

function composeName(name: string, suffix: string | undefined) {
return name + (suffix ?? "");
}
44 changes: 36 additions & 8 deletions packages/storybook/src/vitest-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
import { describe, expect, it } from "vitest";
import { ArgosReporter } from "@argos-ci/vitest/plugin";
import { argosVitestPlugin } from "./vitest-plugin";

function configure(setupFiles: string[]) {
const plugin = argosVitestPlugin({ uploadToArgos: false }) as any;
const project = { config: { setupFiles } };
plugin.configureVitest({ vitest: { config: { reporters: [] } }, project });
return project.config.setupFiles as string[];
const USER_SETUP_FILE = "/project/.storybook/vitest.setup.ts";

/**
* Run the plugin's `configureVitest` hook for `projects` projects of the same
* Vitest run, the way Vitest does for each browser of `browser.instances`.
*/
function configure(
options: { uploadToArgos?: boolean; projects?: number } = {},
) {
const { uploadToArgos = false, projects = 1 } = options;
const plugin = argosVitestPlugin({ uploadToArgos }) as any;
const vitest = { config: { reporters: [] as unknown[] } };
// Each project has its own setup files; all of them end up the same.
let setupFiles: string[] = [];
for (let index = 0; index < projects; index++) {
setupFiles = [USER_SETUP_FILE];
plugin.configureVitest({ vitest, project: { config: { setupFiles } } });
}
return { setupFiles, reporters: vitest.config.reporters };
}

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

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

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

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

it("adds the Argos reporter once for all the browser instances", () => {
const { reporters } = configure({ uploadToArgos: true, projects: 3 });

expect(reporters).toHaveLength(1);
expect(reporters[0]).toBeInstanceOf(ArgosReporter);
});

it("adds no reporter when not uploading to Argos", () => {
const { reporters } = configure();

expect(reporters).toHaveLength(0);
});
});
15 changes: 11 additions & 4 deletions packages/storybook/src/vitest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export const createArgosScreenshotCommand = (
frame,
{
...testContext,
// Each browser of `browser.instances` runs as a project of its own,
// whose config names its browser.
browserName: ctx.project.config.browser.name || null,
playwrightLibraries: ["@storybook/addon-vitest"],
setViewportSize: async (size) => {
await setIframeViewportSize(ctx, size, {
Expand Down Expand Up @@ -134,6 +137,9 @@ export function argosVitestPlugin(options?: ArgosVitestPluginOptions): Plugin {
const distDir = dirname(fileURLToPath(import.meta.url));
const setupFile = resolve(distDir, "./vitest-setup-file.mjs");
const channelSetupFile = resolve(distDir, "./vitest-setup-channel-file.mjs");
const reporter = uploadToArgos
? new ArgosReporter({ ...otherOptions, root })
: null;
return {
name: "@argos-ci/storybook/vitest-plugin",
configureVitest({ vitest, project }) {
Expand All @@ -145,10 +151,11 @@ export function argosVitestPlugin(options?: ArgosVitestPluginOptions): Plugin {
// unmounts the story.
project.config.setupFiles.push(setupFile);

if (uploadToArgos) {
vitest.config.reporters.push(
new ArgosReporter({ ...otherOptions, root }),
);
// Each browser of `browser.instances` is a project of its own, and this
// hook runs for every project: add the reporter only once, or the
// screenshots would be uploaded once per browser.
if (reporter && !vitest.config.reporters.includes(reporter)) {
vitest.config.reporters.push(reporter);
}
},
config() {
Expand Down
14 changes: 13 additions & 1 deletion packages/util/src/name.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { describe, expect, it } from "vitest";
import { getScreenshotName } from "./name";
import { getBrowserSuffix, getScreenshotName } from "./name";

describe("getBrowserSuffix", () => {
it.each([
["chromium", ""],
[null, ""],
[undefined, ""],
["firefox", " browser-[firefox]"],
["webkit", " browser-[webkit]"],
])("returns %j for %s", (browserName, expected) => {
expect(getBrowserSuffix(browserName)).toBe(expected);
});
});

describe("getScreenshotName", () => {
it("returns the name unchanged if no options are provided and name is safe", () => {
Expand Down
17 changes: 17 additions & 0 deletions packages/util/src/name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ export function getScreenshotName(
);
}

/**
* Browser whose screenshots keep their plain name. It is the default of every
* test runner, so existing baselines stay valid when another browser is added.
*/
const DEFAULT_BROWSER = "chromium";

/**
* Suffix that keeps apart the screenshots of each browser, when the same tests
* run in several of them: ` browser-[firefox]`, or `""` for Chromium or an
* unknown browser.
*/
export function getBrowserSuffix(browserName: string | null | undefined) {
return browserName && browserName !== DEFAULT_BROWSER
? ` browser-[${browserName}]`
: "";
}

/**
* Sanitize a path to be safe on all OSes.
*/
Expand Down
Loading