From 1a6e2f248c03dc62d9da81a4aa719f0e0b00fbfe Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 16 Aug 2024 23:57:03 +0200 Subject: [PATCH 1/2] test_runner: make `mock.module`'s `specifier` consistent with `import()` The previous implementation was trying to follow both `require` and `import` conventions. It is not practical to try to follow both, and aligning with `import()` seems to be what makes the most sense. --- doc/api/test.md | 2 +- lib/internal/test_runner/mock/mock.js | 8 ++- test/parallel/test-runner-module-mocking.js | 57 +++++++++++---------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/doc/api/test.md b/doc/api/test.md index b1a8e0030bd43f..60eeb51c954f2c 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -2050,7 +2050,7 @@ added: v22.3.0 > Stability: 1.0 - Early development -* `specifier` {string} A string identifying the module to mock. +* `specifier` {string|URL} A string identifying the module to mock. * `options` {Object} Optional configuration options for the mock module. The following properties are supported: * `cache` {boolean} If `false`, each call to `require()` or `import()` diff --git a/lib/internal/test_runner/mock/mock.js b/lib/internal/test_runner/mock/mock.js index 828f3e513f8e5f..6c23cd8dbe6437 100644 --- a/lib/internal/test_runner/mock/mock.js +++ b/lib/internal/test_runner/mock/mock.js @@ -34,7 +34,7 @@ const { } = require('internal/errors'); const esmLoader = require('internal/modules/esm/loader'); const { getOptionValue } = require('internal/options'); -const { fileURLToPath, toPathIfFileURL, URL } = require('internal/url'); +const { fileURLToPath, toPathIfFileURL, URL, isURL } = require('internal/url'); const { emitExperimentalWarning, getStructuredStack, @@ -488,7 +488,11 @@ class MockTracker { module(specifier, options = kEmptyObject) { emitExperimentalWarning('Module mocking'); - validateString(specifier, 'specifier'); + if (typeof specifier !== 'string') { + if (!isURL(specifier)) + throw new ERR_INVALID_ARG_TYPE('specifier', ['string', 'URL'], specifier); + specifier = `${specifier}`; + } validateObject(options, 'options'); debug('module mock entry, specifier = "%s", options = %o', specifier, options); diff --git a/test/parallel/test-runner-module-mocking.js b/test/parallel/test-runner-module-mocking.js index 7e59c0d4dcde8b..9cf76be3c08fde 100644 --- a/test/parallel/test-runner-module-mocking.js +++ b/test/parallel/test-runner-module-mocking.js @@ -154,7 +154,7 @@ test('CJS mocking with namedExports option', async (t) => { assert.strictEqual(original.string, 'original cjs string'); assert.strictEqual(original.fn, undefined); - t.mock.module(fixture, { + t.mock.module(pathToFileURL(fixture), { namedExports: { fn() { return 42; } }, }); const mocked = require(fixture); @@ -174,7 +174,7 @@ test('CJS mocking with namedExports option', async (t) => { assert.strictEqual(original.string, 'original cjs string'); assert.strictEqual(original.fn, undefined); - t.mock.module(fixture, { + t.mock.module(pathToFileURL(fixture), { namedExports: { fn() { return 42; } }, cache: true, }); @@ -195,7 +195,7 @@ test('CJS mocking with namedExports option', async (t) => { assert.strictEqual(original.string, 'original cjs string'); assert.strictEqual(original.fn, undefined); - t.mock.module(fixture, { + t.mock.module(pathToFileURL(fixture), { namedExports: { fn() { return 42; } }, cache: false, }); @@ -219,7 +219,7 @@ test('CJS mocking with namedExports option', async (t) => { const defaultExport = { val1: 5, val2: 3 }; - t.mock.module(fixture, { + t.mock.module(pathToFileURL(fixture), { defaultExport, namedExports: { val1: 'mock value' }, }); @@ -242,7 +242,7 @@ test('CJS mocking with namedExports option', async (t) => { const defaultExport = null; - t.mock.module(fixture, { + t.mock.module(pathToFileURL(fixture), { defaultExport, namedExports: { val1: 'mock value' }, }); @@ -256,7 +256,7 @@ test('CJS mocking with namedExports option', async (t) => { test('ESM mocking with namedExports option', async (t) => { await t.test('does not cache by default', async (t) => { - const fixture = fixtures.path('module-mocking', 'basic-esm.mjs'); + const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs'); const original = await import(fixture); assert.strictEqual(original.string, 'original esm string'); @@ -276,7 +276,7 @@ test('ESM mocking with namedExports option', async (t) => { }); await t.test('explicitly enables caching', async (t) => { - const fixture = fixtures.path('module-mocking', 'basic-esm.mjs'); + const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs'); const original = await import(fixture); assert.strictEqual(original.string, 'original esm string'); @@ -297,7 +297,7 @@ test('ESM mocking with namedExports option', async (t) => { }); await t.test('explicitly disables caching', async (t) => { - const fixture = fixtures.path('module-mocking', 'basic-esm.mjs'); + const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs'); const original = await import(fixture); assert.strictEqual(original.string, 'original esm string'); @@ -318,7 +318,8 @@ test('ESM mocking with namedExports option', async (t) => { }); await t.test('named exports are not applied to defaultExport', async (t) => { - const fixture = fixtures.path('module-mocking', 'basic-esm.mjs'); + const fixturePath = fixtures.path('module-mocking', 'basic-esm.mjs'); + const fixture = pathToFileURL(fixturePath); const original = await import(fixture); assert.strictEqual(original.string, 'original esm string'); @@ -338,11 +339,11 @@ test('ESM mocking with namedExports option', async (t) => { assert.strictEqual(mocked.default, 'mock default'); assert.strictEqual(mocked.val1, 'mock value'); t.mock.reset(); - common.expectRequiredModule(require(fixture), original); + common.expectRequiredModule(require(fixturePath), original); }); await t.test('throws if named exports cannot be applied to defaultExport as CJS', async (t) => { - const fixture = fixtures.path('module-mocking', 'basic-cjs.js'); + const fixture = fixtures.fileURL('module-mocking', 'basic-cjs.js'); const original = await import(fixture); assert.strictEqual(original.default.string, 'original cjs string'); @@ -366,13 +367,14 @@ test('ESM mocking with namedExports option', async (t) => { test('modules cannot be mocked multiple times at once', async (t) => { await t.test('CJS', async (t) => { const fixture = fixtures.path('module-mocking', 'basic-cjs.js'); + const fixtureURL = pathToFileURL(fixture).href; - t.mock.module(fixture, { + t.mock.module(fixtureURL, { namedExports: { fn() { return 42; } }, }); assert.throws(() => { - t.mock.module(fixture, { + t.mock.module(fixtureURL, { namedExports: { fn() { return 55; } }, }); }, { @@ -386,7 +388,7 @@ test('modules cannot be mocked multiple times at once', async (t) => { }); await t.test('ESM', async (t) => { - const fixture = fixtures.path('module-mocking', 'basic-esm.mjs'); + const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs').href; t.mock.module(fixture, { namedExports: { fn() { return 42; } }, @@ -409,10 +411,10 @@ test('modules cannot be mocked multiple times at once', async (t) => { test('mocks are automatically restored', async (t) => { const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js'); - const esmFixture = fixtures.path('module-mocking', 'basic-esm.mjs'); + const esmFixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs'); await t.test('CJS', async (t) => { - t.mock.module(cjsFixture, { + t.mock.module(pathToFileURL(cjsFixture), { namedExports: { fn() { return 42; } }, }); @@ -442,9 +444,9 @@ test('mocks are automatically restored', async (t) => { test('mocks can be restored independently', async (t) => { const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js'); - const esmFixture = fixtures.path('module-mocking', 'basic-esm.mjs'); + const esmFixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs'); - const cjsMock = t.mock.module(cjsFixture, { + const cjsMock = t.mock.module(pathToFileURL(cjsFixture), { namedExports: { fn() { return 42; } }, }); @@ -511,10 +513,11 @@ test('node:- core module mocks can be used by both module systems', async (t) => test('CJS mocks can be used by both module systems', async (t) => { const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js'); - const cjsMock = t.mock.module(cjsFixture, { + const cjsFixtureURL = pathToFileURL(cjsFixture) + const cjsMock = t.mock.module(cjsFixtureURL, { namedExports: { fn() { return 42; } }, }); - let esmImpl = await import(pathToFileURL(cjsFixture)); + let esmImpl = await import(cjsFixtureURL); let cjsImpl = require(cjsFixture); assert.strictEqual(esmImpl.fn(), 42); @@ -522,7 +525,7 @@ test('CJS mocks can be used by both module systems', async (t) => { cjsMock.restore(); - esmImpl = await import(pathToFileURL(cjsFixture)); + esmImpl = await import(cjsFixtureURL); cjsImpl = require(cjsFixture); assert.strictEqual(esmImpl.default.string, 'original cjs string'); @@ -532,7 +535,7 @@ test('CJS mocks can be used by both module systems', async (t) => { test('relative paths can be used by both module systems', async (t) => { const fixture = relative( __dirname, fixtures.path('module-mocking', 'basic-esm.mjs') - ); + ).replaceAll('\\', '/'); const mock = t.mock.module(fixture, { namedExports: { fn() { return 42; } }, }); @@ -597,24 +600,26 @@ test('mocked modules do not impact unmocked modules', async (t) => { test('defaultExports work with CJS mocks in both module systems', async (t) => { const fixture = fixtures.path('module-mocking', 'basic-cjs.js'); + const fixtureURL = pathToFileURL(fixture); const original = require(fixture); const defaultExport = Symbol('default'); assert.strictEqual(original.string, 'original cjs string'); - t.mock.module(fixture, { defaultExport }); + t.mock.module(fixtureURL, { defaultExport }); assert.strictEqual(require(fixture), defaultExport); - assert.strictEqual((await import(pathToFileURL(fixture))).default, defaultExport); + assert.strictEqual((await import(fixtureURL)).default, defaultExport); }); test('defaultExports work with ESM mocks in both module systems', async (t) => { - const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs'); + const fixturePath = fixtures.path('module-mocking', 'basic-esm.mjs'); + const fixture = pathToFileURL(fixturePath); const original = await import(fixture); const defaultExport = Symbol('default'); assert.strictEqual(original.string, 'original esm string'); t.mock.module(`${fixture}`, { defaultExport }); assert.strictEqual((await import(fixture)).default, defaultExport); - assert.strictEqual(require(fileURLToPath(fixture)), defaultExport); + assert.strictEqual(require(fixturePath), defaultExport); }); test('wrong import syntax should throw error after module mocking.', async () => { From 68550574ecf571835367df5ae6e16317286a8963 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 17 Aug 2024 09:07:30 +0200 Subject: [PATCH 2/2] fixup! test_runner: make `mock.module`'s `specifier` consistent with `import()` --- lib/internal/test_runner/mock/mock.js | 1 - test/parallel/test-runner-module-mocking.js | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/internal/test_runner/mock/mock.js b/lib/internal/test_runner/mock/mock.js index 6c23cd8dbe6437..a0de3d2dd41909 100644 --- a/lib/internal/test_runner/mock/mock.js +++ b/lib/internal/test_runner/mock/mock.js @@ -49,7 +49,6 @@ const { validateInteger, validateObject, validateOneOf, - validateString, } = require('internal/validators'); const { MockTimers } = require('internal/test_runner/mock/mock_timers'); const { strictEqual, notStrictEqual } = require('assert'); diff --git a/test/parallel/test-runner-module-mocking.js b/test/parallel/test-runner-module-mocking.js index 9cf76be3c08fde..a9a5c33a7c26b4 100644 --- a/test/parallel/test-runner-module-mocking.js +++ b/test/parallel/test-runner-module-mocking.js @@ -10,7 +10,7 @@ const fixtures = require('../common/fixtures'); const assert = require('node:assert'); const { relative } = require('node:path'); const { test } = require('node:test'); -const { fileURLToPath, pathToFileURL } = require('node:url'); +const { pathToFileURL } = require('node:url'); test('input validation', async (t) => { await t.test('throws if specifier is not a string', (t) => { @@ -513,7 +513,7 @@ test('node:- core module mocks can be used by both module systems', async (t) => test('CJS mocks can be used by both module systems', async (t) => { const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js'); - const cjsFixtureURL = pathToFileURL(cjsFixture) + const cjsFixtureURL = pathToFileURL(cjsFixture); const cjsMock = t.mock.module(cjsFixtureURL, { namedExports: { fn() { return 42; } }, });