Skip to content

Commit e446517

Browse files
aduh95targos
authored andcommittedOct 2, 2024
test: refactor test-runner-module-mocking
PR-URL: #54233 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 53f5c54 commit e446517

File tree

1 file changed

+11
-33
lines changed

1 file changed

+11
-33
lines changed
 

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

+11-33
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const fixtures = require('../common/fixtures');
1010
const assert = require('node:assert');
1111
const { relative } = require('node:path');
1212
const { test } = require('node:test');
13-
const { pathToFileURL } = require('node:url');
13+
const { fileURLToPath, pathToFileURL } = require('node:url');
1414

1515
test('input validation', async (t) => {
1616
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) => {
514514
const cjsMock = t.mock.module(cjsFixture, {
515515
namedExports: { fn() { return 42; } },
516516
});
517-
let esmImpl = await import(cjsFixture);
517+
let esmImpl = await import(pathToFileURL(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(cjsFixture);
525+
esmImpl = await import(pathToFileURL(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-
552532
test('relative paths can be used by both module systems', async (t) => {
553533
const fixture = relative(
554534
__dirname, fixtures.path('module-mocking', 'basic-esm.mjs')
@@ -586,9 +566,7 @@ test('node_modules can be used by both module systems', async (t) => {
586566
});
587567

588568
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;
592570
const mock = t.mock.module(fixture, {
593571
namedExports: { fn() { return 42; } },
594572
});
@@ -604,9 +582,9 @@ test('file:// imports are supported in ESM only', async (t) => {
604582
});
605583

606584
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}`, {
610588
namedExports: { fn() { return 42; } },
611589
});
612590
const mockedImpl = await import(mockedFixture);
@@ -625,18 +603,18 @@ test('defaultExports work with CJS mocks in both module systems', async (t) => {
625603
assert.strictEqual(original.string, 'original cjs string');
626604
t.mock.module(fixture, { defaultExport });
627605
assert.strictEqual(require(fixture), defaultExport);
628-
assert.strictEqual((await import(fixture)).default, defaultExport);
606+
assert.strictEqual((await import(pathToFileURL(fixture))).default, defaultExport);
629607
});
630608

631609
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');
633611
const original = await import(fixture);
634612
const defaultExport = Symbol('default');
635613

636614
assert.strictEqual(original.string, 'original esm string');
637-
t.mock.module(fixture, { defaultExport });
615+
t.mock.module(`${fixture}`, { defaultExport });
638616
assert.strictEqual((await import(fixture)).default, defaultExport);
639-
assert.strictEqual(require(fixture), defaultExport);
617+
assert.strictEqual(require(fileURLToPath(fixture)), defaultExport);
640618
});
641619

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

0 commit comments

Comments
 (0)
Please sign in to comment.