Skip to content

Commit 64babf7

Browse files
committed
Windows
1 parent db0e8e2 commit 64babf7

1 file changed

Lines changed: 52 additions & 40 deletions

File tree

test/parallel/test-runner-module-mocking.js

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ test('CJS mocking with namedExports option', async (t) => {
154154
assert.strictEqual(original.string, 'original cjs string');
155155
assert.strictEqual(original.fn, undefined);
156156

157-
t.mock.module(fixture, {
157+
t.mock.module(`${pathToFileURL(fixture)}`, {
158158
namedExports: { fn() { return 42; } },
159159
});
160160
const mocked = require(fixture);
@@ -174,7 +174,7 @@ test('CJS mocking with namedExports option', async (t) => {
174174
assert.strictEqual(original.string, 'original cjs string');
175175
assert.strictEqual(original.fn, undefined);
176176

177-
t.mock.module(fixture, {
177+
t.mock.module(`${pathToFileURL(fixture)}`, {
178178
namedExports: { fn() { return 42; } },
179179
cache: true,
180180
});
@@ -195,7 +195,7 @@ test('CJS mocking with namedExports option', async (t) => {
195195
assert.strictEqual(original.string, 'original cjs string');
196196
assert.strictEqual(original.fn, undefined);
197197

198-
t.mock.module(fixture, {
198+
t.mock.module(`${pathToFileURL(fixture)}`, {
199199
namedExports: { fn() { return 42; } },
200200
cache: false,
201201
});
@@ -219,7 +219,7 @@ test('CJS mocking with namedExports option', async (t) => {
219219

220220
const defaultExport = { val1: 5, val2: 3 };
221221

222-
t.mock.module(fixture, {
222+
t.mock.module(`${pathToFileURL(fixture)}`, {
223223
defaultExport,
224224
namedExports: { val1: 'mock value' },
225225
});
@@ -242,7 +242,7 @@ test('CJS mocking with namedExports option', async (t) => {
242242

243243
const defaultExport = null;
244244

245-
t.mock.module(fixture, {
245+
t.mock.module(`${pathToFileURL(fixture)}`, {
246246
defaultExport,
247247
namedExports: { val1: 'mock value' },
248248
});
@@ -256,13 +256,13 @@ test('CJS mocking with namedExports option', async (t) => {
256256

257257
test('ESM mocking with namedExports option', async (t) => {
258258
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');
260260
const original = await import(fixture);
261261

262262
assert.strictEqual(original.string, 'original esm string');
263263
assert.strictEqual(original.fn, undefined);
264264

265-
t.mock.module(fixture, {
265+
t.mock.module(`${fixture}`, {
266266
namedExports: { fn() { return 42; } },
267267
});
268268
const mocked = await import(fixture);
@@ -276,13 +276,13 @@ test('ESM mocking with namedExports option', async (t) => {
276276
});
277277

278278
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');
280280
const original = await import(fixture);
281281

282282
assert.strictEqual(original.string, 'original esm string');
283283
assert.strictEqual(original.fn, undefined);
284284

285-
t.mock.module(fixture, {
285+
t.mock.module(`${fixture}`, {
286286
namedExports: { fn() { return 42; } },
287287
cache: true,
288288
});
@@ -297,13 +297,13 @@ test('ESM mocking with namedExports option', async (t) => {
297297
});
298298

299299
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');
301301
const original = await import(fixture);
302302

303303
assert.strictEqual(original.string, 'original esm string');
304304
assert.strictEqual(original.fn, undefined);
305305

306-
t.mock.module(fixture, {
306+
t.mock.module(`${fixture}`, {
307307
namedExports: { fn() { return 42; } },
308308
cache: false,
309309
});
@@ -318,7 +318,8 @@ test('ESM mocking with namedExports option', async (t) => {
318318
});
319319

320320
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);
322323
const original = await import(fixture);
323324

324325
assert.strictEqual(original.string, 'original esm string');
@@ -327,7 +328,7 @@ test('ESM mocking with namedExports option', async (t) => {
327328

328329
const defaultExport = 'mock default';
329330

330-
t.mock.module(fixture, {
331+
t.mock.module(`${fixture}`, {
331332
defaultExport,
332333
namedExports: { val1: 'mock value' },
333334
});
@@ -338,19 +339,19 @@ test('ESM mocking with namedExports option', async (t) => {
338339
assert.strictEqual(mocked.default, 'mock default');
339340
assert.strictEqual(mocked.val1, 'mock value');
340341
t.mock.reset();
341-
common.expectRequiredModule(require(fixture), original);
342+
common.expectRequiredModule(require(fixturePath), original);
342343
});
343344

344345
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');
346347
const original = await import(fixture);
347348

348349
assert.strictEqual(original.default.string, 'original cjs string');
349350
assert.strictEqual(original.default.val1, undefined);
350351

351352
const defaultExport = null;
352353

353-
t.mock.module(fixture, {
354+
t.mock.module(`${fixture}`, {
354355
defaultExport,
355356
namedExports: { val1: 'mock value' },
356357
});
@@ -366,13 +367,14 @@ test('ESM mocking with namedExports option', async (t) => {
366367
test('modules cannot be mocked multiple times at once', async (t) => {
367368
await t.test('CJS', async (t) => {
368369
const fixture = fixtures.path('module-mocking', 'basic-cjs.js');
370+
const fixtureURL = pathToFileURL(fixture).href;
369371

370-
t.mock.module(fixture, {
372+
t.mock.module(fixtureURL, {
371373
namedExports: { fn() { return 42; } },
372374
});
373375

374376
assert.throws(() => {
375-
t.mock.module(fixture, {
377+
t.mock.module(fixtureURL, {
376378
namedExports: { fn() { return 55; } },
377379
});
378380
}, {
@@ -386,7 +388,7 @@ test('modules cannot be mocked multiple times at once', async (t) => {
386388
});
387389

388390
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;
390392

391393
t.mock.module(fixture, {
392394
namedExports: { fn() { return 42; } },
@@ -405,14 +407,20 @@ test('modules cannot be mocked multiple times at once', async (t) => {
405407

406408
assert.strictEqual(mocked.fn(), 42);
407409
});
410+
411+
await t.test('Importing a Windows path should fail', { skip: common.isWindows }, async (t) => {
412+
const fixture = fixtures.path('module-mocking', 'wrong-path.js');
413+
t.mock.module(fixture, { namedExports: { fn() { return 42; } } });
414+
await assert.rejects(import(fixture), { code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME' });
415+
})
408416
});
409417

410418
test('mocks are automatically restored', async (t) => {
411419
const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js');
412-
const esmFixture = fixtures.path('module-mocking', 'basic-esm.mjs');
420+
const esmFixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');
413421

414422
await t.test('CJS', async (t) => {
415-
t.mock.module(cjsFixture, {
423+
t.mock.module(`${pathToFileURL(cjsFixture)}`, {
416424
namedExports: { fn() { return 42; } },
417425
});
418426

@@ -422,7 +430,7 @@ test('mocks are automatically restored', async (t) => {
422430
});
423431

424432
await t.test('ESM', async (t) => {
425-
t.mock.module(esmFixture, {
433+
t.mock.module(`${esmFixture}`, {
426434
namedExports: { fn() { return 43; } },
427435
});
428436

@@ -442,13 +450,13 @@ test('mocks are automatically restored', async (t) => {
442450

443451
test('mocks can be restored independently', async (t) => {
444452
const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js');
445-
const esmFixture = fixtures.path('module-mocking', 'basic-esm.mjs');
453+
const esmFixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');
446454

447-
const cjsMock = t.mock.module(cjsFixture, {
455+
const cjsMock = t.mock.module(`${pathToFileURL(cjsFixture)}`, {
448456
namedExports: { fn() { return 42; } },
449457
});
450458

451-
const esmMock = t.mock.module(esmFixture, {
459+
const esmMock = t.mock.module(`${esmFixture}`, {
452460
namedExports: { fn() { return 43; } },
453461
});
454462

@@ -511,18 +519,19 @@ test('node:- core module mocks can be used by both module systems', async (t) =>
511519

512520
test('CJS mocks can be used by both module systems', async (t) => {
513521
const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js');
514-
const cjsMock = t.mock.module(cjsFixture, {
522+
const cjsFixtureURL = pathToFileURL(cjsFixture)
523+
const cjsMock = t.mock.module(`${cjsFixtureURL}`, {
515524
namedExports: { fn() { return 42; } },
516525
});
517-
let esmImpl = await import(cjsFixture);
526+
let esmImpl = await import(cjsFixtureURL);
518527
let cjsImpl = require(cjsFixture);
519528

520529
assert.strictEqual(esmImpl.fn(), 42);
521530
assert.strictEqual(cjsImpl.fn(), 42);
522531

523532
cjsMock.restore();
524533

525-
esmImpl = await import(cjsFixture);
534+
esmImpl = await import(cjsFixtureURL);
526535
cjsImpl = require(cjsFixture);
527536

528537
assert.strictEqual(esmImpl.default.string, 'original cjs string');
@@ -531,19 +540,20 @@ test('CJS mocks can be used by both module systems', async (t) => {
531540

532541
test('ESM mocks can be used by both module systems', async (t) => {
533542
const esmFixture = fixtures.path('module-mocking', 'basic-esm.mjs');
534-
const esmMock = t.mock.module(esmFixture, {
543+
const esmFixtureURL = pathToFileURL(esmFixture)
544+
const esmMock = t.mock.module(`${esmFixtureURL}`, {
535545
namedExports: { fn() { return 42; } },
536546
});
537547

538548
let cjsImpl = require(esmFixture);
539-
let esmImpl = await import(esmFixture);
549+
let esmImpl = await import(esmFixtureURL);
540550

541551
assert.strictEqual(cjsImpl.fn(), 42);
542552
assert.strictEqual(esmImpl.fn(), 42);
543553

544554
esmMock.restore();
545555
cjsImpl = require(esmFixture);
546-
esmImpl = await import(esmFixture);
556+
esmImpl = await import(esmFixtureURL);
547557

548558
assert.strictEqual(esmImpl.string, 'original esm string');
549559
assert.strictEqual(cjsImpl.string, 'original esm string');
@@ -552,7 +562,7 @@ test('ESM mocks can be used by both module systems', async (t) => {
552562
test('relative paths can be used by both module systems', async (t) => {
553563
const fixture = relative(
554564
__dirname, fixtures.path('module-mocking', 'basic-esm.mjs')
555-
);
565+
).replaceAll('\\', '/');
556566
const mock = t.mock.module(fixture, {
557567
namedExports: { fn() { return 42; } },
558568
});
@@ -604,9 +614,9 @@ test('file:// imports are supported in ESM only', async (t) => {
604614
});
605615

606616
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, {
617+
const mockedFixture = fixtures.fileURL('module-mocking', 'basic-cjs.js');
618+
const unmockedFixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');
619+
t.mock.module(`${mockedFixture}`, {
610620
namedExports: { fn() { return 42; } },
611621
});
612622
const mockedImpl = await import(mockedFixture);
@@ -619,24 +629,26 @@ test('mocked modules do not impact unmocked modules', async (t) => {
619629

620630
test('defaultExports work with CJS mocks in both module systems', async (t) => {
621631
const fixture = fixtures.path('module-mocking', 'basic-cjs.js');
632+
const fixtureURL = pathToFileURL(fixture);
622633
const original = require(fixture);
623634
const defaultExport = Symbol('default');
624635

625636
assert.strictEqual(original.string, 'original cjs string');
626-
t.mock.module(fixture, { defaultExport });
637+
t.mock.module(`${fixtureURL}`, { defaultExport });
627638
assert.strictEqual(require(fixture), defaultExport);
628-
assert.strictEqual((await import(fixture)).default, defaultExport);
639+
assert.strictEqual((await import(fixtureURL)).default, defaultExport);
629640
});
630641

631642
test('defaultExports work with ESM mocks in both module systems', async (t) => {
632-
const fixture = fixtures.path('module-mocking', 'basic-esm.mjs');
643+
const fixturePath = fixtures.path('module-mocking', 'basic-esm.mjs');
644+
const fixture = pathToFileURL(fixturePath);
633645
const original = await import(fixture);
634646
const defaultExport = Symbol('default');
635647

636648
assert.strictEqual(original.string, 'original esm string');
637-
t.mock.module(fixture, { defaultExport });
649+
t.mock.module(`${fixture}`, { defaultExport });
638650
assert.strictEqual((await import(fixture)).default, defaultExport);
639-
assert.strictEqual(require(fixture), defaultExport);
651+
assert.strictEqual(require(fixturePath), defaultExport);
640652
});
641653

642654
test('wrong import syntax should throw error after module mocking.', async () => {

0 commit comments

Comments
 (0)