@@ -28,18 +28,20 @@ const{
2828} = require ( 'internal/test_runner/utils' ) ;
2929const { queueMicrotask } = require ( 'internal/process/task_queues' ) ;
3030const { bigint : hrtime } = process . hrtime ;
31-
31+ const resolvedPromise = PromiseResolve ( ) ;
3232const testResources = new SafeMap ( ) ;
33+ let globalRoot ;
3334
3435testResources . set ( reporterScope . asyncId ( ) , reporterScope ) ;
3536
3637function createTestTree ( options = kEmptyObject ) {
37- return setup ( new Test ( { __proto__ : null , ...options , name : '<root>' } ) ) ;
38+ globalRoot = setup ( new Test ( { __proto__ : null , ...options , name : '<root>' } ) ) ;
39+ return globalRoot ;
3840}
3941
4042function createProcessEventHandler ( eventName , rootTest ) {
4143return ( err ) => {
42- if ( ! rootTest . harness . bootstrapComplete ) {
44+ if ( rootTest . harness . bootstrapPromise ) {
4345// Something went wrong during the asynchronous portion of bootstrapping
4446// the test runner. Since the test runner is not setup properly, we can't
4547// do anything but throw the error.
@@ -196,7 +198,7 @@ function setup(root){
196198root . harness = {
197199__proto__ : null ,
198200allowTestsToRun : false ,
199- bootstrapComplete : false ,
201+ bootstrapPromise : resolvedPromise ,
200202watching : false ,
201203coverage : FunctionPrototypeBind ( collectCoverage , null , root , coverage ) ,
202204resetCounters ( ) {
@@ -222,33 +224,30 @@ function setup(root){
222224return root ;
223225}
224226
225- let globalRoot ;
226- let asyncBootstrap ;
227227function lazyBootstrapRoot ( ) {
228228if ( ! globalRoot ) {
229- globalRoot = createTestTree ( { __proto__ : null , entryFile : process . argv ?. [ 1 ] } ) ;
229+ // This is where the test runner is bootstrapped when node:test is used
230+ // without the --test flag or the run() API.
231+ createTestTree ( { __proto__ : null , entryFile : process . argv ?. [ 1 ] } ) ;
230232globalRoot . reporter . on ( 'test:fail' , ( data ) => {
231233if ( data . todo === undefined || data . todo === false ) {
232234process . exitCode = kGenericUserError ;
233235}
234236} ) ;
235- asyncBootstrap = setupTestReporters ( globalRoot . reporter ) ;
237+ globalRoot . harness . bootstrapPromise = setupTestReporters ( globalRoot . reporter ) ;
236238}
237239return globalRoot ;
238240}
239241
240- async function startSubtest ( subtest ) {
241- if ( asyncBootstrap ) {
242+ async function startSubtestAfterBootstrap ( subtest ) {
243+ if ( subtest . root . harness . bootstrapPromise ) {
242244// Only incur the overhead of awaiting the Promise once.
243- await asyncBootstrap ;
244- asyncBootstrap = undefined ;
245- if ( ! subtest . root . harness . bootstrapComplete ) {
246- subtest . root . harness . bootstrapComplete = true ;
247- queueMicrotask ( ( ) => {
248- subtest . root . harness . allowTestsToRun = true ;
249- subtest . root . processPendingSubtests ( ) ;
250- } ) ;
251- }
245+ await subtest . root . harness . bootstrapPromise ;
246+ subtest . root . harness . bootstrapPromise = null ;
247+ queueMicrotask ( ( ) => {
248+ subtest . root . harness . allowTestsToRun = true ;
249+ subtest . root . processPendingSubtests ( ) ;
250+ } ) ;
252251}
253252
254253await subtest . start ( ) ;
@@ -262,7 +261,7 @@ function runInParentContext(Factory){
262261return PromiseResolve ( ) ;
263262}
264263
265- return startSubtest ( subtest ) ;
264+ return startSubtestAfterBootstrap ( subtest ) ;
266265}
267266
268267const test = ( name , options , fn ) => {
0 commit comments