Skip to content

Commit

Permalink
test_runner: fix mocking modules with quote in their URL
Browse files Browse the repository at this point in the history
PR-URL: #55083
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tierney Cyren <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
  • Loading branch information
aduh95 authored Sep 25, 2024
1 parent 7d0ce25 commit aac8ba7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
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

0 comments on commit aac8ba7

Please sign in to comment.