11import TypedModule from "module" ;
22import type { TestplaneMetaConfig , TestplaneStoryConfig } from "../../types" ;
3- import type { StorybookStory , StorybookStoryExtended } from "./types" ;
3+ import type { StorybookStory , StorybookStoryExtraProperties , StorybookStoryExtended } from "./types" ;
44
55const Module = TypedModule as any ; // eslint-disable-line @typescript-eslint/no-explicit-any
66
@@ -12,45 +12,62 @@ type StoryFile = { default: TestplaneMetaConfig } & Record<string, TestplaneStor
1212
1313let loggedStoryFileRequireError = Boolean ( process . env . TESTPLANE_STORYBOOK_DISABLE_STORY_REQUIRE_WARNING ) ;
1414
15- export function extendStoriesFromStoryFile ( stories : StorybookStory [ ] ) : StorybookStoryExtended [ ] {
15+ export function extendStoriesFromStoryFile (
16+ stories : StorybookStory [ ] ,
17+ { requireFn = require } = { } ,
18+ ) : StorybookStoryExtended [ ] {
1619 const storiesMap = getStoriesMap ( stories ) ;
1720 const storyPath = stories [ 0 ] . absolutePath ;
18- const storyFile = getStoryFile ( storyPath ) ;
21+ const storyFile = getStoryFile ( storyPath , { requireFn } ) ;
1922 const withStoryFileExtendedStories = stories as StorybookStoryExtended [ ] ;
2023
21- if ( ! storyFile ) {
22- return withStoryFileExtendedStories . map ( story => {
23- story . skip = false ;
24- story . assertViewOpts = { } ;
25- story . browserIds = null ;
26- story . autoScreenshotStorybookGlobals = { } ;
24+ const storyExtendedBaseConfig = {
25+ skip : false ,
26+ browserIds : null ,
27+ assertViewOpts : { } ,
28+ autoScreenshots : null ,
29+ autoscreenshotSelector : null ,
30+ autoScreenshotStorybookGlobals : { } ,
31+ extraTests : null ,
32+ } satisfies StorybookStoryExtraProperties ;
2733
28- return story ;
29- } ) ;
34+ if ( ! storyFile ) {
35+ return withStoryFileExtendedStories . map ( story => Object . assign ( story , storyExtendedBaseConfig ) ) ;
3036 }
3137
32- for ( const storyName in storyFile ) {
38+ const storyTestplaneDefaultConfigs = storyFile . default ?. testplaneConfig || storyFile . default ?. testplane || { } ;
39+
40+ for ( const storyName of Object . keys ( storyFile ) ) {
3341 if ( storyName === "default" ) {
34- withStoryFileExtendedStories . forEach ( story => {
35- const testplaneStoryOpts = storyFile [ storyName ] . testplane || { } ;
42+ continue ;
43+ }
3644
37- story . skip = testplaneStoryOpts . skip || false ;
38- story . assertViewOpts = testplaneStoryOpts . assertViewOpts || { } ;
39- story . browserIds = testplaneStoryOpts . browserIds || null ;
40- story . autoscreenshotSelector = testplaneStoryOpts . autoscreenshotSelector || null ;
41- story . autoScreenshotStorybookGlobals = testplaneStoryOpts . autoScreenshotStorybookGlobals || { } ;
42- } ) ;
45+ const storyMapKey = getStoryNameId ( storyName ) ;
4346
47+ if ( ! storiesMap . has ( storyMapKey ) ) {
4448 continue ;
4549 }
4650
47- const storyMapKey = getStoryNameId ( storyName ) ;
51+ const storyTestlaneConfigs = storyFile [ storyName ] . testplaneConfig || { } ;
52+ const story = storiesMap . get ( storyMapKey ) as StorybookStoryExtended ;
4853
49- if ( storiesMap . has ( storyMapKey ) && storyFile [ storyName ] . testplane ) {
50- const story = storiesMap . get ( storyMapKey ) as StorybookStoryExtended ;
54+ Object . assign ( story , storyExtendedBaseConfig , storyTestplaneDefaultConfigs , storyTestlaneConfigs , {
55+ extraTests : storyFile [ storyName ] . testplane || null ,
56+ } ) ;
5157
52- story . extraTests = storyFile [ storyName ] . testplane ;
53- }
58+ story . assertViewOpts = Object . assign (
59+ { } ,
60+ storyExtendedBaseConfig . assertViewOpts ,
61+ storyTestplaneDefaultConfigs . assertViewOpts ,
62+ storyTestlaneConfigs . assertViewOpts ,
63+ ) ;
64+
65+ story . autoScreenshotStorybookGlobals = Object . assign (
66+ { } ,
67+ storyExtendedBaseConfig . autoScreenshotStorybookGlobals ,
68+ storyTestplaneDefaultConfigs . autoScreenshotStorybookGlobals ,
69+ storyTestlaneConfigs . autoScreenshotStorybookGlobals ,
70+ ) ;
5471 }
5572
5673 return withStoryFileExtendedStories ;
@@ -74,13 +91,13 @@ function getStoryNameId(storyName: string): string {
7491 return storyName . replace ( nonAsciiWordRegExp , "" ) . toLowerCase ( ) ;
7592}
7693
77- function getStoryFile ( storyPath : string ) : StoryFile | null {
94+ function getStoryFile ( storyPath : string , { requireFn = require } = { } ) : StoryFile | null {
7895 const unmockFn = mockLoaders ( { except : storyPath } ) ;
7996
8097 let storyFile ;
8198
8299 try {
83- storyFile = require ( storyPath ) ; // eslint-disable-line @typescript-eslint/no-var-requires
100+ storyFile = requireFn ( storyPath ) ;
84101 } catch ( error ) {
85102 if ( ! loggedStoryFileRequireError ) {
86103 loggedStoryFileRequireError = true ;
0 commit comments