@@ -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 { pathToFileURL } = require ( 'node:url' ) ;
13
+ const { fileURLToPath , 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,41 +514,21 @@ 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 ( cjsFixture ) ;
517
+ let esmImpl = await import ( pathToFileURL ( 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 ( cjsFixture ) ;
525
+ esmImpl = await import ( pathToFileURL ( 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
-
552
532
test ( 'relative paths can be used by both module systems' , async ( t ) => {
553
533
const fixture = relative (
554
534
__dirname , fixtures . path ( 'module-mocking' , 'basic-esm.mjs' )
@@ -586,9 +566,7 @@ test('node_modules can be used by both module systems', async (t) => {
586
566
} ) ;
587
567
588
568
test ( 'file:// imports are supported in ESM only' , async ( t ) => {
589
- const fixture = pathToFileURL (
590
- fixtures . path ( 'module-mocking' , 'basic-esm.mjs' )
591
- ) . href ;
569
+ const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) . href ;
592
570
const mock = t . mock . module ( fixture , {
593
571
namedExports : { fn ( ) { return 42 ; } } ,
594
572
} ) ;
@@ -604,9 +582,9 @@ test('file:// imports are supported in ESM only', async (t) => {
604
582
} ) ;
605
583
606
584
test ( 'mocked modules do not impact unmocked modules' , async ( t ) => {
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 , {
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 } ` , {
610
588
namedExports : { fn ( ) { return 42 ; } } ,
611
589
} ) ;
612
590
const mockedImpl = await import ( mockedFixture ) ;
@@ -625,18 +603,18 @@ test('defaultExports work with CJS mocks in both module systems', async (t) => {
625
603
assert . strictEqual ( original . string , 'original cjs string' ) ;
626
604
t . mock . module ( fixture , { defaultExport } ) ;
627
605
assert . strictEqual ( require ( fixture ) , defaultExport ) ;
628
- assert . strictEqual ( ( await import ( fixture ) ) . default , defaultExport ) ;
606
+ assert . strictEqual ( ( await import ( pathToFileURL ( fixture ) ) ) . default , defaultExport ) ;
629
607
} ) ;
630
608
631
609
test ( 'defaultExports work with ESM mocks in both module systems' , async ( t ) => {
632
- const fixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
610
+ const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) ;
633
611
const original = await import ( fixture ) ;
634
612
const defaultExport = Symbol ( 'default' ) ;
635
613
636
614
assert . strictEqual ( original . string , 'original esm string' ) ;
637
- t . mock . module ( fixture , { defaultExport } ) ;
615
+ t . mock . module ( ` ${ fixture } ` , { defaultExport } ) ;
638
616
assert . strictEqual ( ( await import ( fixture ) ) . default , defaultExport ) ;
639
- assert . strictEqual ( require ( fixture ) , defaultExport ) ;
617
+ assert . strictEqual ( require ( fileURLToPath ( fixture ) ) , defaultExport ) ;
640
618
} ) ;
641
619
642
620
test ( 'wrong import syntax should throw error after module mocking.' , async ( ) => {
0 commit comments