1+ import type { FoundSpec , FullConfig } from '@packages/types'
12import { CsfFile , readCsfOrMdx } from '@storybook/csf-tools'
23import endent from 'endent'
34import * as path from 'path'
@@ -13,15 +14,18 @@ export class StorybookActions {
1314 throw Error ( `Cannot generate a spec without activeProject.` )
1415 }
1516
16- const spec = await this . generate ( storyPath , project . projectRoot )
17+ const config = await this . ctx . project . getConfig ( project . projectRoot )
18+
19+ const spec = await this . generate ( storyPath , project . projectRoot , config . componentFolder )
1720
1821 this . ctx . wizardData . generatedSpec = spec
1922 }
2023
2124 private async generate (
2225 storyPath : string ,
2326 projectRoot : string ,
24- ) : Promise < Cypress . Cypress [ 'spec' ] | null > {
27+ componentFolder : FullConfig [ 'componentFolder' ] ,
28+ ) : Promise < FoundSpec | null > {
2529 const storyFile = path . parse ( storyPath )
2630 const storyName = storyFile . name . split ( '.' ) [ 0 ]
2731
@@ -38,11 +42,12 @@ export class StorybookActions {
3842 return null
3943 }
4044
45+ const specFileExtension = '.cy-spec'
4146 const newSpecContent = this . generateSpecFromCsf ( parsed , storyFile )
4247 const newSpecPath = path . join (
4348 storyPath ,
4449 '..' ,
45- `${ parsed . meta . component } .cy-spec ${ storyFile . ext } ` ,
50+ `${ parsed . meta . component } ${ specFileExtension } ${ storyFile . ext } ` ,
4651 )
4752
4853 // If this passes then the file exists and we don't want to overwrite it
@@ -56,10 +61,18 @@ export class StorybookActions {
5661
5762 await this . ctx . fs . outputFileSync ( newSpecPath , newSpecContent )
5863
64+ const parsedSpec = path . parse ( newSpecPath )
65+
66+ // Can this be obtained from the spec watcher?
5967 return {
60- name : path . parse ( newSpecPath ) . name ,
68+ baseName : parsedSpec . base ,
69+ fileName : parsedSpec . base . replace ( specFileExtension , '' ) ,
70+ specFileExtension,
71+ fileExtension : parsedSpec . ext ,
72+ name : path . relative ( componentFolder || projectRoot , newSpecPath ) ,
6173 relative : path . relative ( projectRoot , newSpecPath ) ,
6274 absolute : newSpecPath ,
75+ specType : 'component' ,
6376 }
6477 } catch ( e ) {
6578 return null
0 commit comments