test_runner: fix mocking modules with quote in their URL

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Antoine du Hamel 2024-09-25 19:34:58 +02:00 committed by GitHub
parent 7d0ce254e8
commit aac8ba7bd7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 1 deletions

View File

@ -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}"`)});
}

View File

@ -0,0 +1 @@
export let string = 'original esm string';

View File

@ -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) => {