@@ -18,19 +18,41 @@ const variants = vitest.inject('variants');
1818
1919type Fixtures < Addons extends AddonMap > = {
2020 page : Page ;
21- run ( variant : ProjectVariant , options : OptionMap < Addons > ) : Promise < string > ;
21+ run ( variant : ProjectVariant , options : OptionMap < Addons > ) : Promise < void > ;
22+ cwdVariant : ( variant : ProjectVariant ) => string ;
23+ } ;
24+
25+ const installAddonHelper = async < Addons extends AddonMap > (
26+ cwdToUse : string ,
27+ addons : Addons ,
28+ variant : ProjectVariant ,
29+ options : OptionMap < Addons >
30+ ) => {
31+ const metaPath = path . resolve ( cwdToUse , 'meta.json' ) ;
32+ fs . writeFileSync ( metaPath , JSON . stringify ( { variant, options } , null , '\t' ) , 'utf8' ) ;
33+
34+ // run addon
35+ const { pnpmBuildDependencies } = await installAddon ( {
36+ cwd : cwdToUse ,
37+ addons,
38+ options,
39+ packageManager : 'pnpm'
40+ } ) ;
41+
42+ addPnpmBuildDependencies ( cwdToUse , 'pnpm' , [ 'esbuild' , ...pnpmBuildDependencies ] ) ;
2243} ;
2344
2445export function setupTest < Addons extends AddonMap > (
2546 addons : Addons ,
26- options ?: { skipBrowser ?: boolean }
47+ options ?: { skipBrowser ?: boolean ; runPrepareAndInstallWithOption ?: OptionMap < Addons > }
2748) {
2849 const test = vitest . test . extend < Fixtures < Addons > > ( { } as any ) ;
2950
3051 const withBrowser = ! options ?. skipBrowser ;
3152
3253 let create : CreateProject ;
3354 let browser : Browser ;
55+ let cwdTestName : string ;
3456
3557 if ( withBrowser ) {
3658 vitest . beforeAll ( async ( ) => {
@@ -41,8 +63,11 @@ export function setupTest<Addons extends AddonMap>(
4163 } ) ;
4264 }
4365
44- vitest . beforeAll ( ( { name } ) => {
66+ vitest . beforeAll ( async ( { name } ) => {
4567 const testName = path . dirname ( name ) . split ( '/' ) . at ( - 1 ) ! ;
68+ cwdTestName = path . resolve ( cwd , testName ) ;
69+
70+ fs . rmSync ( cwdTestName , { force : true , recursive : true } ) ;
4671
4772 // constructs a builder for create test projects
4873 create = createProject ( { cwd, templatesDir, testName } ) ;
@@ -62,6 +87,18 @@ export function setupTest<Addons extends AddonMap>(
6287 private : true
6388 } )
6489 ) ;
90+
91+ // run prepare and install steps if requested
92+ if ( options ?. runPrepareAndInstallWithOption ) {
93+ // prepare: run addon for all variants
94+ for ( const variant of variants ) {
95+ const cwd = create ( { testId : variant , variant } ) ;
96+ await installAddonHelper ( cwd , addons , variant , options . runPrepareAndInstallWithOption ) ;
97+ }
98+
99+ // install: run pnpm install
100+ execSync ( 'pnpm install --no-frozen-lockfile' , { cwd : cwdTestName , stdio : 'pipe' } ) ;
101+ }
65102 } ) ;
66103
67104 // runs before each test case
@@ -71,23 +108,12 @@ export function setupTest<Addons extends AddonMap>(
71108 browserCtx = await browser . newContext ( ) ;
72109 ctx . page = await browserCtx . newPage ( ) ;
73110 }
74- ctx . run = async ( variant , options ) => {
75- const cwd = create ( { testId : ctx . task . id , variant } ) ;
76-
77- // test metadata
78- const metaPath = path . resolve ( cwd , 'meta.json' ) ;
79- fs . writeFileSync ( metaPath , JSON . stringify ( { variant, options } , null , '\t' ) , 'utf8' ) ;
80-
81- // run addon
82- const { pnpmBuildDependencies } = await installAddon ( {
83- cwd,
84- addons,
85- options,
86- packageManager : 'pnpm'
87- } ) ;
88- addPnpmBuildDependencies ( cwd , 'pnpm' , [ 'esbuild' , ...pnpmBuildDependencies ] ) ;
89-
90- return cwd ;
111+ ctx . cwdVariant = ( variant ) => {
112+ return path . resolve ( cwdTestName , variant ) ;
113+ } ;
114+ ctx . run = async ( variant , runOptions ) => {
115+ const cwd = create ( { testId : variant , variant } ) ;
116+ await installAddonHelper ( cwd , addons , variant , runOptions ) ;
91117 } ;
92118
93119 return async ( ) => {
0 commit comments