Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7eaeba4

Browse filesBrowse files
aduh95targos
authored andcommitted
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 fdae57f commit 7eaeba4
Copy full SHA for 7eaeba4

File tree

Expand file treeCollapse file tree

3 files changed

+11
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+11
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/test_runner/mock/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock/loader.js
+1-1Lines changed: 1 addition & 1 deletion
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
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export let string = 'original esm string';
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-runner-module-mocking.js
+9Lines changed: 9 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.