@@ -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 ) => {
@@ -154,7 +154,7 @@ test('CJS mocking with namedExports option', async (t) => {
154
154
assert . strictEqual ( original . string , 'original cjs string' ) ;
155
155
assert . strictEqual ( original . fn , undefined ) ;
156
156
157
- t . mock . module ( fixture , {
157
+ t . mock . module ( pathToFileURL ( fixture ) , {
158
158
namedExports : { fn ( ) { return 42 ; } } ,
159
159
} ) ;
160
160
const mocked = require ( fixture ) ;
@@ -174,7 +174,7 @@ test('CJS mocking with namedExports option', async (t) => {
174
174
assert . strictEqual ( original . string , 'original cjs string' ) ;
175
175
assert . strictEqual ( original . fn , undefined ) ;
176
176
177
- t . mock . module ( fixture , {
177
+ t . mock . module ( pathToFileURL ( fixture ) , {
178
178
namedExports : { fn ( ) { return 42 ; } } ,
179
179
cache : true ,
180
180
} ) ;
@@ -195,7 +195,7 @@ test('CJS mocking with namedExports option', async (t) => {
195
195
assert . strictEqual ( original . string , 'original cjs string' ) ;
196
196
assert . strictEqual ( original . fn , undefined ) ;
197
197
198
- t . mock . module ( fixture , {
198
+ t . mock . module ( pathToFileURL ( fixture ) , {
199
199
namedExports : { fn ( ) { return 42 ; } } ,
200
200
cache : false ,
201
201
} ) ;
@@ -219,7 +219,7 @@ test('CJS mocking with namedExports option', async (t) => {
219
219
220
220
const defaultExport = { val1 : 5 , val2 : 3 } ;
221
221
222
- t . mock . module ( fixture , {
222
+ t . mock . module ( pathToFileURL ( fixture ) , {
223
223
defaultExport,
224
224
namedExports : { val1 : 'mock value' } ,
225
225
} ) ;
@@ -242,7 +242,7 @@ test('CJS mocking with namedExports option', async (t) => {
242
242
243
243
const defaultExport = null ;
244
244
245
- t . mock . module ( fixture , {
245
+ t . mock . module ( pathToFileURL ( fixture ) , {
246
246
defaultExport,
247
247
namedExports : { val1 : 'mock value' } ,
248
248
} ) ;
@@ -256,7 +256,7 @@ test('CJS mocking with namedExports option', async (t) => {
256
256
257
257
test ( 'ESM mocking with namedExports option' , async ( t ) => {
258
258
await t . test ( 'does not cache by default' , async ( t ) => {
259
- const fixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
259
+ const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) ;
260
260
const original = await import ( fixture ) ;
261
261
262
262
assert . strictEqual ( original . string , 'original esm string' ) ;
@@ -276,7 +276,7 @@ test('ESM mocking with namedExports option', async (t) => {
276
276
} ) ;
277
277
278
278
await t . test ( 'explicitly enables caching' , async ( t ) => {
279
- const fixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
279
+ const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) ;
280
280
const original = await import ( fixture ) ;
281
281
282
282
assert . strictEqual ( original . string , 'original esm string' ) ;
@@ -297,7 +297,7 @@ test('ESM mocking with namedExports option', async (t) => {
297
297
} ) ;
298
298
299
299
await t . test ( 'explicitly disables caching' , async ( t ) => {
300
- const fixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
300
+ const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) ;
301
301
const original = await import ( fixture ) ;
302
302
303
303
assert . strictEqual ( original . string , 'original esm string' ) ;
@@ -318,7 +318,8 @@ test('ESM mocking with namedExports option', async (t) => {
318
318
} ) ;
319
319
320
320
await t . test ( 'named exports are not applied to defaultExport' , async ( t ) => {
321
- const fixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
321
+ const fixturePath = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
322
+ const fixture = pathToFileURL ( fixturePath ) ;
322
323
const original = await import ( fixture ) ;
323
324
324
325
assert . strictEqual ( original . string , 'original esm string' ) ;
@@ -338,11 +339,11 @@ test('ESM mocking with namedExports option', async (t) => {
338
339
assert . strictEqual ( mocked . default , 'mock default' ) ;
339
340
assert . strictEqual ( mocked . val1 , 'mock value' ) ;
340
341
t . mock . reset ( ) ;
341
- common . expectRequiredModule ( require ( fixture ) , original ) ;
342
+ common . expectRequiredModule ( require ( fixturePath ) , original ) ;
342
343
} ) ;
343
344
344
345
await t . test ( 'throws if named exports cannot be applied to defaultExport as CJS' , async ( t ) => {
345
- const fixture = fixtures . path ( 'module-mocking' , 'basic-cjs.js' ) ;
346
+ const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-cjs.js' ) ;
346
347
const original = await import ( fixture ) ;
347
348
348
349
assert . strictEqual ( original . default . string , 'original cjs string' ) ;
@@ -366,13 +367,14 @@ test('ESM mocking with namedExports option', async (t) => {
366
367
test ( 'modules cannot be mocked multiple times at once' , async ( t ) => {
367
368
await t . test ( 'CJS' , async ( t ) => {
368
369
const fixture = fixtures . path ( 'module-mocking' , 'basic-cjs.js' ) ;
370
+ const fixtureURL = pathToFileURL ( fixture ) . href ;
369
371
370
- t . mock . module ( fixture , {
372
+ t . mock . module ( fixtureURL , {
371
373
namedExports : { fn ( ) { return 42 ; } } ,
372
374
} ) ;
373
375
374
376
assert . throws ( ( ) => {
375
- t . mock . module ( fixture , {
377
+ t . mock . module ( fixtureURL , {
376
378
namedExports : { fn ( ) { return 55 ; } } ,
377
379
} ) ;
378
380
} , {
@@ -386,7 +388,7 @@ test('modules cannot be mocked multiple times at once', async (t) => {
386
388
} ) ;
387
389
388
390
await t . test ( 'ESM' , async ( t ) => {
389
- const fixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
391
+ const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) . href ;
390
392
391
393
t . mock . module ( fixture , {
392
394
namedExports : { fn ( ) { return 42 ; } } ,
@@ -409,10 +411,10 @@ test('modules cannot be mocked multiple times at once', async (t) => {
409
411
410
412
test ( 'mocks are automatically restored' , async ( t ) => {
411
413
const cjsFixture = fixtures . path ( 'module-mocking' , 'basic-cjs.js' ) ;
412
- const esmFixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
414
+ const esmFixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) ;
413
415
414
416
await t . test ( 'CJS' , async ( t ) => {
415
- t . mock . module ( cjsFixture , {
417
+ t . mock . module ( pathToFileURL ( cjsFixture ) , {
416
418
namedExports : { fn ( ) { return 42 ; } } ,
417
419
} ) ;
418
420
@@ -442,9 +444,9 @@ test('mocks are automatically restored', async (t) => {
442
444
443
445
test ( 'mocks can be restored independently' , async ( t ) => {
444
446
const cjsFixture = fixtures . path ( 'module-mocking' , 'basic-cjs.js' ) ;
445
- const esmFixture = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
447
+ const esmFixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) ;
446
448
447
- const cjsMock = t . mock . module ( cjsFixture , {
449
+ const cjsMock = t . mock . module ( pathToFileURL ( cjsFixture ) , {
448
450
namedExports : { fn ( ) { return 42 ; } } ,
449
451
} ) ;
450
452
@@ -511,18 +513,19 @@ test('node:- core module mocks can be used by both module systems', async (t) =>
511
513
512
514
test ( 'CJS mocks can be used by both module systems' , async ( t ) => {
513
515
const cjsFixture = fixtures . path ( 'module-mocking' , 'basic-cjs.js' ) ;
514
- const cjsMock = t . mock . module ( cjsFixture , {
516
+ const cjsFixtureURL = pathToFileURL ( cjsFixture ) ;
517
+ const cjsMock = t . mock . module ( cjsFixtureURL , {
515
518
namedExports : { fn ( ) { return 42 ; } } ,
516
519
} ) ;
517
- let esmImpl = await import ( pathToFileURL ( cjsFixture ) ) ;
520
+ let esmImpl = await import ( cjsFixtureURL ) ;
518
521
let cjsImpl = require ( cjsFixture ) ;
519
522
520
523
assert . strictEqual ( esmImpl . fn ( ) , 42 ) ;
521
524
assert . strictEqual ( cjsImpl . fn ( ) , 42 ) ;
522
525
523
526
cjsMock . restore ( ) ;
524
527
525
- esmImpl = await import ( pathToFileURL ( cjsFixture ) ) ;
528
+ esmImpl = await import ( cjsFixtureURL ) ;
526
529
cjsImpl = require ( cjsFixture ) ;
527
530
528
531
assert . strictEqual ( esmImpl . default . string , 'original cjs string' ) ;
@@ -532,7 +535,7 @@ test('CJS mocks can be used by both module systems', async (t) => {
532
535
test ( 'relative paths can be used by both module systems' , async ( t ) => {
533
536
const fixture = relative (
534
537
__dirname , fixtures . path ( 'module-mocking' , 'basic-esm.mjs' )
535
- ) ;
538
+ ) . replaceAll ( '\\' , '/' ) ;
536
539
const mock = t . mock . module ( fixture , {
537
540
namedExports : { fn ( ) { return 42 ; } } ,
538
541
} ) ;
@@ -597,24 +600,26 @@ test('mocked modules do not impact unmocked modules', async (t) => {
597
600
598
601
test ( 'defaultExports work with CJS mocks in both module systems' , async ( t ) => {
599
602
const fixture = fixtures . path ( 'module-mocking' , 'basic-cjs.js' ) ;
603
+ const fixtureURL = pathToFileURL ( fixture ) ;
600
604
const original = require ( fixture ) ;
601
605
const defaultExport = Symbol ( 'default' ) ;
602
606
603
607
assert . strictEqual ( original . string , 'original cjs string' ) ;
604
- t . mock . module ( fixture , { defaultExport } ) ;
608
+ t . mock . module ( fixtureURL , { defaultExport } ) ;
605
609
assert . strictEqual ( require ( fixture ) , defaultExport ) ;
606
- assert . strictEqual ( ( await import ( pathToFileURL ( fixture ) ) ) . default , defaultExport ) ;
610
+ assert . strictEqual ( ( await import ( fixtureURL ) ) . default , defaultExport ) ;
607
611
} ) ;
608
612
609
613
test ( 'defaultExports work with ESM mocks in both module systems' , async ( t ) => {
610
- const fixture = fixtures . fileURL ( 'module-mocking' , 'basic-esm.mjs' ) ;
614
+ const fixturePath = fixtures . path ( 'module-mocking' , 'basic-esm.mjs' ) ;
615
+ const fixture = pathToFileURL ( fixturePath ) ;
611
616
const original = await import ( fixture ) ;
612
617
const defaultExport = Symbol ( 'default' ) ;
613
618
614
619
assert . strictEqual ( original . string , 'original esm string' ) ;
615
620
t . mock . module ( `${ fixture } ` , { defaultExport } ) ;
616
621
assert . strictEqual ( ( await import ( fixture ) ) . default , defaultExport ) ;
617
- assert . strictEqual ( require ( fileURLToPath ( fixture ) ) , defaultExport ) ;
622
+ assert . strictEqual ( require ( fixturePath ) , defaultExport ) ;
618
623
} ) ;
619
624
620
625
test ( 'wrong import syntax should throw error after module mocking.' , async ( ) => {
0 commit comments