Skip to content

Commit a84c2ad

Browse files
Merge pull request #30 from gemini-testing/TESTPLANE-357.autoscreenshotSelector
feat: add ability to specify autoscreenshotSelector globally
2 parents b93e1b3 + 3c14fe9 commit a84c2ad

File tree

8 files changed

+37
-19
lines changed

8 files changed

+37
-19
lines changed

src/config.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ describe("config", () => {
1919
enabled: false,
2020
storybookConfigDir: "custom-dir",
2121
autoScreenshots: false,
22+
autoscreenshotSelector: "foobar",
23+
autoScreenshotStorybookGlobals: { default: { theme: "dark" } },
2224
localport: 1234,
2325
remoteStorybookUrl: "http://localhost:3000",
2426
browserIds: ["chrome", "firefox"],

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export interface PluginConfig {
7777
enabled: boolean;
7878
storybookConfigDir: string;
7979
autoScreenshots: boolean;
80+
autoscreenshotSelector: string;
8081
autoScreenshotStorybookGlobals: Record<string, Record<string, unknown>>;
8182
localport: number;
8283
remoteStorybookUrl: string;
@@ -94,6 +95,7 @@ export function parseConfig(options: PluginPartialConfig): PluginConfig {
9495
enabled: booleanOption("enabled", true),
9596
storybookConfigDir: stringOption("storybookConfigDir", ".storybook"),
9697
autoScreenshots: booleanOption("autoScreenshots", true),
98+
autoscreenshotSelector: stringOption("autoscreenshotSelector", ""),
9799
autoScreenshotStorybookGlobals: optionalRecordOfRecordsOption("autoScreenshotStorybookGlobals", {}),
98100
localport: numberOption("localport", 6006),
99101
remoteStorybookUrl: stringOption("remoteStorybookUrl", ""),

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ function onTestplaneMaster(testplane: Testplane, config: PluginConfig): void {
8080

8181
const storyTestFiles = await buildStoryTestFiles(stories, {
8282
autoScreenshots: config.autoScreenshots,
83+
autoscreenshotSelector: config.autoscreenshotSelector,
8384
autoScreenshotStorybookGlobals: config.autoScreenshotStorybookGlobals,
8485
});
8586

src/storybook/story-test-runner/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function run(stories: StorybookStory[], opts: TestplaneOpts): void {
1818

1919
function createTestplaneTests(
2020
story: StorybookStoryExtended,
21-
{ autoScreenshots, autoScreenshotStorybookGlobals }: TestplaneOpts,
21+
{ autoScreenshots, autoscreenshotSelector, autoScreenshotStorybookGlobals }: TestplaneOpts,
2222
): void {
2323
nestedDescribe(story, () => {
2424
const rawAutoScreenshotGlobalSets = {
@@ -41,8 +41,9 @@ function createTestplaneTests(
4141
ctx.expect = globalThis.expect;
4242

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

45-
await autoScreenshotStep(ctx.browser, story.autoscreenshotSelector || result.rootSelector);
46+
await autoScreenshotStep(ctx.browser, selector);
4647
},
4748
);
4849
}

src/storybook/story-to-test/index.test.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ describe("storybook/story-to-test", () => {
1414

1515
const storyTestFiles = await buildStoryTestFiles([story], {
1616
autoScreenshots: true,
17+
autoscreenshotSelector: "foobar",
1718
autoScreenshotStorybookGlobals: {},
1819
});
1920

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

2324
it("should empty tests dir before writing tests", async () => {
24-
await buildStoryTestFiles([], { autoScreenshots: true, autoScreenshotStorybookGlobals: {} });
25+
await buildStoryTestFiles([], {
26+
autoScreenshots: true,
27+
autoscreenshotSelector: "foobar",
28+
autoScreenshotStorybookGlobals: {},
29+
});
2530

2631
expect(fs.emptyDir).toBeCalled();
2732
});
@@ -30,22 +35,24 @@ describe("storybook/story-to-test", () => {
3035
jest.spyOn(path, "resolve").mockImplementation((_, storyPath) => storyPath);
3136
const storyFirst = { importPath: "./story/path/story-first.js" } as StorybookStoryExtended;
3237
const storySecond = { importPath: "./story/path/story-second.js" } as StorybookStoryExtended;
33-
34-
const storyTestFiles = await buildStoryTestFiles([storyFirst, storySecond], {
38+
const opts = {
3539
autoScreenshots: true,
40+
autoscreenshotSelector: "foobar",
3641
autoScreenshotStorybookGlobals: { foo: { bar: "baz" } },
37-
});
42+
};
43+
44+
const storyTestFiles = await buildStoryTestFiles([storyFirst, storySecond], opts);
3845

3946
expect(writeStoryTestsFile).toBeCalledWith({
4047
testFile: "./story/path/story-first.js.testplane.js",
41-
opts: { autoScreenshots: true, autoScreenshotStorybookGlobals: { foo: { bar: "baz" } } },
4248
stories: [storyFirst],
49+
opts,
4350
});
4451

4552
expect(writeStoryTestsFile).toBeCalledWith({
4653
testFile: "./story/path/story-second.js.testplane.js",
47-
opts: { autoScreenshots: true, autoScreenshotStorybookGlobals: { foo: { bar: "baz" } } },
4854
stories: [storySecond],
55+
opts,
4956
});
5057

5158
expect(storyTestFiles).toEqual([

src/storybook/story-to-test/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ import os from "os";
33
import fs from "fs-extra";
44
import _ from "lodash";
55
import type { Test } from "testplane";
6-
import { writeStoryTestsFile } from "./write-tests-file";
6+
import { writeStoryTestsFile, TestplaneOpts } from "./write-tests-file";
77
import { STORYBOOK_TEST_DIRNAME } from "../../constants";
88
import type { StorybookStoryExtended } from "../get-stories";
99

10-
export interface TestplaneOpts {
11-
autoScreenshots: boolean;
12-
autoScreenshotStorybookGlobals: Record<string, Record<string, unknown>>;
13-
}
10+
export type { TestplaneOpts };
1411

1512
const testplaneTestNameSuffix = ".testplane.js";
1613

src/storybook/story-to-test/write-tests-file.test.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,29 @@ jest.mock("fs-extra", () => ({
1313

1414
describe("storybook/story-to-test/write-tests-file", () => {
1515
it("should write test file with correct content", async () => {
16-
const opts = { autoScreenshots: true, autoScreenshotStorybookGlobals: { foo: { bar: "baz" } } };
1716
const stories = [{ id: "foo" }, { id: "bar" }] as StorybookStoryExtended[];
17+
const storyTestRunnerPath = "/absolute/story/runner/path";
18+
const testplaneOpts = {
19+
autoScreenshots: true,
20+
autoscreenshotSelector: ".foobar",
21+
autoScreenshotStorybookGlobals: { foo: { bar: "baz" } },
22+
};
23+
1824
const testFile = "/absolute/test/path/file.testplane.js";
25+
1926
const expectedContents = `
20-
const stories = [{"id":"foo"},{"id":"bar"}];
21-
const storyTestRunnerPath = "/absolute/story/runner/path";
22-
const testplaneOpts = {"autoScreenshots":true,"autoScreenshotStorybookGlobals":{"foo":{"bar":"baz"}}};
27+
const stories = ${JSON.stringify(stories)};
28+
const storyTestRunnerPath = ${JSON.stringify(storyTestRunnerPath)};
29+
const testplaneOpts = ${JSON.stringify(testplaneOpts)};
2330
2431
require(storyTestRunnerPath).run(stories, testplaneOpts);
2532
`;
26-
jest.mocked(getStoryRunnerAbsoluteFilePath).mockReturnValue("/absolute/story/runner/path");
33+
jest.mocked(getStoryRunnerAbsoluteFilePath).mockReturnValue(storyTestRunnerPath);
2734

2835
await writeStoryTestsFile({
2936
testFile,
3037
stories,
31-
opts,
38+
opts: testplaneOpts,
3239
});
3340

3441
expect(getStoryRunnerAbsoluteFilePath).toHaveBeenCalled();

src/storybook/story-to-test/write-tests-file.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { StorybookStoryExtended } from "../get-stories";
55

66
export interface TestplaneOpts {
77
autoScreenshots: boolean;
8+
autoscreenshotSelector: string;
89
autoScreenshotStorybookGlobals: Record<string, Record<string, unknown>>;
910
}
1011

0 commit comments

Comments
 (0)