@@ -10,7 +10,7 @@ const fixtures = require('../common/fixtures');
1010const assert = require ( 'node:assert' ) ;
1111const { relative } = require ( 'node:path' ) ;
1212const { test } = require ( 'node:test' ) ;
13- const { fileURLToPath , pathToFileURL } = require ( 'node:url' ) ;
13+ const { pathToFileURL } = require ( 'node:url' ) ;
1414
1515test ( 'input validation' , async ( t ) => {
1616 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) => {
514514 const cjsMock = t . mock . module ( cjsFixture , {
515515 namedExports : { fn ( ) { return 42 ; } } ,
516516 } ) ;
517- let esmImpl = await import ( pathToFileURL ( cjsFixture ) ) ;
517+ let esmImpl = await import ( cjsFixture ) ;
518518 let cjsImpl = require ( cjsFixture ) ;
519519
520520 assert . strictEqual ( esmImpl . fn ( ) , 42 ) ;
521521 assert . strictEqual ( cjsImpl . fn ( ) , 42 ) ;
522522
523523 cjsMock . restore ( ) ;
524524
525- esmImpl = await import ( pathToFileURL ( cjsFixture ) ) ;
525+ esmImpl = await import ( cjsFixture ) ;
526526 cjsImpl = require ( cjsFixture ) ;
527527
528528 assert . strictEqual ( esmImpl . default . string , 'original cjs string' ) ;
529529 assert . strictEqual ( cjsImpl . string , 'original cjs string' ) ;
530530} ) ;
531531
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+
532552test ( 'relative paths can be used by both module systems' , async ( t ) => {
533553 const fixture = relative (
534554 __dirname , fixtures . path ( 'module-mocking' , 'basic-esm.mjs' )
@@ -566,7 +586,9 @@ test('node_modules can be used by both module systems', async (t) => {
566586} ) ;
567587
568588test ( '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 ;
570592 const mock = t . mock . module ( fixture , {
571593 namedExports : { fn ( ) { return 42 ; } } ,
572594 } ) ;
@@ -582,9 +604,9 @@ test('file:// imports are supported in ESM only', async (t) => {
582604} ) ;
583605
584606test ( '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 , {
588610 namedExports : { fn ( ) { return 42 ; } } ,
589611 } ) ;
590612 const mockedImpl = await import ( mockedFixture ) ;
@@ -603,18 +625,18 @@ test('defaultExports work with CJS mocks in both module systems', async (t) => {
603625 assert . strictEqual ( original . string , 'original cjs string' ) ;
604626 t . mock . module ( fixture , { defaultExport } ) ;
605627 assert . strictEqual ( require ( fixture ) , defaultExport ) ;
606- assert . strictEqual ( ( await import ( pathToFileURL ( fixture ) ) ) . default , defaultExport ) ;
628+ assert . strictEqual ( ( await import ( fixture ) ) . default , defaultExport ) ;
607629} ) ;
608630
609631test ( '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' ) ;
611633 const original = await import ( fixture ) ;
612634 const defaultExport = Symbol ( 'default' ) ;
613635
614636 assert . strictEqual ( original . string , 'original esm string' ) ;
615- t . mock . module ( ` ${ fixture } ` , { defaultExport } ) ;
637+ t . mock . module ( fixture , { defaultExport } ) ;
616638 assert . strictEqual ( ( await import ( fixture ) ) . default , defaultExport ) ;
617- assert . strictEqual ( require ( fileURLToPath ( fixture ) ) , defaultExport ) ;
639+ assert . strictEqual ( require ( fixture ) , defaultExport ) ;
618640} ) ;
619641
620642test ( 'wrong import syntax should throw error after module mocking.' , async ( ) => {
0 commit comments