@@ -98,10 +98,9 @@ const tests = allTests.filter((name) => {
9898} ) ;
9999
100100// Remove tests that are not part of this shard.
101- const shardedTests = tests . filter ( ( name , i ) => shardId === null || i % nbShards == shardId ) ;
102- const testsToRun = allSetups . concat ( shardedTests ) ;
101+ const testsToRun = tests . filter ( ( name , i ) => shardId === null || i % nbShards == shardId ) ;
103102
104- if ( shardedTests . length === 0 ) {
103+ if ( testsToRun . length === 0 ) {
105104 console . log ( `No tests would be ran, aborting.` ) ;
106105 process . exit ( 1 ) ;
107106}
@@ -114,28 +113,27 @@ console.log(testsToRun.join('\n'));
114113if ( testsToRun . length == allTests . length ) {
115114 console . log ( `Running ${ testsToRun . length } tests` ) ;
116115} else {
117- console . log ( `Running ${ testsToRun . length } tests (${ allTests . length + allSetups . length } total)` ) ;
116+ console . log ( `Running ${ testsToRun . length } tests (${ allTests . length } total)` ) ;
118117}
119118
120119setGlobalVariable ( 'argv' , argv ) ;
121120setGlobalVariable ( 'ci' , process . env [ 'CI' ] ?. toLowerCase ( ) === 'true' || process . env [ 'CI' ] === '1' ) ;
122121setGlobalVariable ( 'package-manager' , argv . yarn ? 'yarn' : 'npm' ) ;
123122
123+ let lastTestRun : string | null = null ;
124+
124125Promise . all ( [ findFreePort ( ) , findFreePort ( ) ] )
125126 . then ( async ( [ httpPort , httpsPort ] ) => {
126127 setGlobalVariable ( 'package-registry' , 'http://localhost:' + httpPort ) ;
127128 setGlobalVariable ( 'package-secure-registry' , 'http://localhost:' + httpsPort ) ;
128129
129- let lastTestRun : string | null = null ;
130-
131130 // NPM registries for the lifetime of the test execution
132131 const registryProcess = await createNpmRegistry ( httpPort , httpPort ) ;
133132 const secureRegistryProcess = await createNpmRegistry ( httpPort , httpsPort , true ) ;
134133
135134 try {
136- for ( const [ testIndex , test ] of testsToRun . entries ( ) ) {
137- await runTest ( ( lastTestRun = test ) , testIndex ) ;
138- }
135+ await runSteps ( runSetup , allSetups , 'setup' ) ;
136+ await runSteps ( runTest , testsToRun , 'test' ) ;
139137
140138 console . log ( colors . green ( 'Done.' ) ) ;
141139 } catch ( err ) {
@@ -165,16 +163,43 @@ Promise.all([findFreePort(), findFreePort()])
165163 ( ) => process . exit ( 1 ) ,
166164 ) ;
167165
168- async function runTest ( relativeName : string , testIndex : number ) {
169- // Make sure this is a windows compatible path.
170- let absoluteName = path . join ( e2eRoot , relativeName ) ;
171- if ( / ^ w i n / . test ( process . platform ) ) {
172- absoluteName = absoluteName . replace ( / \\ / g, path . posix . sep ) ;
166+ async function runSteps (
167+ run : ( name : string ) => Promise < void > | void ,
168+ steps : string [ ] ,
169+ type : 'setup' | 'test' ,
170+ ) {
171+ for ( const [ stepIndex , relativeName ] of steps . entries ( ) ) {
172+ // Make sure this is a windows compatible path.
173+ let absoluteName = path . join ( e2eRoot , relativeName ) . replace ( / \. t s $ / , '' ) ;
174+ if ( / ^ w i n / . test ( process . platform ) ) {
175+ absoluteName = absoluteName . replace ( / \\ / g, path . posix . sep ) ;
176+ }
177+
178+ const name = relativeName . replace ( / \. t s $ / , '' ) ;
179+ const start = Date . now ( ) ;
180+
181+ printHeader ( relativeName , stepIndex , steps . length , type ) ;
182+
183+ // Run the test function with the current file on the logStack.
184+ logStack . push ( lastLogger ( ) . createChild ( absoluteName ) ) ;
185+ try {
186+ await run ( ( lastTestRun = absoluteName ) ) ;
187+ } finally {
188+ logStack . pop ( ) ;
189+ }
190+
191+ console . log ( '----' ) ;
192+ printFooter ( name , type , start ) ;
173193 }
194+ }
195+
196+ async function runSetup ( absoluteName : string ) {
197+ const module = require ( absoluteName ) ;
174198
175- const currentFileName = relativeName . replace ( / \. t s $ / , '' ) ;
176- const start = + new Date ( ) ;
199+ await ( typeof module === 'function' ? module : module . default ) ( ) ;
200+ }
177201
202+ async function runTest ( absoluteName : string ) {
178203 const module = require ( absoluteName ) ;
179204 const originalEnvVariables = {
180205 ...process . env ,
@@ -189,70 +214,50 @@ async function runTest(relativeName: string, testIndex: number) {
189214 throw new Error ( 'Invalid test module.' ) ;
190215 } ;
191216
192- printHeader ( currentFileName , testIndex ) ;
193-
194217 let clean = true ;
195218 let previousDir = process . cwd ( ) ;
196- try {
197- // Run the test function with the current file on the logStack.
198- logStack . push ( lastLogger ( ) . createChild ( currentFileName ) ) ;
199- try {
200- await fn ( ( ) => ( clean = false ) ) ;
201- } finally {
202- logStack . pop ( ) ;
203- }
204219
205- console . log ( '----' ) ;
220+ await fn ( ( ) => ( clean = false ) ) ;
206221
207- // If we're not in a setup, change the directory back to where it was before the test.
208- // This allows tests to chdir without worrying about keeping the original directory.
209- if ( ! allSetups . includes ( relativeName ) && previousDir ) {
210- process . chdir ( previousDir ) ;
222+ // Change the directory back to where it was before the test.
223+ // This allows tests to chdir without worrying about keeping the original directory.
224+ if ( previousDir ) {
225+ process . chdir ( previousDir ) ;
211226
212- // Restore env variables before each test.
213- console . log ( ' Restoring original environment variables...' ) ;
214- process . env = originalEnvVariables ;
215- }
227+ // Restore env variables before each test.
228+ console . log ( 'Restoring original environment variables...' ) ;
229+ process . env = originalEnvVariables ;
230+ }
216231
217- // Only clean after a real test, not a setup step. Also skip cleaning if the test
218- // requested an exception.
219- if ( ! allSetups . includes ( relativeName ) && clean ) {
220- logStack . push ( new logging . NullLogger ( ) ) ;
221- try {
222- await gitClean ( ) ;
223- } finally {
224- logStack . pop ( ) ;
225- }
232+ // Skip cleaning if the test requested an exception.
233+ if ( clean ) {
234+ logStack . push ( new logging . NullLogger ( ) ) ;
235+ try {
236+ await gitClean ( ) ;
237+ } finally {
238+ logStack . pop ( ) ;
226239 }
227-
228- printFooter ( currentFileName , start ) ;
229- } catch ( err ) {
230- printFooter ( currentFileName , start ) ;
231- console . error ( err ) ;
232- throw err ;
233240 }
234241}
235242
236- function printHeader ( testName : string , testIndex : number ) {
237- const text = `${ testIndex + 1 } of ${ testsToRun . length } ` ;
238- const fullIndex =
239- ( testIndex < allSetups . length
240- ? testIndex
241- : ( testIndex - allSetups . length ) * nbShards + shardId + allSetups . length ) + 1 ;
242- const length = tests . length + allSetups . length ;
243+ function printHeader ( testName : string , testIndex : number , count : number , type : 'setup' | 'test' ) {
244+ const text = `${ testIndex + 1 } of ${ count } ` ;
245+ const fullIndex = testIndex * nbShards + shardId + 1 ;
243246 const shard =
244- shardId === null
247+ shardId === null || type !== 'test'
245248 ? ''
246- : colors . yellow ( ` [${ shardId } :${ nbShards } ]` + colors . bold ( ` (${ fullIndex } /${ length } )` ) ) ;
249+ : colors . yellow ( ` [${ shardId } :${ nbShards } ]` + colors . bold ( ` (${ fullIndex } /${ tests . length } )` ) ) ;
247250 console . log (
248- colors . green ( `Running "${ colors . bold . blue ( testName ) } " (${ colors . bold . white ( text ) } ${ shard } )...` ) ,
251+ colors . green (
252+ `Running ${ type } "${ colors . bold . blue ( testName ) } " (${ colors . bold . white ( text ) } ${ shard } )...` ,
253+ ) ,
249254 ) ;
250255}
251256
252- function printFooter ( testName : string , startTime : number ) {
257+ function printFooter ( testName : string , type : 'setup' | 'test' , startTime : number ) {
253258 // Round to hundredth of a second.
254259 const t = Math . round ( ( Date . now ( ) - startTime ) / 10 ) / 100 ;
255- console . log ( colors . green ( ' Last step took ' ) + colors . bold . blue ( '' + t ) + colors . green ( 's...' ) ) ;
260+ console . log ( colors . green ( ` Last ${ type } took ` ) + colors . bold . blue ( '' + t ) + colors . green ( 's...' ) ) ;
256261 console . log ( '' ) ;
257262}
258263
0 commit comments