Skip to content

Commit aac8ba7

Browse files
authored
test_runner: fix mocking modules with quote in their URL
PR-URL: #55083 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tierney Cyren <hello@bnb.im> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 7d0ce25 commit aac8ba7

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

lib/internal/test_runner/mock/loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async function createSourceFromMock(mock, format) {
139139
const { exportNames, hasDefaultExport, url } = mock;
140140
const useESM = format === 'module' || format === 'module-typescript';
141141
const source = `${testImportSource(useESM)}
142-
if (!$__test.mock._mockExports.has('${url}')) {
142+
if (!$__test.mock._mockExports.has(${JSONStringify(url)})) {
143143
throw new Error(${JSONStringify(`mock exports not found for "${url}"`)});
144144
}
145145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export let string = 'original esm string';

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

+9
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,15 @@ test('modules cannot be mocked multiple times at once', async (t) => {
413413
t.mock.module(fixture, { namedExports: { fn() { return 42; } } });
414414
await assert.rejects(import(fixture), { code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME' });
415415
});
416+
417+
await t.test('Importing a module with a quote in its URL should work', async (t) => {
418+
const fixture = fixtures.fileURL('module-mocking', 'don\'t-open.mjs');
419+
t.mock.module(fixture, { namedExports: { fn() { return 42; } } });
420+
421+
const mocked = await import(fixture);
422+
423+
assert.strictEqual(mocked.fn(), 42);
424+
});
416425
});
417426

418427
test('mocks are automatically restored', async (t) => {

0 commit comments

Comments
 (0)