@@ -25,29 +25,29 @@ const {
25
25
parseCommandLine,
26
26
reporterScope,
27
27
shouldColorizeTestFiles,
28
+ setupGlobalSetupTeardownFunctions,
28
29
} = require ( 'internal/test_runner/utils' ) ;
29
30
const { queueMicrotask } = require ( 'internal/process/task_queues' ) ;
30
31
const { TIMEOUT_MAX } = require ( 'internal/timers' ) ;
31
32
const { clearInterval, setInterval } = require ( 'timers' ) ;
32
33
const { bigint : hrtime } = process . hrtime ;
33
- const resolvedPromise = PromiseResolve ( ) ;
34
34
const testResources = new SafeMap ( ) ;
35
35
let globalRoot ;
36
+ let globalSetupExecuted = false ;
36
37
37
38
testResources . set ( reporterScope . asyncId ( ) , reporterScope ) ;
38
39
39
40
function createTestTree ( rootTestOptions , globalOptions ) {
40
41
const buildPhaseDeferred = PromiseWithResolvers ( ) ;
41
42
const isFilteringByName = globalOptions . testNamePatterns ||
42
- globalOptions . testSkipPatterns ;
43
+ globalOptions . testSkipPatterns ;
43
44
const isFilteringByOnly = ( globalOptions . isolation === 'process' || process . env . NODE_TEST_CONTEXT ) ?
44
45
globalOptions . only : true ;
45
46
const harness = {
46
47
__proto__ : null ,
47
48
buildPromise : buildPhaseDeferred . promise ,
48
49
buildSuites : [ ] ,
49
50
isWaitingForBuildPhase : false ,
50
- bootstrapPromise : resolvedPromise ,
51
51
watching : false ,
52
52
config : globalOptions ,
53
53
coverage : null ,
@@ -71,6 +71,21 @@ function createTestTree(rootTestOptions, globalOptions) {
71
71
snapshotManager : null ,
72
72
isFilteringByName,
73
73
isFilteringByOnly,
74
+ async runBootstrap ( ) {
75
+ if ( globalSetupExecuted ) {
76
+ return PromiseResolve ( ) ;
77
+ }
78
+ globalSetupExecuted = true ;
79
+ const globalSetupFunctions = await setupGlobalSetupTeardownFunctions (
80
+ globalOptions . globalSetupPath ,
81
+ globalOptions . cwd ,
82
+ ) ;
83
+ harness . globalTeardownFunction = globalSetupFunctions . globalTeardownFunction ;
84
+ if ( typeof globalSetupFunctions . globalSetupFunction === 'function' ) {
85
+ return globalSetupFunctions . globalSetupFunction ( ) ;
86
+ }
87
+ return PromiseResolve ( ) ;
88
+ } ,
74
89
async waitForBuildPhase ( ) {
75
90
if ( harness . buildSuites . length > 0 ) {
76
91
await SafePromiseAllReturnVoid ( harness . buildSuites ) ;
@@ -81,6 +96,7 @@ function createTestTree(rootTestOptions, globalOptions) {
81
96
} ;
82
97
83
98
harness . resetCounters ( ) ;
99
+ harness . bootstrapPromise = harness . runBootstrap ( ) ;
84
100
globalRoot = new Test ( {
85
101
__proto__ : null ,
86
102
...rootTestOptions ,
@@ -232,6 +248,11 @@ function setupProcessState(root, globalOptions) {
232
248
'Promise resolution is still pending but the event loop has already resolved' ,
233
249
kCancelledByParent ) ) ;
234
250
251
+ if ( root . harness . globalTeardownFunction ) {
252
+ await root . harness . globalTeardownFunction ( ) ;
253
+ root . harness . globalTeardownFunction = null ;
254
+ }
255
+
235
256
hook . disable ( ) ;
236
257
process . removeListener ( 'uncaughtException' , exceptionHandler ) ;
237
258
process . removeListener ( 'unhandledRejection' , rejectionHandler ) ;
@@ -278,7 +299,10 @@ function lazyBootstrapRoot() {
278
299
process . exitCode = kGenericUserError ;
279
300
}
280
301
} ) ;
281
- globalRoot . harness . bootstrapPromise = globalOptions . setup ( globalRoot . reporter ) ;
302
+ globalRoot . harness . bootstrapPromise = SafePromiseAllReturnVoid ( [
303
+ globalRoot . harness . bootstrapPromise ,
304
+ globalOptions . setup ( globalRoot . reporter ) ,
305
+ ] ) ;
282
306
}
283
307
return globalRoot ;
284
308
}
0 commit comments