Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to specify autoscreenshotSelector globally #30

Merged
merged 1 commit into from
Dec 5, 2024
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
2 changes: 2 additions & 0 deletions src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe("config", () => {
enabled: false,
storybookConfigDir: "custom-dir",
autoScreenshots: false,
autoscreenshotSelector: "foobar",
autoScreenshotStorybookGlobals: { default: { theme: "dark" } },
localport: 1234,
remoteStorybookUrl: "http://localhost:3000",
browserIds: ["chrome", "firefox"],
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface PluginConfig {
enabled: boolean;
storybookConfigDir: string;
autoScreenshots: boolean;
autoscreenshotSelector: string;
autoScreenshotStorybookGlobals: Record<string, Record<string, unknown>>;
localport: number;
remoteStorybookUrl: string;
Expand All @@ -94,6 +95,7 @@ export function parseConfig(options: PluginPartialConfig): PluginConfig {
enabled: booleanOption("enabled", true),
storybookConfigDir: stringOption("storybookConfigDir", ".storybook"),
autoScreenshots: booleanOption("autoScreenshots", true),
autoscreenshotSelector: stringOption("autoscreenshotSelector", ""),
autoScreenshotStorybookGlobals: optionalRecordOfRecordsOption("autoScreenshotStorybookGlobals", {}),
localport: numberOption("localport", 6006),
remoteStorybookUrl: stringOption("remoteStorybookUrl", ""),
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function onTestplaneMaster(testplane: Testplane, config: PluginConfig): void {

const storyTestFiles = await buildStoryTestFiles(stories, {
autoScreenshots: config.autoScreenshots,
autoscreenshotSelector: config.autoscreenshotSelector,
autoScreenshotStorybookGlobals: config.autoScreenshotStorybookGlobals,
});

Expand Down
5 changes: 3 additions & 2 deletions src/storybook/story-test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function run(stories: StorybookStory[], opts: TestplaneOpts): void {

function createTestplaneTests(
story: StorybookStoryExtended,
{ autoScreenshots, autoScreenshotStorybookGlobals }: TestplaneOpts,
{ autoScreenshots, autoscreenshotSelector, autoScreenshotStorybookGlobals }: TestplaneOpts,
): void {
nestedDescribe(story, () => {
const rawAutoScreenshotGlobalSets = {
Expand All @@ -41,8 +41,9 @@ function createTestplaneTests(
ctx.expect = globalThis.expect;

const result = await openStoryStep(ctx.browser, story, globals);
const selector = story.autoscreenshotSelector || autoscreenshotSelector || result.rootSelector;

await autoScreenshotStep(ctx.browser, story.autoscreenshotSelector || result.rootSelector);
await autoScreenshotStep(ctx.browser, selector);
},
);
}
Expand Down
19 changes: 13 additions & 6 deletions src/storybook/story-to-test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ describe("storybook/story-to-test", () => {

const storyTestFiles = await buildStoryTestFiles([story], {
autoScreenshots: true,
autoscreenshotSelector: "foobar",
autoScreenshotStorybookGlobals: {},
});

expect(storyTestFiles).toEqual(["/tmpdir/testplane-storybook-autogenerated/story/path/story.js.testplane.js"]);
});

it("should empty tests dir before writing tests", async () => {
await buildStoryTestFiles([], { autoScreenshots: true, autoScreenshotStorybookGlobals: {} });
await buildStoryTestFiles([], {
autoScreenshots: true,
autoscreenshotSelector: "foobar",
autoScreenshotStorybookGlobals: {},
});

expect(fs.emptyDir).toBeCalled();
});
Expand All @@ -30,22 +35,24 @@ describe("storybook/story-to-test", () => {
jest.spyOn(path, "resolve").mockImplementation((_, storyPath) => storyPath);
const storyFirst = { importPath: "./story/path/story-first.js" } as StorybookStoryExtended;
const storySecond = { importPath: "./story/path/story-second.js" } as StorybookStoryExtended;

const storyTestFiles = await buildStoryTestFiles([storyFirst, storySecond], {
const opts = {
autoScreenshots: true,
autoscreenshotSelector: "foobar",
autoScreenshotStorybookGlobals: { foo: { bar: "baz" } },
});
};

const storyTestFiles = await buildStoryTestFiles([storyFirst, storySecond], opts);

expect(writeStoryTestsFile).toBeCalledWith({
testFile: "./story/path/story-first.js.testplane.js",
opts: { autoScreenshots: true, autoScreenshotStorybookGlobals: { foo: { bar: "baz" } } },
stories: [storyFirst],
opts,
});

expect(writeStoryTestsFile).toBeCalledWith({
testFile: "./story/path/story-second.js.testplane.js",
opts: { autoScreenshots: true, autoScreenshotStorybookGlobals: { foo: { bar: "baz" } } },
stories: [storySecond],
opts,
});

expect(storyTestFiles).toEqual([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where the test in which you check that autoscreenshotSelector from story has more priority then autoscreenshotSelector from config?

Copy link
Member Author

@KuznetsovRoman KuznetsovRoman Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can not be tested with unit tests, as this code is inside story-test-runner, so the file just generates testplane tests (so its just a file with auto-executed function, which generates its and describes)

Expand Down
7 changes: 2 additions & 5 deletions src/storybook/story-to-test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import os from "os";
import fs from "fs-extra";
import _ from "lodash";
import type { Test } from "testplane";
import { writeStoryTestsFile } from "./write-tests-file";
import { writeStoryTestsFile, TestplaneOpts } from "./write-tests-file";
import { STORYBOOK_TEST_DIRNAME } from "../../constants";
import type { StorybookStoryExtended } from "../get-stories";

export interface TestplaneOpts {
autoScreenshots: boolean;
autoScreenshotStorybookGlobals: Record<string, Record<string, unknown>>;
}
export type { TestplaneOpts };

const testplaneTestNameSuffix = ".testplane.js";

Expand Down
19 changes: 13 additions & 6 deletions src/storybook/story-to-test/write-tests-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,29 @@ jest.mock("fs-extra", () => ({

describe("storybook/story-to-test/write-tests-file", () => {
it("should write test file with correct content", async () => {
const opts = { autoScreenshots: true, autoScreenshotStorybookGlobals: { foo: { bar: "baz" } } };
const stories = [{ id: "foo" }, { id: "bar" }] as StorybookStoryExtended[];
const storyTestRunnerPath = "/absolute/story/runner/path";
const testplaneOpts = {
autoScreenshots: true,
autoscreenshotSelector: ".foobar",
autoScreenshotStorybookGlobals: { foo: { bar: "baz" } },
};

const testFile = "/absolute/test/path/file.testplane.js";

const expectedContents = `
const stories = [{"id":"foo"},{"id":"bar"}];
const storyTestRunnerPath = "/absolute/story/runner/path";
const testplaneOpts = {"autoScreenshots":true,"autoScreenshotStorybookGlobals":{"foo":{"bar":"baz"}}};
const stories = ${JSON.stringify(stories)};
const storyTestRunnerPath = ${JSON.stringify(storyTestRunnerPath)};
const testplaneOpts = ${JSON.stringify(testplaneOpts)};

require(storyTestRunnerPath).run(stories, testplaneOpts);
`;
jest.mocked(getStoryRunnerAbsoluteFilePath).mockReturnValue("/absolute/story/runner/path");
jest.mocked(getStoryRunnerAbsoluteFilePath).mockReturnValue(storyTestRunnerPath);

await writeStoryTestsFile({
testFile,
stories,
opts,
opts: testplaneOpts,
});

expect(getStoryRunnerAbsoluteFilePath).toHaveBeenCalled();
Expand Down
1 change: 1 addition & 0 deletions src/storybook/story-to-test/write-tests-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { StorybookStoryExtended } from "../get-stories";

export interface TestplaneOpts {
autoScreenshots: boolean;
autoscreenshotSelector: string;
autoScreenshotStorybookGlobals: Record<string, Record<string, unknown>>;
}

Expand Down