@@ -25,29 +25,29 @@ const {
2525 parseCommandLine,
2626 reporterScope,
2727 shouldColorizeTestFiles,
28+ setupGlobalSetupTeardownFunctions,
2829} = require ( 'internal/test_runner/utils' ) ;
2930const { queueMicrotask } = require ( 'internal/process/task_queues' ) ;
3031const { TIMEOUT_MAX } = require ( 'internal/timers' ) ;
3132const { clearInterval, setInterval } = require ( 'timers' ) ;
3233const { bigint : hrtime } = process . hrtime ;
33- const resolvedPromise = PromiseResolve ( ) ;
3434const testResources = new SafeMap ( ) ;
3535let globalRoot ;
36+ let globalSetupExecuted = false ;
3637
3738testResources . set ( reporterScope . asyncId ( ) , reporterScope ) ;
3839
3940function createTestTree ( rootTestOptions , globalOptions ) {
4041 const buildPhaseDeferred = PromiseWithResolvers ( ) ;
4142 const isFilteringByName = globalOptions . testNamePatterns ||
42- globalOptions . testSkipPatterns ;
43+ globalOptions . testSkipPatterns ;
4344 const isFilteringByOnly = ( globalOptions . isolation === 'process' || process . env . NODE_TEST_CONTEXT ) ?
4445 globalOptions . only : true ;
4546 const harness = {
4647 __proto__ : null ,
4748 buildPromise : buildPhaseDeferred . promise ,
4849 buildSuites : [ ] ,
4950 isWaitingForBuildPhase : false ,
50- bootstrapPromise : resolvedPromise ,
5151 watching : false ,
5252 config : globalOptions ,
5353 coverage : null ,
@@ -71,6 +71,21 @@ function createTestTree(rootTestOptions, globalOptions) {
7171 snapshotManager : null ,
7272 isFilteringByName,
7373 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+ } ,
7489 async waitForBuildPhase ( ) {
7590 if ( harness . buildSuites . length > 0 ) {
7691 await SafePromiseAllReturnVoid ( harness . buildSuites ) ;
@@ -81,6 +96,7 @@ function createTestTree(rootTestOptions, globalOptions) {
8196 } ;
8297
8398 harness . resetCounters ( ) ;
99+ harness . bootstrapPromise = harness . runBootstrap ( ) ;
84100 globalRoot = new Test ( {
85101 __proto__ : null ,
86102 ...rootTestOptions ,
@@ -232,6 +248,11 @@ function setupProcessState(root, globalOptions) {
232248 'Promise resolution is still pending but the event loop has already resolved' ,
233249 kCancelledByParent ) ) ;
234250
251+ if ( root . harness . globalTeardownFunction ) {
252+ await root . harness . globalTeardownFunction ( ) ;
253+ root . harness . globalTeardownFunction = null ;
254+ }
255+
235256 hook . disable ( ) ;
236257 process . removeListener ( 'uncaughtException' , exceptionHandler ) ;
237258 process . removeListener ( 'unhandledRejection' , rejectionHandler ) ;
@@ -278,7 +299,10 @@ function lazyBootstrapRoot() {
278299 process . exitCode = kGenericUserError ;
279300 }
280301 } ) ;
281- globalRoot . harness . bootstrapPromise = globalOptions . setup ( globalRoot . reporter ) ;
302+ globalRoot . harness . bootstrapPromise = SafePromiseAllReturnVoid ( [
303+ globalRoot . harness . bootstrapPromise ,
304+ globalOptions . setup ( globalRoot . reporter ) ,
305+ ] ) ;
282306 }
283307 return globalRoot ;
284308}
0 commit comments