@@ -388,11 +388,16 @@ async function runTest(
388388 const cjsPath = join ( testFixtures , 'global-setup-teardown' , 'required-module.cjs' ) ;
389389 const esmpFile = fixtures . fileURL ( 'test-runner' , 'global-setup-teardown' , 'imported-module.mjs' ) ;
390390
391- it ( 'should run required module before globalSetup' , async ( ) => {
391+ // The difference in behavior is due to how --require and --import are handled by
392+ // the main entry point versus the test runner entry point.
393+ // When isolation is 'none', both --require and --import are handled by the test runner.
394+ const shouldRequireAfterSetup = runnerEnabled && isolation === 'none' ;
395+ const shouldImportAfterSetup = runnerEnabled ;
396+
397+ it ( `should run required module ${ shouldRequireAfterSetup ? 'after' : 'before' } globalSetup` , async ( ) => {
392398 const setupFlagPath = tmpdir . resolve ( 'setup-for-required.tmp' ) ;
393399 const teardownFlagPath = tmpdir . resolve ( 'teardown-for-required.tmp' ) ;
394400
395- // Create a setup file for test-file.js to find
396401 fs . writeFileSync ( setupFlagPath , '' ) ;
397402
398403 const { stdout } = await runTest ( {
@@ -415,108 +420,63 @@ async function runTest(
415420 assert . match ( stdout , / G l o b a l s e t u p e x e c u t e d / ) ;
416421 assert . match ( stdout , / G l o b a l t e a r d o w n e x e c u t e d / ) ;
417422
418- // Verify that the required module was executed before the global setup
419423 const requiredExecutedPosition = stdout . indexOf ( 'Required module executed' ) ;
420424 const globalSetupExecutedPosition = stdout . indexOf ( 'Global setup executed' ) ;
421- assert . ok ( requiredExecutedPosition < globalSetupExecutedPosition ,
422- 'Required module should have been executed before global setup' ) ;
423425
424- // After all tests complete, the teardown should have run
426+ assert . ok (
427+ shouldRequireAfterSetup ?
428+ requiredExecutedPosition > globalSetupExecutedPosition :
429+ requiredExecutedPosition < globalSetupExecutedPosition ,
430+ `Required module should have been executed ${ shouldRequireAfterSetup ? 'after' : 'before' } global setup`
431+ ) ;
432+
425433 assert . ok ( fs . existsSync ( teardownFlagPath ) , 'Teardown flag file should exist' ) ;
426434 const content = fs . readFileSync ( teardownFlagPath , 'utf8' ) ;
427435 assert . strictEqual ( content , 'Teardown was executed' ) ;
428-
429- // Setup flag should have been removed by teardown
430436 assert . ok ( ! fs . existsSync ( setupFlagPath ) , 'Setup flag file should have been removed' ) ;
431437 } ) ;
432438
433- // This difference in behavior is due to the way --import is being handled by
434- // run_main entry point or test_runner entry point
435- if ( runnerEnabled ) {
436- it ( 'should run imported module after globalSetup' , async ( ) => {
437- const setupFlagPath = tmpdir . resolve ( 'setup-for-imported.tmp' ) ;
438- const teardownFlagPath = tmpdir . resolve ( 'teardown-for-imported.tmp' ) ;
439-
440- // Create a setup file for test-file.js to find
441- fs . writeFileSync ( setupFlagPath , 'non-empty' ) ;
442-
443- const { stdout } = await runTest ( {
444- isolation,
445- globalSetupFile : 'basic-setup-teardown.mjs' ,
446- importPath : './imported-module.js' ,
447- env : {
448- SETUP_FLAG_PATH : setupFlagPath ,
449- TEARDOWN_FLAG_PATH : teardownFlagPath
450- } ,
451- additionalFlags : [
452- `--import=${ esmpFile } ` ,
453- ] ,
454- runnerEnabled
455- } ) ;
456-
457- assert . match ( stdout , / p a s s 2 / ) ;
458- assert . match ( stdout , / f a i l 0 / ) ;
459- assert . match ( stdout , / I m p o r t e d m o d u l e e x e c u t e d / ) ;
460- assert . match ( stdout , / G l o b a l s e t u p e x e c u t e d / ) ;
461- assert . match ( stdout , / G l o b a l t e a r d o w n e x e c u t e d / ) ;
462-
463- // Verify that the imported module was executed after the global setup
464- const globalSetupExecutedPosition = stdout . indexOf ( 'Global setup executed' ) ;
465- const importedExecutedPosition = stdout . indexOf ( 'Imported module executed' ) ;
466- assert . ok ( globalSetupExecutedPosition < importedExecutedPosition ,
467- 'Imported module should be executed after global setup' ) ;
468-
469- // After all tests complete, the teardown should have run
470- assert . ok ( fs . existsSync ( teardownFlagPath ) , 'Teardown flag file should exist' ) ;
471- const content = fs . readFileSync ( teardownFlagPath , 'utf8' ) ;
472- assert . strictEqual ( content , 'Teardown was executed' ) ;
473-
474- // Setup flag should have been removed by teardown
475- assert . ok ( ! fs . existsSync ( setupFlagPath ) , 'Setup flag file should have been removed' ) ;
476- } ) ;
477- } else {
478- it ( 'should run imported module before globalSetup' , async ( ) => {
479- const setupFlagPath = tmpdir . resolve ( 'setup-for-imported.tmp' ) ;
480- const teardownFlagPath = tmpdir . resolve ( 'teardown-for-imported.tmp' ) ;
481-
482- // Create a setup file for test-file.js to find
483- fs . writeFileSync ( setupFlagPath , 'non-empty' ) ;
484-
485- const { stdout } = await runTest ( {
486- isolation,
487- globalSetupFile : 'basic-setup-teardown.mjs' ,
488- importPath : './imported-module.js' ,
489- env : {
490- SETUP_FLAG_PATH : setupFlagPath ,
491- TEARDOWN_FLAG_PATH : teardownFlagPath
492- } ,
493- additionalFlags : [
494- `--import=${ esmpFile } ` ,
495- ] ,
496- runnerEnabled
497- } ) ;
498-
499- assert . match ( stdout , / p a s s 2 / ) ;
500- assert . match ( stdout , / f a i l 0 / ) ;
501- assert . match ( stdout , / I m p o r t e d m o d u l e e x e c u t e d / ) ;
502- assert . match ( stdout , / G l o b a l s e t u p e x e c u t e d / ) ;
503- assert . match ( stdout , / G l o b a l t e a r d o w n e x e c u t e d / ) ;
504-
505- // Verify that the imported module was executed before the global setup
506- const importedExecutedPosition = stdout . indexOf ( 'Imported module executed' ) ;
507- const globalSetupExecutedPosition = stdout . indexOf ( 'Global setup executed' ) ;
508- assert . ok ( importedExecutedPosition < globalSetupExecutedPosition ,
509- 'Imported module should be executed before global setup' ) ;
510-
511- // After all tests complete, the teardown should have run
512- assert . ok ( fs . existsSync ( teardownFlagPath ) , 'Teardown flag file should exist' ) ;
513- const content = fs . readFileSync ( teardownFlagPath , 'utf8' ) ;
514- assert . strictEqual ( content , 'Teardown was executed' ) ;
515-
516- // Setup flag should have been removed by teardown
517- assert . ok ( ! fs . existsSync ( setupFlagPath ) , 'Setup flag file should have been removed' ) ;
439+ it ( `should run imported module ${ shouldImportAfterSetup ? 'after' : 'before' } globalSetup` , async ( ) => {
440+ const setupFlagPath = tmpdir . resolve ( 'setup-for-imported.tmp' ) ;
441+ const teardownFlagPath = tmpdir . resolve ( 'teardown-for-imported.tmp' ) ;
442+
443+ fs . writeFileSync ( setupFlagPath , 'non-empty' ) ;
444+
445+ const { stdout } = await runTest ( {
446+ isolation,
447+ globalSetupFile : 'basic-setup-teardown.mjs' ,
448+ importPath : './imported-module.js' ,
449+ env : {
450+ SETUP_FLAG_PATH : setupFlagPath ,
451+ TEARDOWN_FLAG_PATH : teardownFlagPath
452+ } ,
453+ additionalFlags : [
454+ `--import=${ esmpFile } ` ,
455+ ] ,
456+ runnerEnabled
518457 } ) ;
519- }
458+
459+ assert . match ( stdout , / p a s s 2 / ) ;
460+ assert . match ( stdout , / f a i l 0 / ) ;
461+ assert . match ( stdout , / I m p o r t e d m o d u l e e x e c u t e d / ) ;
462+ assert . match ( stdout , / G l o b a l s e t u p e x e c u t e d / ) ;
463+ assert . match ( stdout , / G l o b a l t e a r d o w n e x e c u t e d / ) ;
464+
465+ const importedExecutedPosition = stdout . indexOf ( 'Imported module executed' ) ;
466+ const globalSetupExecutedPosition = stdout . indexOf ( 'Global setup executed' ) ;
467+
468+ assert . ok (
469+ shouldImportAfterSetup ?
470+ importedExecutedPosition > globalSetupExecutedPosition :
471+ importedExecutedPosition < globalSetupExecutedPosition ,
472+ `Imported module should have been executed ${ shouldImportAfterSetup ? 'after' : 'before' } global setup`
473+ ) ;
474+
475+ assert . ok ( fs . existsSync ( teardownFlagPath ) , 'Teardown flag file should exist' ) ;
476+ const content = fs . readFileSync ( teardownFlagPath , 'utf8' ) ;
477+ assert . strictEqual ( content , 'Teardown was executed' ) ;
478+ assert . ok ( ! fs . existsSync ( setupFlagPath ) , 'Setup flag file should have been removed' ) ;
479+ } ) ;
520480
521481 it ( 'should execute globalSetup and globalTeardown correctly with imported module containing tests' , async ( ) => {
522482 const setupFlagPath = tmpdir . resolve ( 'setup-executed.tmp' ) ;
0 commit comments