@@ -575,28 +575,29 @@ describe('Loader hooks', { concurrency: true }, () => {
575
575
assert . strictEqual ( signal , null ) ;
576
576
} ) ;
577
577
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
+ ] ) ;
587
588
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
+ } ) ;
593
594
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
+ `
600
601
import {MessageChannel} from 'node:worker_threads';
601
602
import {register} from 'node:module';
602
603
import {once} from 'node:events';
@@ -618,24 +619,24 @@ describe('Loader hooks', { concurrency: true }, () => {
618
619
clearTimeout(timeout);
619
620
port1.close();
620
621
` ,
621
- ] ) ;
622
+ ] ) ;
622
623
623
- assert . strictEqual ( stderr , '' ) ;
624
- assert . deepStrictEqual ( stdout . split ( '\n' ) , [ 'register ok' ,
625
- 'message initialize' ,
626
- 'message resolve node:os' ,
627
- '' ] ) ;
624
+ assert . strictEqual ( stderr , '' ) ;
625
+ assert . deepStrictEqual ( stdout . split ( '\n' ) , [ 'register ok' ,
626
+ 'message initialize' ,
627
+ 'message resolve node:os' ,
628
+ '' ] ) ;
628
629
629
- assert . strictEqual ( code , 0 ) ;
630
- assert . strictEqual ( signal , null ) ;
631
- } ) ;
630
+ assert . strictEqual ( code , 0 ) ;
631
+ assert . strictEqual ( signal , null ) ;
632
+ } ) ;
632
633
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
+ `
639
640
'use strict';
640
641
const {register} = require('node:module');
641
642
register(
@@ -649,55 +650,55 @@ describe('Loader hooks', { concurrency: true }, () => {
649
650
console.log(JSON.stringify(result));
650
651
});
651
652
` ,
652
- ] ) ;
653
+ ] ) ;
653
654
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 ( ) ) ;
656
657
657
- assert . strictEqual ( code , 0 ) ;
658
- assert . strictEqual ( signal , null ) ;
659
- } ) ;
658
+ assert . strictEqual ( code , 0 ) ;
659
+ assert . strictEqual ( signal , null ) ;
660
+ } ) ;
660
661
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
+ ] ) ;
670
671
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
+ } ) ;
676
677
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
- `
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
+ `
685
686
import 'node:os';
686
687
` ,
687
- ] ) ;
688
+ ] ) ;
688
689
689
- assert . strictEqual ( stderr , '' ) ;
690
- assert . deepStrictEqual ( stdout . split ( '\n' ) , [ 'resolve passthru' , '' ] ) ;
691
- assert . strictEqual ( code , 0 ) ;
692
- assert . strictEqual ( signal , null ) ;
693
- } ) ;
690
+ assert . strictEqual ( stderr , '' ) ;
691
+ assert . deepStrictEqual ( stdout . split ( '\n' ) , [ 'resolve passthru' , '' ] ) ;
692
+ assert . strictEqual ( code , 0 ) ;
693
+ assert . strictEqual ( signal , null ) ;
694
+ } ) ;
694
695
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
- `
696
+ it ( 'should execute `initialize` in sequence' , async ( ) => {
697
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
698
+ '--no-warnings' ,
699
+ '--input-type=module' ,
700
+ '--eval' ,
701
+ `
701
702
import {register} from 'node:module';
702
703
console.log('result', register(
703
704
${ JSON . stringify ( fixtures . fileURL ( 'es-module-loaders/hooks-initialize.mjs' ) ) }
@@ -708,16 +709,85 @@ describe('Loader hooks', { concurrency: true }, () => {
708
709
709
710
await import('node:os');
710
711
` ,
711
- ] ) ;
712
+ ] ) ;
712
713
713
- assert . strictEqual ( stderr , '' ) ;
714
- assert . deepStrictEqual ( stdout . split ( '\n' ) , [ 'hooks initialize 1' ,
715
- 'result 1' ,
716
- 'hooks initialize 2' ,
717
- 'result 2' ,
718
- '' ] ) ;
719
- assert . strictEqual ( code , 0 ) ;
720
- assert . strictEqual ( signal , null ) ;
714
+ assert . strictEqual ( stderr , '' ) ;
715
+ assert . deepStrictEqual ( stdout . split ( '\n' ) , [ 'hooks initialize 1' ,
716
+ 'result 1' ,
717
+ 'hooks initialize 2' ,
718
+ 'result 2' ,
719
+ '' ] ) ;
720
+ assert . strictEqual ( code , 0 ) ;
721
+ assert . strictEqual ( signal , null ) ;
722
+ } ) ;
723
+
724
+ it ( 'should handle `initialize` returning never-settling promise' , async ( ) => {
725
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
726
+ '--no-warnings' ,
727
+ '--input-type=module' ,
728
+ '--eval' ,
729
+ `
730
+ import {register} from 'node:module';
731
+ register('data:text/javascript,export function initialize(){return new Promise(()=>{})}');
732
+ ` ,
733
+ ] ) ;
734
+
735
+ assert . strictEqual ( stderr , '' ) ;
736
+ assert . strictEqual ( stdout , '' ) ;
737
+ assert . strictEqual ( code , 13 ) ;
738
+ assert . strictEqual ( signal , null ) ;
739
+ } ) ;
740
+
741
+ it ( 'should handle `initialize` returning rejecting promise' , async ( ) => {
742
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
743
+ '--no-warnings' ,
744
+ '--input-type=module' ,
745
+ '--eval' ,
746
+ `
747
+ import {register} from 'node:module';
748
+ register('data:text/javascript,export function initialize(){return Promise.reject()}');
749
+ ` ,
750
+ ] ) ;
751
+
752
+ assert . match ( stderr , / u n d e f i n e d \r ? \n / ) ;
753
+ assert . strictEqual ( stdout , '' ) ;
754
+ assert . strictEqual ( code , 1 ) ;
755
+ assert . strictEqual ( signal , null ) ;
756
+ } ) ;
757
+
758
+ it ( 'should handle `initialize` throwing null' , async ( ) => {
759
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
760
+ '--no-warnings' ,
761
+ '--input-type=module' ,
762
+ '--eval' ,
763
+ `
764
+ import {register} from 'node:module';
765
+ register('data:text/javascript,export function initialize(){throw null}');
766
+ ` ,
767
+ ] ) ;
768
+
769
+ assert . match ( stderr , / n u l l \r ? \n / ) ;
770
+ assert . strictEqual ( stdout , '' ) ;
771
+ assert . strictEqual ( code , 1 ) ;
772
+ assert . strictEqual ( signal , null ) ;
773
+ } ) ;
774
+
775
+ it ( 'should be fine to call `process.exit` from a initialize hook' , async ( ) => {
776
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
777
+ '--no-warnings' ,
778
+ '--input-type=module' ,
779
+ '--eval' ,
780
+ `
781
+ import {register} from 'node:module';
782
+ register('data:text/javascript,export function initialize(){process.exit(42);}');
783
+ ` ,
784
+ ] ) ;
785
+
786
+ assert . strictEqual ( stderr , '' ) ;
787
+ assert . strictEqual ( stdout , '' ) ;
788
+ assert . strictEqual ( code , 42 ) ;
789
+ assert . strictEqual ( signal , null ) ;
790
+ } ) ;
721
791
} ) ;
722
792
723
793
it ( 'should use CJS loader to respond to require.resolve calls by default' , async ( ) => {
0 commit comments