@@ -98,10 +98,9 @@ const tests = allTests.filter((name) => {
98
98
} ) ;
99
99
100
100
// 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 ) ;
103
102
104
- if ( shardedTests . length === 0 ) {
103
+ if ( testsToRun . length === 0 ) {
105
104
console . log ( `No tests would be ran, aborting.` ) ;
106
105
process . exit ( 1 ) ;
107
106
}
@@ -114,28 +113,27 @@ console.log(testsToRun.join('\n'));
114
113
if ( testsToRun . length == allTests . length ) {
115
114
console . log ( `Running ${ testsToRun . length } tests` ) ;
116
115
} else {
117
- console . log ( `Running ${ testsToRun . length } tests (${ allTests . length + allSetups . length } total)` ) ;
116
+ console . log ( `Running ${ testsToRun . length } tests (${ allTests . length } total)` ) ;
118
117
}
119
118
120
119
setGlobalVariable ( 'argv' , argv ) ;
121
120
setGlobalVariable ( 'ci' , process . env [ 'CI' ] ?. toLowerCase ( ) === 'true' || process . env [ 'CI' ] === '1' ) ;
122
121
setGlobalVariable ( 'package-manager' , argv . yarn ? 'yarn' : 'npm' ) ;
123
122
123
+ let lastTestRun : string | null = null ;
124
+
124
125
Promise . all ( [ findFreePort ( ) , findFreePort ( ) ] )
125
126
. then ( async ( [ httpPort , httpsPort ] ) => {
126
127
setGlobalVariable ( 'package-registry' , 'http://localhost:' + httpPort ) ;
127
128
setGlobalVariable ( 'package-secure-registry' , 'http://localhost:' + httpsPort ) ;
128
129
129
- let lastTestRun : string | null = null ;
130
-
131
130
// NPM registries for the lifetime of the test execution
132
131
const registryProcess = await createNpmRegistry ( httpPort , httpPort ) ;
133
132
const secureRegistryProcess = await createNpmRegistry ( httpPort , httpsPort , true ) ;
134
133
135
134
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' ) ;
139
137
140
138
console . log ( colors . green ( 'Done.' ) ) ;
141
139
} catch ( err ) {
@@ -165,16 +163,43 @@ Promise.all([findFreePort(), findFreePort()])
165
163
( ) => process . exit ( 1 ) ,
166
164
) ;
167
165
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 , start ) ;
173
193
}
194
+ }
195
+
196
+ async function runSetup ( absoluteName : string ) {
197
+ const module = require ( absoluteName ) ;
174
198
175
- const currentFileName = relativeName . replace ( / \. t s $ / , '' ) ;
176
- const start = + new Date ( ) ;
199
+ await ( typeof module === 'function' ? module : module . default ) ( ) ;
200
+ }
177
201
202
+ async function runTest ( absoluteName : string ) {
178
203
const module = require ( absoluteName ) ;
179
204
const originalEnvVariables = {
180
205
...process . env ,
@@ -189,63 +214,43 @@ async function runTest(relativeName: string, testIndex: number) {
189
214
throw new Error ( 'Invalid test module.' ) ;
190
215
} ;
191
216
192
- printHeader ( currentFileName , testIndex ) ;
193
-
194
217
let clean = true ;
195
218
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
- }
204
219
205
- console . log ( '----' ) ;
220
+ await fn ( ( ) => ( clean = false ) ) ;
206
221
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 ) ;
211
226
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
+ }
216
231
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 ( ) ;
226
239
}
227
-
228
- printFooter ( currentFileName , start ) ;
229
- } catch ( err ) {
230
- printFooter ( currentFileName , start ) ;
231
- console . error ( err ) ;
232
- throw err ;
233
240
}
234
241
}
235
242
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 ;
243
246
const shard =
244
- shardId === null
247
+ shardId === null || type !== 'test'
245
248
? ''
246
- : colors . yellow ( ` [${ shardId } :${ nbShards } ]` + colors . bold ( ` (${ fullIndex } /${ length } )` ) ) ;
249
+ : colors . yellow ( ` [${ shardId } :${ nbShards } ]` + colors . bold ( ` (${ fullIndex } /${ tests . length } )` ) ) ;
247
250
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
+ ) ,
249
254
) ;
250
255
}
251
256
0 commit comments