@@ -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,27 +113,26 @@ 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 ( ) ] ) . then ( async ( [ httpPort , httpsPort ] ) => {
125
126
setGlobalVariable ( 'package-registry' , 'http://localhost:' + httpPort ) ;
126
127
setGlobalVariable ( 'package-secure-registry' , 'http://localhost:' + httpsPort ) ;
127
128
128
- let lastTestRun : string | null = null ;
129
-
130
129
try {
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
try {
135
- for ( const [ testIndex , test ] of testsToRun . entries ( ) ) {
136
- await runTest ( ( lastTestRun = test ) , testIndex ) ;
137
- }
134
+ await runSteps ( runSetup , allSetups , 'setup' ) ;
135
+ await runSteps ( runTest , testsToRun , 'test' ) ;
138
136
} finally {
139
137
registryProcess . kill ( ) ;
140
138
secureRegistryProcess . kill ( ) ;
@@ -162,16 +160,43 @@ Promise.all([findFreePort(), findFreePort()]).then(async ([httpPort, httpsPort])
162
160
}
163
161
} ) ;
164
162
165
- async function runTest ( relativeName : string , testIndex : number ) {
166
- // Make sure this is a windows compatible path.
167
- let absoluteName = path . join ( e2eRoot , relativeName ) ;
168
- if ( / ^ w i n / . test ( process . platform ) ) {
169
- absoluteName = absoluteName . replace ( / \\ / g, path . posix . sep ) ;
163
+ async function runSteps (
164
+ run : ( name : string ) => Promise < void > | void ,
165
+ steps : string [ ] ,
166
+ type : 'setup' | 'test' ,
167
+ ) {
168
+ for ( const [ stepIndex , relativeName ] of steps . entries ( ) ) {
169
+ // Make sure this is a windows compatible path.
170
+ let absoluteName = path . join ( e2eRoot , relativeName ) . replace ( / \. t s $ / , '' ) ;
171
+ if ( / ^ w i n / . test ( process . platform ) ) {
172
+ absoluteName = absoluteName . replace ( / \\ / g, path . posix . sep ) ;
173
+ }
174
+
175
+ const name = relativeName . replace ( / \. t s $ / , '' ) ;
176
+ const start = Date . now ( ) ;
177
+
178
+ printHeader ( relativeName , stepIndex , steps . length , type ) ;
179
+
180
+ // Run the test function with the current file on the logStack.
181
+ logStack . push ( lastLogger ( ) . createChild ( absoluteName ) ) ;
182
+ try {
183
+ await run ( ( lastTestRun = absoluteName ) ) ;
184
+ } finally {
185
+ logStack . pop ( ) ;
186
+ }
187
+
188
+ console . log ( '----' ) ;
189
+ printFooter ( name , start ) ;
170
190
}
191
+ }
192
+
193
+ async function runSetup ( absoluteName : string ) {
194
+ const module = require ( absoluteName ) ;
171
195
172
- const currentFileName = relativeName . replace ( / \. t s $ / , '' ) ;
173
- const start = + new Date ( ) ;
196
+ await ( typeof module === 'function' ? module : module . default ) ( ) ;
197
+ }
174
198
199
+ async function runTest ( absoluteName : string ) {
175
200
const module = require ( absoluteName ) ;
176
201
const originalEnvVariables = {
177
202
...process . env ,
@@ -186,63 +211,43 @@ async function runTest(relativeName: string, testIndex: number) {
186
211
throw new Error ( 'Invalid test module.' ) ;
187
212
} ;
188
213
189
- printHeader ( currentFileName , testIndex ) ;
190
-
191
214
let clean = true ;
192
215
let previousDir = process . cwd ( ) ;
193
- try {
194
- // Run the test function with the current file on the logStack.
195
- logStack . push ( lastLogger ( ) . createChild ( currentFileName ) ) ;
196
- try {
197
- await fn ( ( ) => ( clean = false ) ) ;
198
- } finally {
199
- logStack . pop ( ) ;
200
- }
201
216
202
- console . log ( '----' ) ;
217
+ await fn ( ( ) => ( clean = false ) ) ;
203
218
204
- // If we're not in a setup, change the directory back to where it was before the test.
205
- // This allows tests to chdir without worrying about keeping the original directory.
206
- if ( ! allSetups . includes ( relativeName ) && previousDir ) {
207
- process . chdir ( previousDir ) ;
219
+ // Change the directory back to where it was before the test.
220
+ // This allows tests to chdir without worrying about keeping the original directory.
221
+ if ( previousDir ) {
222
+ process . chdir ( previousDir ) ;
208
223
209
- // Restore env variables before each test.
210
- console . log ( ' Restoring original environment variables...' ) ;
211
- process . env = originalEnvVariables ;
212
- }
224
+ // Restore env variables before each test.
225
+ console . log ( 'Restoring original environment variables...' ) ;
226
+ process . env = originalEnvVariables ;
227
+ }
213
228
214
- // Only clean after a real test, not a setup step. Also skip cleaning if the test
215
- // requested an exception.
216
- if ( ! allSetups . includes ( relativeName ) && clean ) {
217
- logStack . push ( new logging . NullLogger ( ) ) ;
218
- try {
219
- await gitClean ( ) ;
220
- } finally {
221
- logStack . pop ( ) ;
222
- }
229
+ // Skip cleaning if the test requested an exception.
230
+ if ( clean ) {
231
+ logStack . push ( new logging . NullLogger ( ) ) ;
232
+ try {
233
+ await gitClean ( ) ;
234
+ } finally {
235
+ logStack . pop ( ) ;
223
236
}
224
-
225
- printFooter ( currentFileName , start ) ;
226
- } catch ( err ) {
227
- printFooter ( currentFileName , start ) ;
228
- console . error ( err ) ;
229
- throw err ;
230
237
}
231
238
}
232
239
233
- function printHeader ( testName : string , testIndex : number ) {
234
- const text = `${ testIndex + 1 } of ${ testsToRun . length } ` ;
235
- const fullIndex =
236
- ( testIndex < allSetups . length
237
- ? testIndex
238
- : ( testIndex - allSetups . length ) * nbShards + shardId + allSetups . length ) + 1 ;
239
- const length = tests . length + allSetups . length ;
240
+ function printHeader ( testName : string , testIndex : number , count : number , type : 'setup' | 'test' ) {
241
+ const text = `${ testIndex + 1 } of ${ count } ` ;
242
+ const fullIndex = testIndex * nbShards + shardId + 1 ;
240
243
const shard =
241
- shardId === null
244
+ shardId === null || type !== 'test'
242
245
? ''
243
- : colors . yellow ( ` [${ shardId } :${ nbShards } ]` + colors . bold ( ` (${ fullIndex } /${ length } )` ) ) ;
246
+ : colors . yellow ( ` [${ shardId } :${ nbShards } ]` + colors . bold ( ` (${ fullIndex } /${ tests . length } )` ) ) ;
244
247
console . log (
245
- colors . green ( `Running "${ colors . bold . blue ( testName ) } " (${ colors . bold . white ( text ) } ${ shard } )...` ) ,
248
+ colors . green (
249
+ `Running ${ type } "${ colors . bold . blue ( testName ) } " (${ colors . bold . white ( text ) } ${ shard } )...` ,
250
+ ) ,
246
251
) ;
247
252
}
248
253
0 commit comments