Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor test-runner-module-mocking #54233

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 11 additions & 33 deletions test/parallel/test-runner-module-mocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 { pathToFileURL } = require('node:url');
const { fileURLToPath, pathToFileURL } = require('node:url');

test('input validation', async (t) => {
await t.test('throws if specifier is not a string', (t) => {
Expand Down Expand Up @@ -514,41 +514,21 @@ test('CJS mocks can be used by both module systems', async (t) => {
const cjsMock = t.mock.module(cjsFixture, {
namedExports: { fn() { return 42; } },
});
let esmImpl = await import(cjsFixture);
let esmImpl = await import(pathToFileURL(cjsFixture));
let cjsImpl = require(cjsFixture);

assert.strictEqual(esmImpl.fn(), 42);
assert.strictEqual(cjsImpl.fn(), 42);

cjsMock.restore();

esmImpl = await import(cjsFixture);
esmImpl = await import(pathToFileURL(cjsFixture));
cjsImpl = require(cjsFixture);

assert.strictEqual(esmImpl.default.string, 'original cjs string');
assert.strictEqual(cjsImpl.string, 'original cjs string');
});

test('ESM mocks can be used by both module systems', async (t) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this test? (Other changes LGTM)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way to refactor it. Either we use an absolute path for require and a file: URL for import() (but that's already the previous test), either we use the same relative specifier (but that's already the following test); so it seemed to me it was testing a "real" use case, or rather it's testing some particular case where an absolute path coincides with an origin-relative URL – but we cannot guarantee that, it depends on where the node repo was cloned.

const esmFixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const esmMock = t.mock.module(esmFixture, {
namedExports: { fn() { return 42; } },
});

let cjsImpl = require(esmFixture);
let esmImpl = await import(esmFixture);

assert.strictEqual(cjsImpl.fn(), 42);
assert.strictEqual(esmImpl.fn(), 42);

esmMock.restore();
cjsImpl = require(esmFixture);
esmImpl = await import(esmFixture);

assert.strictEqual(esmImpl.string, 'original esm string');
assert.strictEqual(cjsImpl.string, 'original esm string');
});

test('relative paths can be used by both module systems', async (t) => {
const fixture = relative(
__dirname, fixtures.path('module-mocking', 'basic-esm.mjs')
Expand Down Expand Up @@ -586,9 +566,7 @@ test('node_modules can be used by both module systems', async (t) => {
});

test('file:// imports are supported in ESM only', async (t) => {
const fixture = pathToFileURL(
fixtures.path('module-mocking', 'basic-esm.mjs')
).href;
const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs').href;
const mock = t.mock.module(fixture, {
namedExports: { fn() { return 42; } },
});
Expand All @@ -604,9 +582,9 @@ test('file:// imports are supported in ESM only', async (t) => {
});

test('mocked modules do not impact unmocked modules', async (t) => {
const mockedFixture = fixtures.path('module-mocking', 'basic-cjs.js');
const unmockedFixture = fixtures.path('module-mocking', 'basic-esm.mjs');
t.mock.module(mockedFixture, {
const mockedFixture = fixtures.fileURL('module-mocking', 'basic-cjs.js');
const unmockedFixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');
t.mock.module(`${mockedFixture}`, {
namedExports: { fn() { return 42; } },
});
const mockedImpl = await import(mockedFixture);
Expand All @@ -625,18 +603,18 @@ test('defaultExports work with CJS mocks in both module systems', async (t) => {
assert.strictEqual(original.string, 'original cjs string');
t.mock.module(fixture, { defaultExport });
assert.strictEqual(require(fixture), defaultExport);
assert.strictEqual((await import(fixture)).default, defaultExport);
assert.strictEqual((await import(pathToFileURL(fixture))).default, defaultExport);
});

test('defaultExports work with ESM mocks in both module systems', 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);
const defaultExport = Symbol('default');

assert.strictEqual(original.string, 'original esm string');
t.mock.module(fixture, { defaultExport });
t.mock.module(`${fixture}`, { defaultExport });
assert.strictEqual((await import(fixture)).default, defaultExport);
assert.strictEqual(require(fixture), defaultExport);
assert.strictEqual(require(fileURLToPath(fixture)), defaultExport);
});

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