@@ -575,28 +575,29 @@ describe('Loader hooks', { concurrency: true }, () => {
575575    assert . strictEqual ( signal ,  null ) ; 
576576  } ) ; 
577577
578-   it ( 'should invoke `initialize` correctly' ,  async  ( )  =>  { 
579-     const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
580-       '--no-warnings' , 
581-       '--experimental-loader' , 
582-       fixtures . fileURL ( 'es-module-loaders/hooks-initialize.mjs' ) , 
583-       '--input-type=module' , 
584-       '--eval' , 
585-       'import os from "node:os";' , 
586-     ] ) ; 
578+   describe ( '`initialize`/`register`' ,  ( )  =>  { 
579+     it ( 'should invoke `initialize` correctly' ,  async  ( )  =>  { 
580+       const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
581+         '--no-warnings' , 
582+         '--experimental-loader' , 
583+         fixtures . fileURL ( 'es-module-loaders/hooks-initialize.mjs' ) , 
584+         '--input-type=module' , 
585+         '--eval' , 
586+         'import os from "node:os";' , 
587+       ] ) ; 
587588
588-     assert . strictEqual ( stderr ,  '' ) ; 
589-     assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [ 'hooks initialize 1' ,  '' ] ) ; 
590-     assert . strictEqual ( code ,  0 ) ; 
591-     assert . strictEqual ( signal ,  null ) ; 
592-   } ) ; 
589+        assert . strictEqual ( stderr ,  '' ) ; 
590+        assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [ 'hooks initialize 1' ,  '' ] ) ; 
591+        assert . strictEqual ( code ,  0 ) ; 
592+        assert . strictEqual ( signal ,  null ) ; 
593+      } ) ; 
593594
594-   it ( 'should allow communicating with loader via `register` ports' ,  async  ( )  =>  { 
595-     const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
596-       '--no-warnings' , 
597-       '--input-type=module' , 
598-       '--eval' , 
599-       ` 
595+      it ( 'should allow communicating with loader via `register` ports' ,  async  ( )  =>  { 
596+        const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
597+          '--no-warnings' , 
598+          '--input-type=module' , 
599+          '--eval' , 
600+          ` 
600601        import {MessageChannel} from 'node:worker_threads'; 
601602        import {register} from 'node:module'; 
602603        import {once} from 'node:events'; 
@@ -617,25 +618,25 @@ describe('Loader hooks', { concurrency: true }, () => {
617618        ]); 
618619        clearTimeout(timeout); 
619620        port1.close(); 
620-       ` , 
621-     ] ) ; 
621+          ` , 
622+        ] ) ; 
622623
623-     assert . strictEqual ( stderr ,  '' ) ; 
624-     assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [  'register undefined' , 
625-                                                  'message initialize' , 
626-                                                  'message resolve node:os' , 
627-                                                  ''  ] ) ; 
624+        assert . strictEqual ( stderr ,  '' ) ; 
625+        assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [  'register undefined' , 
626+                                                     'message initialize' , 
627+                                                     'message resolve node:os' , 
628+                                                     ''  ] ) ; 
628629
629-     assert . strictEqual ( code ,  0 ) ; 
630-     assert . strictEqual ( signal ,  null ) ; 
631-   } ) ; 
630+        assert . strictEqual ( code ,  0 ) ; 
631+        assert . strictEqual ( signal ,  null ) ; 
632+      } ) ; 
632633
633-   it ( 'should have `register` work with cjs' ,  async  ( )  =>  { 
634-     const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
635-       '--no-warnings' , 
636-       '--input-type=commonjs' , 
637-       '--eval' , 
638-       ` 
634+      it ( 'should have `register` work with cjs' ,  async  ( )  =>  { 
635+        const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
636+          '--no-warnings' , 
637+          '--input-type=commonjs' , 
638+          '--eval' , 
639+          ` 
639640        'use strict'; 
640641        const {register} = require('node:module'); 
641642        register( 
@@ -648,56 +649,54 @@ describe('Loader hooks', { concurrency: true }, () => {
648649        import('node:os').then((result) => { 
649650          console.log(JSON.stringify(result)); 
650651        }); 
651-       ` , 
652-     ] ) ; 
652+          ` , 
653+        ] ) ; 
653654
654-     assert . strictEqual ( stderr ,  '' ) ; 
655-     assert . deepStrictEqual ( stdout . split ( '\n' ) . sort ( ) ,  [ 'hooks initialize 1' ,  '{"default":"foo"}' ,  '' ] . sort ( ) ) ; 
655+        assert . strictEqual ( stderr ,  '' ) ; 
656+        assert . deepStrictEqual ( stdout . split ( '\n' ) . sort ( ) ,  [ 'hooks initialize 1' ,  '{"default":"foo"}' ,  '' ] . sort ( ) ) ; 
656657
657-     assert . strictEqual ( code ,  0 ) ; 
658-     assert . strictEqual ( signal ,  null ) ; 
659-   } ) ; 
658+        assert . strictEqual ( code ,  0 ) ; 
659+        assert . strictEqual ( signal ,  null ) ; 
660+      } ) ; 
660661
661-   it ( '`register` should work with `require`' ,  async  ( )  =>  { 
662-     const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
663-       '--no-warnings' , 
664-       '--require' , 
665-       fixtures . path ( 'es-module-loaders/register-loader.cjs' ) , 
666-       '--input-type=module' , 
667-       '--eval' , 
668-       'import "node:os";' , 
669-     ] ) ; 
662+      it ( '`register` should work with `require`' ,  async  ( )  =>  { 
663+        const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
664+          '--no-warnings' , 
665+          '--require' , 
666+          fixtures . path ( 'es-module-loaders/register-loader.cjs' ) , 
667+          '--input-type=module' , 
668+          '--eval' , 
669+          'import "node:os";' , 
670+        ] ) ; 
670671
671-     assert . strictEqual ( stderr ,  '' ) ; 
672-     assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [ 'resolve passthru' ,  'resolve passthru' ,  '' ] ) ; 
673-     assert . strictEqual ( code ,  0 ) ; 
674-     assert . strictEqual ( signal ,  null ) ; 
675-   } ) ; 
672+        assert . strictEqual ( stderr ,  '' ) ; 
673+        assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [ 'resolve passthru' ,  'resolve passthru' ,  '' ] ) ; 
674+        assert . strictEqual ( code ,  0 ) ; 
675+        assert . strictEqual ( signal ,  null ) ; 
676+      } ) ; 
676677
677-   it ( '`register` should work with `import`' ,  async  ( )  =>  { 
678-     const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
679-       '--no-warnings' , 
680-       '--import' , 
681-       fixtures . fileURL ( 'es-module-loaders/register-loader.mjs' ) , 
682-       '--input-type=module' , 
683-       '--eval' , 
684-       ` 
685-         import 'node:os'; 
686-       ` , 
687-     ] ) ; 
678+     it ( '`register` should work with `import`' ,  async  ( )  =>  { 
679+       const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
680+         '--no-warnings' , 
681+         '--import' , 
682+         fixtures . fileURL ( 'es-module-loaders/register-loader.mjs' ) , 
683+         '--input-type=module' , 
684+         '--eval' , 
685+         'import "node:os"' , 
686+       ] ) ; 
688687
689-     assert . strictEqual ( stderr ,  '' ) ; 
690-     assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [ 'resolve passthru' ,  '' ] ) ; 
691-     assert . strictEqual ( code ,  0 ) ; 
692-     assert . strictEqual ( signal ,  null ) ; 
693-   } ) ; 
688+        assert . strictEqual ( stderr ,  '' ) ; 
689+        assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [ 'resolve passthru' ,  '' ] ) ; 
690+        assert . strictEqual ( code ,  0 ) ; 
691+        assert . strictEqual ( signal ,  null ) ; 
692+      } ) ; 
694693
695-   it ( 'should execute `initialize` in sequence' ,  async  ( )  =>  { 
696-     const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
697-       '--no-warnings' , 
698-       '--input-type=module' , 
699-       '--eval' , 
700-       ` 
694+      it ( 'should execute `initialize` in sequence' ,  async  ( )  =>  { 
695+        const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
696+          '--no-warnings' , 
697+          '--input-type=module' , 
698+          '--eval' , 
699+          ` 
701700        import {register} from 'node:module'; 
702701        console.log('result 1', register( 
703702          ${ JSON . stringify ( fixtures . fileURL ( 'es-module-loaders/hooks-initialize.mjs' ) ) }  
@@ -707,17 +706,86 @@ describe('Loader hooks', { concurrency: true }, () => {
707706        )); 
708707
709708        await import('node:os'); 
710-       ` , 
711-     ] ) ; 
709+          ` , 
710+        ] ) ; 
712711
713-     assert . strictEqual ( stderr ,  '' ) ; 
714-     assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [  'hooks initialize 1' , 
715-                                                  'result 1 undefined' , 
716-                                                  'hooks initialize 2' , 
717-                                                  'result 2 undefined' , 
718-                                                  ''  ] ) ; 
719-     assert . strictEqual ( code ,  0 ) ; 
720-     assert . strictEqual ( signal ,  null ) ; 
712+       assert . strictEqual ( stderr ,  '' ) ; 
713+       assert . deepStrictEqual ( stdout . split ( '\n' ) ,  [  'hooks initialize 1' , 
714+                                                    'result 1 undefined' , 
715+                                                    'hooks initialize 2' , 
716+                                                    'result 2 undefined' , 
717+                                                    ''  ] ) ; 
718+       assert . strictEqual ( code ,  0 ) ; 
719+       assert . strictEqual ( signal ,  null ) ; 
720+     } ) ; 
721+ 
722+     it ( 'should handle `initialize` returning never-settling promise' ,  async  ( )  =>  { 
723+       const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
724+         '--no-warnings' , 
725+         '--input-type=module' , 
726+         '--eval' , 
727+         ` 
728+         import {register} from 'node:module'; 
729+         register('data:text/javascript,export function initialize(){return new Promise(()=>{})}'); 
730+         ` , 
731+       ] ) ; 
732+ 
733+       assert . strictEqual ( stderr ,  '' ) ; 
734+       assert . strictEqual ( stdout ,  '' ) ; 
735+       assert . strictEqual ( code ,  13 ) ; 
736+       assert . strictEqual ( signal ,  null ) ; 
737+     } ) ; 
738+ 
739+     it ( 'should handle `initialize` returning rejecting promise' ,  async  ( )  =>  { 
740+       const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
741+         '--no-warnings' , 
742+         '--input-type=module' , 
743+         '--eval' , 
744+         ` 
745+         import {register} from 'node:module'; 
746+         register('data:text/javascript,export function initialize(){return Promise.reject()}'); 
747+         ` , 
748+       ] ) ; 
749+ 
750+       assert . match ( stderr ,  / u n d e f i n e d \r ? \n / ) ; 
751+       assert . strictEqual ( stdout ,  '' ) ; 
752+       assert . strictEqual ( code ,  1 ) ; 
753+       assert . strictEqual ( signal ,  null ) ; 
754+     } ) ; 
755+ 
756+     it ( 'should handle `initialize` throwing null' ,  async  ( )  =>  { 
757+       const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
758+         '--no-warnings' , 
759+         '--input-type=module' , 
760+         '--eval' , 
761+         ` 
762+         import {register} from 'node:module'; 
763+         register('data:text/javascript,export function initialize(){throw null}'); 
764+         ` , 
765+       ] ) ; 
766+ 
767+       assert . match ( stderr ,  / n u l l \r ? \n / ) ; 
768+       assert . strictEqual ( stdout ,  '' ) ; 
769+       assert . strictEqual ( code ,  1 ) ; 
770+       assert . strictEqual ( signal ,  null ) ; 
771+     } ) ; 
772+ 
773+     it ( 'should be fine to call `process.exit` from a initialize hook' ,  async  ( )  =>  { 
774+       const  {  code,  signal,  stdout,  stderr }  =  await  spawnPromisified ( execPath ,  [ 
775+         '--no-warnings' , 
776+         '--input-type=module' , 
777+         '--eval' , 
778+         ` 
779+         import {register} from 'node:module'; 
780+         register('data:text/javascript,export function initialize(){process.exit(42);}'); 
781+         ` , 
782+       ] ) ; 
783+ 
784+       assert . strictEqual ( stderr ,  '' ) ; 
785+       assert . strictEqual ( stdout ,  '' ) ; 
786+       assert . strictEqual ( code ,  42 ) ; 
787+       assert . strictEqual ( signal ,  null ) ; 
788+     } ) ; 
721789  } ) ; 
722790
723791  it ( 'should use CJS loader to respond to require.resolve calls by default' ,  async  ( )  =>  { 
0 commit comments