-
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 all 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 |
|---|---|---|
| @@ -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,26 +23,28 @@ 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 screenshotGlobalSetNames = Object.keys(rawAutoScreenshotGlobalSets).filter(name => | ||
| Boolean(rawAutoScreenshotGlobalSets[name]), | ||
| ); | ||
|
|
||
| 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, | ||
| `Autoscreenshot${name ? ` ${name}` : ""}`, | ||
| async function (ctx: TestFunctionExtendedCtx) { | ||
| ctx.expect = globalThis.expect; | ||
|
|
||
| const result = await openStoryStep(ctx.browser, story, globals); | ||
| const result = await openStoryStep(ctx.browser, story, globals as Record<string, unknown>); | ||
| const selector = story.autoscreenshotSelector || autoscreenshotSelector || result.rootSelector; | ||
|
|
||
| await autoScreenshotStep(ctx.browser, selector); | ||
|
|
||
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"