-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add ability to override testplane storyfile configs #33
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,28 +2,88 @@ import { extendStoriesFromStoryFile } from "./extend-stories"; | |
| import { StorybookStory } from "./types"; | ||
|
|
||
| describe("storybook/story-test-runner/extend-stories", () => { | ||
| it("should log warning failed storyfile import", async () => { | ||
| const mkRequireStub_ = ( | ||
| impl?: Parameters<(typeof jest)["fn"]>[0], | ||
| ): jest.MockedFunction<(typeof globalThis)["require"]> => { | ||
| return jest.fn(impl) as unknown as jest.MockedFunction<(typeof globalThis)["require"]>; | ||
| }; | ||
|
|
||
| it("should log warning failed storyfile import", () => { | ||
| jest.spyOn(console, "warn").mockImplementation(jest.fn); | ||
| const requireFn = mkRequireStub_(() => { | ||
| throw new Error("some error message"); | ||
| }); | ||
| const stories = [{ name: "foo", absolutePath: "not/existing.ts" }] as StorybookStory[]; | ||
|
|
||
| extendStoriesFromStoryFile(stories); | ||
| extendStoriesFromStoryFile(stories, { requireFn }); | ||
|
|
||
| const expectedMsg = [ | ||
| '"testplane" section is ignored in storyfile "not/existing.ts", because the file could not be read:', | ||
| "Error: Cannot find module 'not/existing.ts' from 'src/storybook/story-test-runner/extend-stories.ts' ", | ||
| "Error: some error message ", | ||
| "There could be other story files. ", | ||
| "Set 'TESTPLANE_STORYBOOK_DISABLE_STORY_REQUIRE_WARNING' environment variable to hide this warning", | ||
| ].join("\n"); | ||
| expect(console.warn).toBeCalledWith(expectedMsg); | ||
| }); | ||
|
|
||
| it("should fallback, when could not read story file", async () => { | ||
| it("should fallback, when could not read story file", () => { | ||
| const requireFn = mkRequireStub_(() => { | ||
| throw new Error("file does not exist"); | ||
| }); | ||
| const stories = [{ name: "foo", absolutePath: "not/existing.js" }] as StorybookStory[]; | ||
|
|
||
| const extendedStories = extendStoriesFromStoryFile(stories); | ||
| const extendedStories = extendStoriesFromStoryFile(stories, { requireFn }); | ||
|
|
||
| expect(extendedStories[0].skip).toBe(false); | ||
| expect(extendedStories[0].assertViewOpts).toEqual({}); | ||
| expect(extendedStories[0].browserIds).toBe(null); | ||
| }); | ||
|
|
||
| it("should overlay story configs / file confifs / default configs", () => { | ||
|
||
| const customTests = {}; | ||
| const defaultExport = { | ||
| testplane: { browserIds: ["firefox"] }, | ||
| testplaneConfig: { | ||
| skip: true, | ||
| browserIds: ["chrome"], | ||
| assertViewOpts: { tolerance: 10, ignoreDiffPixelCount: 10 }, | ||
| autoScreenshotStorybookGlobals: { dark: { theme: "dark" } }, | ||
| }, | ||
| }; | ||
| const fooExport = { | ||
| testplane: customTests, | ||
| testplaneConfig: { skip: false, autoScreenshots: false, assertViewOpts: { ignoreElements: ["foobar"] } }, | ||
| }; | ||
| const requireFn = mkRequireStub_().mockReturnValue({ default: defaultExport, foo: fooExport }); | ||
| const stories = [{ name: "foo", absolutePath: "not/existing.js" }] as StorybookStory[]; | ||
|
|
||
| const extendedStories = extendStoriesFromStoryFile(stories, { requireFn }); | ||
|
|
||
| expect(extendedStories).toEqual([ | ||
| { | ||
| name: "foo", | ||
| absolutePath: "not/existing.js", | ||
| extraTests: {}, | ||
| skip: false, | ||
| browserIds: ["chrome"], | ||
| assertViewOpts: { | ||
| ignoreElements: ["foobar"], | ||
| tolerance: 10, | ||
| ignoreDiffPixelCount: 10, | ||
| }, | ||
| autoScreenshots: false, | ||
| autoscreenshotSelector: null, | ||
| autoScreenshotStorybookGlobals: { dark: { theme: "dark" } }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("should fallback reading story configs from deprecated testplane property", () => { | ||
| const stories = [{ name: "foo", absolutePath: "not/existing.js" }] as StorybookStory[]; | ||
| const requireFn = mkRequireStub_().mockReturnValue({ default: { testplane: { skip: true } }, foo: {} }); | ||
|
|
||
| const extendedStories = extendStoriesFromStoryFile(stories, { requireFn }); | ||
|
|
||
| expect(extendedStories).toMatchObject([{ name: "foo", skip: true }]); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import TypedModule from "module"; | ||
| import { extractInheritedValue, inheritValue } from "./inheritable-values"; | ||
| import type { TestplaneMetaConfig, TestplaneStoryConfig } from "../../types"; | ||
| import type { StorybookStory, StorybookStoryExtended } from "./types"; | ||
| import type { StorybookStory, StorybookStoryExtraProperties, StorybookStoryExtended } from "./types"; | ||
|
|
||
| const Module = TypedModule as any; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
|
|
||
|
|
@@ -12,45 +13,60 @@ type StoryFile = { default: TestplaneMetaConfig } & Record<string, TestplaneStor | |
|
|
||
| let loggedStoryFileRequireError = Boolean(process.env.TESTPLANE_STORYBOOK_DISABLE_STORY_REQUIRE_WARNING); | ||
|
|
||
| export function extendStoriesFromStoryFile(stories: StorybookStory[]): StorybookStoryExtended[] { | ||
| export function extendStoriesFromStoryFile( | ||
| stories: StorybookStory[], | ||
| { requireFn = require } = {}, | ||
| ): StorybookStoryExtended[] { | ||
| const storiesMap = getStoriesMap(stories); | ||
| const storyPath = stories[0].absolutePath; | ||
| const storyFile = getStoryFile(storyPath); | ||
| const storyFile = getStoryFile(storyPath, { requireFn }); | ||
| const withStoryFileExtendedStories = stories as StorybookStoryExtended[]; | ||
|
|
||
| if (!storyFile) { | ||
| return withStoryFileExtendedStories.map(story => { | ||
| story.skip = false; | ||
| story.assertViewOpts = {}; | ||
| story.browserIds = null; | ||
| story.autoScreenshotStorybookGlobals = {}; | ||
| const storyExtendedBaseConfig = { | ||
| skip: false, | ||
| browserIds: null, | ||
| assertViewOpts: {}, | ||
| autoScreenshots: null, | ||
| autoscreenshotSelector: null, | ||
| autoScreenshotStorybookGlobals: {}, | ||
| extraTests: null, | ||
| } satisfies StorybookStoryExtraProperties; | ||
|
|
||
| return story; | ||
| }); | ||
| if (!storyFile) { | ||
| return withStoryFileExtendedStories.map(story => Object.assign(story, storyExtendedBaseConfig)); | ||
| } | ||
|
|
||
| for (const storyName in storyFile) { | ||
| const storyTestplaneDefaultConfigs = storyFile.default?.testplaneConfig || storyFile.default?.testplane || {}; | ||
|
|
||
| for (const storyName of Object.keys(storyFile)) { | ||
| if (storyName === "default") { | ||
| withStoryFileExtendedStories.forEach(story => { | ||
| const testplaneStoryOpts = storyFile[storyName].testplane || {}; | ||
| continue; | ||
| } | ||
|
|
||
| story.skip = testplaneStoryOpts.skip || false; | ||
| story.assertViewOpts = testplaneStoryOpts.assertViewOpts || {}; | ||
| story.browserIds = testplaneStoryOpts.browserIds || null; | ||
| story.autoscreenshotSelector = testplaneStoryOpts.autoscreenshotSelector || null; | ||
| story.autoScreenshotStorybookGlobals = testplaneStoryOpts.autoScreenshotStorybookGlobals || {}; | ||
| }); | ||
| const storyMapKey = getStoryNameId(storyName); | ||
|
|
||
| if (!storiesMap.has(storyMapKey)) { | ||
| continue; | ||
| } | ||
|
|
||
| const storyMapKey = getStoryNameId(storyName); | ||
| const storyTestlaneConfigs = storyFile[storyName].testplaneConfig || {}; | ||
| const story = storiesMap.get(storyMapKey) as StorybookStoryExtended; | ||
|
|
||
| if (storiesMap.has(storyMapKey) && storyFile[storyName].testplane) { | ||
| const story = storiesMap.get(storyMapKey) as StorybookStoryExtended; | ||
| Object.assign(story, storyExtendedBaseConfig, storyTestplaneDefaultConfigs, storyTestlaneConfigs, { | ||
| extraTests: storyFile[storyName].testplane || null, | ||
| }); | ||
|
|
||
| story.extraTests = storyFile[storyName].testplane; | ||
| } | ||
| story.assertViewOpts = extractInheritedValue( | ||
| inheritValue(storyTestplaneDefaultConfigs.assertViewOpts, storyTestlaneConfigs.assertViewOpts), | ||
| storyExtendedBaseConfig.assertViewOpts, | ||
| ); | ||
|
|
||
| story.autoScreenshotStorybookGlobals = | ||
| inheritValue( | ||
| storyExtendedBaseConfig.autoScreenshotStorybookGlobals, | ||
| storyTestplaneDefaultConfigs.autoScreenshotStorybookGlobals, | ||
| storyTestlaneConfigs.autoScreenshotStorybookGlobals, | ||
| ) || {}; | ||
|
Comment on lines
+64
to
+69
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, when we have multiple layers of overriding, its important that these values can't be mutated. With "autoScreenshotStorybookGlobals can be a function" (which is required to be able to overwrite without overriding), it can no longer be done with just "Object.assign", so i made "./inheritable-values.ts" module, which resolves the problem |
||
| } | ||
|
|
||
| return withStoryFileExtendedStories; | ||
|
|
@@ -74,13 +90,13 @@ function getStoryNameId(storyName: string): string { | |
| return storyName.replace(nonAsciiWordRegExp, "").toLowerCase(); | ||
| } | ||
|
|
||
| function getStoryFile(storyPath: string): StoryFile | null { | ||
| function getStoryFile(storyPath: string, { requireFn = require } = {}): StoryFile | null { | ||
| const unmockFn = mockLoaders({ except: storyPath }); | ||
|
|
||
| let storyFile; | ||
|
|
||
| try { | ||
| storyFile = require(storyPath); // eslint-disable-line @typescript-eslint/no-var-requires | ||
| storyFile = requireFn(storyPath); | ||
| } catch (error) { | ||
| if (!loggedStoryFileRequireError) { | ||
| loggedStoryFileRequireError = true; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,13 @@ import type { StoryLoadResult } from "./open-story/testplane-open-story"; | |
| import type { TestplaneOpts } from "../story-to-test"; | ||
| import type { TestFunctionExtendedCtx } from "../../types"; | ||
| import type { StorybookStoryExtended, StorybookStory } from "./types"; | ||
| import { extractInheritedValue } from "./inheritable-values"; | ||
|
|
||
| export function getAbsoluteFilePath(): string { | ||
| return __filename; | ||
| } | ||
|
|
||
| // Those stories must be from a single story file | ||
| export function run(stories: StorybookStory[], opts: TestplaneOpts): void { | ||
| const withStoryFileDataStories = extendStoriesFromStoryFile(stories); | ||
|
|
||
|
|
@@ -21,18 +23,17 @@ function createTestplaneTests( | |
| { autoScreenshots, autoscreenshotSelector, autoScreenshotStorybookGlobals }: TestplaneOpts, | ||
| ): void { | ||
| nestedDescribe(story, () => { | ||
| const rawAutoScreenshotGlobalSets = { | ||
| ...autoScreenshotStorybookGlobals, | ||
| ...story.autoScreenshotStorybookGlobals, | ||
| }; | ||
|
|
||
| const rawAutoScreenshotGlobalSets = extractInheritedValue( | ||
| story.autoScreenshotStorybookGlobals, | ||
|
Comment on lines
+26
to
+27
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here More about |
||
| autoScreenshotStorybookGlobals, | ||
| ); | ||
| const screenshotGlobalSetNames = Object.keys(rawAutoScreenshotGlobalSets); | ||
|
|
||
| const autoScreenshotGlobalSets = screenshotGlobalSetNames.length | ||
| ? screenshotGlobalSetNames.map(name => ({ name, globals: rawAutoScreenshotGlobalSets[name] })) | ||
| : [{ name: "", globals: {} }]; | ||
|
|
||
| if (autoScreenshots) { | ||
| if (story.autoScreenshots ?? autoScreenshots) { | ||
| for (const { name, globals } of autoScreenshotGlobalSets) { | ||
| extendedIt( | ||
| story, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { extractInheritedValue, inheritValue } from "./inheritable-values"; | ||
|
|
||
| type TestingObject = { foo: number; bar: number; baz: number }; | ||
| type DeepObject = { foo: number; bar: { baz: number[] } }; | ||
| type InheritedFn<T> = (base: T) => T; | ||
|
|
||
| describe("storybook/story-test-runner/inheritable-values", () => { | ||
| describe("inheritValue", () => { | ||
| it("should return overlayed value with objects", () => { | ||
| const firstObject = { foo: 4 } as TestingObject; | ||
| const secondObject = { bar: 6 } as TestingObject; | ||
| const thirdObject = { baz: 8 } as TestingObject; | ||
| const fourthObject = { bar: 10 } as TestingObject; | ||
|
|
||
| const inheritedValue = inheritValue(firstObject, secondObject, thirdObject, fourthObject); | ||
|
|
||
| expect(inheritedValue).toEqual({ foo: 4, bar: 10, baz: 8 }); | ||
| }); | ||
|
|
||
| it("should return overlayed value with mixed objects and functions", () => { | ||
| const baseObject = { foo: 4 } as TestingObject; | ||
| const extendObjectFn: InheritedFn<TestingObject> = baseObj => ({ ...baseObj, bar: 8 }); | ||
|
|
||
| const inheritedValueFn = inheritValue(baseObject, extendObjectFn); | ||
| const inheritedValue = (inheritedValueFn as InheritedFn<TestingObject>)({} as TestingObject); | ||
|
|
||
| expect(inheritedValue).toEqual({ foo: 4, bar: 8 }); | ||
| }); | ||
|
|
||
| it("should not mutate objects", () => { | ||
| const firstObject = { foo: 4 } as TestingObject; | ||
| const secondObject = { bar: 6 } as TestingObject; | ||
| const thirdObject = { bar: 8 } as TestingObject; | ||
|
|
||
| inheritValue(firstObject, secondObject, thirdObject); | ||
|
|
||
| expect(firstObject).toEqual({ foo: 4 }); | ||
| expect(secondObject).toEqual({ bar: 6 }); | ||
| expect(thirdObject).toEqual({ bar: 8 }); | ||
| }); | ||
|
|
||
| it("should not mutate objects deeply even if they are mutated directly by functions", () => { | ||
| const baseObject = { bar: { baz: [1, 2, 3] } } as DeepObject; | ||
| const mutateFn: InheritedFn<DeepObject> = baseObj => { | ||
| baseObj.foo = 4; | ||
| baseObj.bar.baz[1] = 100500; | ||
|
|
||
| return baseObj; | ||
| }; | ||
|
|
||
| const inheritedValueFn = inheritValue(baseObject, mutateFn); | ||
| const inheritedValue = (inheritedValueFn as InheritedFn<DeepObject>)({} as DeepObject); | ||
|
|
||
| expect(inheritedValue).toEqual({ foo: 4, bar: { baz: [1, 100500, 3] } }); | ||
| expect(baseObject).toEqual({ bar: { baz: [1, 2, 3] } }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("extractInheritedValue", () => { | ||
| it("should extract inherited value", () => { | ||
| const baseValue = { foo: 4 } as TestingObject; | ||
| const overlayedValue = { bar: 6 } as TestingObject; | ||
|
|
||
| const inheritedValue = inheritValue(baseValue, overlayedValue); | ||
| const resultValue = extractInheritedValue(inheritedValue, { foo: 8, baz: 10 } as TestingObject); | ||
|
|
||
| expect(resultValue).toEqual({ foo: 4, bar: 6, baz: 10 }); | ||
| }); | ||
|
|
||
| it("should extract inherited function", () => { | ||
| const initialValue = { foo: 100, baz: 10 } as TestingObject; | ||
| const baseValue = { foo: 4 } as TestingObject; | ||
| const mutateFn: InheritedFn<TestingObject> = baseObj => { | ||
| baseObj.foo *= 2; | ||
| baseObj.baz *= 2; | ||
|
|
||
| return baseObj; | ||
| }; | ||
|
|
||
| const inheritedValue = inheritValue(baseValue, mutateFn); | ||
| const resultValue = extractInheritedValue(inheritedValue, initialValue); | ||
|
|
||
| expect(resultValue).toEqual({ foo: 8, baz: 20 }); | ||
| }); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old jest didn't knew about "structuredClone" or "satisfies"