@@ -138,45 +138,93 @@ export class TestContext {
138138 }
139139
140140 async runSeedTest ( seedFile : string , projectName : string , progress : ProgressCallback ) {
141- const { screen } = this . createScreen ( progress ) ;
141+ await this . runWithGlobalSetup ( async ( testRunner , reporter ) => {
142+ const result = await testRunner . runTests ( reporter , {
143+ headed : ! this . options ?. headless ,
144+ locations : [ '/' + escapeRegExp ( seedFile ) + '/' ] ,
145+ projects : [ projectName ] ,
146+ timeout : 0 ,
147+ workers : 1 ,
148+ pauseAtEnd : true ,
149+ disableConfigReporters : true ,
150+ failOnLoadErrors : true ,
151+ } ) ;
152+ // Ideally, we should check that page was indeed created and browser mcp has kicked in.
153+ // However, that is handled in the upper layer, so hard to check here.
154+ if ( result . status === 'passed' && ! reporter . suite ?. allTests ( ) . length )
155+ throw new Error ( 'seed test not found.' ) ;
156+
157+ if ( result . status !== 'passed' )
158+ throw new Error ( 'Errors while running the seed test.' ) ;
159+ } , progress ) ;
160+ }
161+
162+ async runWithGlobalSetup (
163+ callback : ( testRunner : TestRunner , reporter : ListReporter ) => Promise < void > ,
164+ progress : ProgressCallback ) : Promise < void > {
165+ const { screen, claimStdio, releaseStdio } = createScreen ( progress ) ;
142166 const configDir = this . configLocation . configDir ;
143- const reporter = new ListReporter ( { configDir, screen } ) ;
144167 const testRunner = await this . createTestRunner ( ) ;
145168
146- const result = await testRunner . runTests ( reporter , {
147- headed : ! this . options ?. headless ,
148- locations : [ '/' + escapeRegExp ( seedFile ) + '/' ] ,
149- projects : [ projectName ] ,
150- timeout : 0 ,
151- workers : 1 ,
152- pauseAtEnd : true ,
153- disableConfigReporters : true ,
154- failOnLoadErrors : true ,
155- } ) ;
169+ claimStdio ( ) ;
170+ try {
171+ const setupReporter = new ListReporter ( { configDir, screen, includeTestId : true } ) ;
172+ const { status } = await testRunner . runGlobalSetup ( [ setupReporter ] ) ;
173+ if ( status !== 'passed' )
174+ throw new Error ( 'Failed to run global setup' ) ;
175+ } finally {
176+ releaseStdio ( ) ;
177+ }
156178
157- // Ideally, we should check that page was indeed created and browser mcp has kicked in.
158- // However, that is handled in the upper layer, so hard to check here.
159- if ( result . status === 'passed' && ! reporter . suite ?. allTests ( ) . length )
160- throw new Error ( 'seed test not found.' ) ;
179+ try {
180+ const reporter = new ListReporter ( { configDir, screen, includeTestId : true } ) ;
181+ return await callback ( testRunner , reporter ) ;
182+ } finally {
183+ claimStdio ( ) ;
184+ await testRunner . runGlobalTeardown ( ) . finally ( ( ) => {
185+ releaseStdio ( ) ;
186+ } ) ;
187+ }
188+ }
161189
162- if ( result . status !== 'passed' )
163- throw new Error ( 'Errors while running the seed test.' ) ;
190+ async close ( ) {
164191 }
192+ }
165193
166- createScreen ( progress : ProgressCallback ) {
167- const stream = new StringWriteStream ( progress ) ;
168- const screen = {
169- ...terminalScreen ,
170- isTTY : false ,
171- colors : noColors ,
172- stdout : stream as unknown as NodeJS . WriteStream ,
173- stderr : stream as unknown as NodeJS . WriteStream ,
194+ export function createScreen ( progress : ProgressCallback ) {
195+ const stdout = new StringWriteStream ( progress , 'stdout' ) ;
196+ const stderr = new StringWriteStream ( progress , 'stderr' ) ;
197+
198+ const screen = {
199+ ...terminalScreen ,
200+ isTTY : false ,
201+ colors : noColors ,
202+ stdout : stdout as unknown as NodeJS . WriteStream ,
203+ stderr : stderr as unknown as NodeJS . WriteStream ,
204+ } ;
205+
206+ /* eslint-disable no-restricted-properties */
207+ const originalStdoutWrite = process . stdout . write ;
208+ const originalStderrWrite = process . stderr . write ;
209+
210+ const claimStdio = ( ) => {
211+ process . stdout . write = ( chunk : string | Buffer ) => {
212+ stdout . write ( chunk ) ;
213+ return true ;
174214 } ;
175- return { screen, stream } ;
176- }
215+ process . stderr . write = ( chunk : string | Buffer ) => {
216+ stderr . write ( chunk ) ;
217+ return true ;
218+ } ;
219+ } ;
177220
178- async close ( ) {
179- }
221+ const releaseStdio = ( ) => {
222+ process . stdout . write = originalStdoutWrite ;
223+ process . stderr . write = originalStderrWrite ;
224+ } ;
225+ /* eslint-enable no-restricted-properties */
226+
227+ return { screen, claimStdio, releaseStdio } ;
180228}
181229
182230const bestPracticesMarkdown = `
0 commit comments