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_runner: fix mocking modules with quote in their URL #55083

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lib/internal/test_runner/mock/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async function createSourceFromMock(mock, format) {
const { exportNames, hasDefaultExport, url } = mock;
const useESM = format === 'module' || format === 'module-typescript';
const source = `${testImportSource(useESM)}
if (!$__test.mock._mockExports.has('${url}')) {
if (!$__test.mock._mockExports.has(${JSONStringify(url)})) {
throw new Error(${JSONStringify(`mock exports not found for "${url}"`)});
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/module-mocking/don't-open.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export let string = 'original esm string';
9 changes: 9 additions & 0 deletions test/parallel/test-runner-module-mocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,15 @@ test('modules cannot be mocked multiple times at once', async (t) => {
t.mock.module(fixture, { namedExports: { fn() { return 42; } } });
await assert.rejects(import(fixture), { code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME' });
});

await t.test('Importing a module with a quote in its URL should work', async (t) => {
const fixture = fixtures.fileURL('module-mocking', 'don\'t-open.mjs');
t.mock.module(fixture, { namedExports: { fn() { return 42; } } });

const mocked = await import(fixture);

assert.strictEqual(mocked.fn(), 42);
});
});

test('mocks are automatically restored', async (t) => {
Expand Down
Loading