@@ -10,7 +10,7 @@ const fixtures = require('../common/fixtures');
10
10
const assert = require ( 'node:assert' ) ;
11
11
const { relative } = require ( 'node:path' ) ;
12
12
const { test } = require ( 'node:test' ) ;
13
- const { fileURLToPath , pathToFileURL } = require ( 'node:url' ) ;
13
+ const { pathToFileURL } = require ( 'node:url' ) ;
14
14
15
15
test ( 'input validation' , async ( t ) => {
16
16
await t . test ( 'throws if specifier is not a string' , ( t ) => {
@@ -514,21 +514,41 @@ test('CJS mocks can be used by both module systems', async (t) => {
514
514
const cjsMock = t . mock . module ( cjsFixture , {
515
515
namedExports : { fn ( ) { return 42 ; } } ,
516
516
} ) ;
517
- let esmImpl = await import ( pathToFileURL ( cjsFixture ) ) ;
517
+ let esmImpl = await import ( cjsFixture ) ;
518
518
let cjsImpl = require ( cjsFixture ) ;
519
519
520
520
assert . strictEqual ( esmImpl . fn ( ) , 42 ) ;
521
521
assert . strictEqual ( cjsImpl . fn ( ) , 42 ) ;
522
522
523
523
cjsMock . restore ( ) ;
524
524
525
- esmImpl = await import ( pathToFileURL ( cjsFixture ) ) ;
525
+ esmImpl = await import ( cjsFixture ) ;
526
526
cjsImpl = require ( cjsFixture ) ;
527
527
528
528
assert . strictEqual ( esmImpl . default . string , 'original cjs string' ) ;
529
529
assert . strictEqual ( cjsImpl . string , 'original cjs string' ) ;
530
530
} ) ;
531
531
532
+ test ( 'ESM mocks can be used by both module systems' , async ( t ) => {
533
+ const esmFixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
534
+ const esmMock = t . mock . module ( esmFixture , {
535
+ namedExports : { fn ( ) { return 42 ; } } ,
536
+ } ) ;
537
+
538
+ let cjsImpl = require ( esmFixture ) ;
539
+ let esmImpl = await import ( esmFixture ) ;
540
+
541
+ assert . strictEqual ( cjsImpl . fn ( ) , 42 ) ;
542
+ assert . strictEqual ( esmImpl . fn ( ) , 42 ) ;
543
+
544
+ esmMock . restore ( ) ;
545
+ cjsImpl = require ( esmFixture ) ;
546
+ esmImpl = await import ( esmFixture ) ;
547
+
548
+ assert . strictEqual ( esmImpl . string , 'original esm string' ) ;
549
+ assert . strictEqual ( cjsImpl . string , 'original esm string' ) ;
550
+ } ) ;
551
+
532
552
test ( 'relative paths can be used by both module systems' , async ( t ) => {
533
553
const fixture = relative (
534
554
__dirname , fixtures . path ( 'module-mocking' , 'basic-esm.mjs' )
@@ -566,7 +586,9 @@ test('node_modules can be used by both module systems', async (t) => {
566
586
} ) ;
567
587
568
588
test ( 'file:// imports are supported in ESM only' , async ( t ) => {
569
- const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) . href ;
589
+ const fixture = pathToFileURL (
590
+ fixtures . path ( 'module-mocking' , 'basic-esm.mjs' )
591
+ ) . href ;
570
592
const mock = t . mock . module ( fixture , {
571
593
namedExports : { fn ( ) { return 42 ; } } ,
572
594
} ) ;
@@ -582,9 +604,9 @@ test('file:// imports are supported in ESM only', async (t) => {
582
604
} ) ;
583
605
584
606
test ( 'mocked modules do not impact unmocked modules' , async ( t ) => {
585
- const mockedFixture = fixtures . fileURL ( 'module-mocking' , 'basic-cjs.js' ) ;
586
- const unmockedFixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) ;
587
- t . mock . module ( ` ${ mockedFixture } ` , {
607
+ const mockedFixture = fixtures . path ( 'module-mocking' , 'basic-cjs.js' ) ;
608
+ const unmockedFixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
609
+ t . mock . module ( mockedFixture , {
588
610
namedExports : { fn ( ) { return 42 ; } } ,
589
611
} ) ;
590
612
const mockedImpl = await import ( mockedFixture ) ;
@@ -603,18 +625,18 @@ test('defaultExports work with CJS mocks in both module systems', async (t) => {
603
625
assert . strictEqual ( original . string , 'original cjs string' ) ;
604
626
t . mock . module ( fixture , { defaultExport } ) ;
605
627
assert . strictEqual ( require ( fixture ) , defaultExport ) ;
606
- assert . strictEqual ( ( await import ( pathToFileURL ( fixture ) ) ) . default , defaultExport ) ;
628
+ assert . strictEqual ( ( await import ( fixture ) ) . default , defaultExport ) ;
607
629
} ) ;
608
630
609
631
test ( 'defaultExports work with ESM mocks in both module systems' , async ( t ) => {
610
- const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) ;
632
+ const fixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
611
633
const original = await import ( fixture ) ;
612
634
const defaultExport = Symbol ( 'default' ) ;
613
635
614
636
assert . strictEqual ( original . string , 'original esm string' ) ;
615
- t . mock . module ( ` ${ fixture } ` , { defaultExport } ) ;
637
+ t . mock . module ( fixture , { defaultExport } ) ;
616
638
assert . strictEqual ( ( await import ( fixture ) ) . default , defaultExport ) ;
617
- assert . strictEqual ( require ( fileURLToPath ( fixture ) ) , defaultExport ) ;
639
+ assert . strictEqual ( require ( fixture ) , defaultExport ) ;
618
640
} ) ;
619
641
620
642
test ( 'wrong import syntax should throw error after module mocking.' , async ( ) => {
0 commit comments