mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
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:
parent
7d0ce254e8
commit
aac8ba7bd7
@ -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}"`)});
|
||||
}
|
||||
|
||||
|
1
test/fixtures/module-mocking/don't-open.mjs
vendored
Normal file
1
test/fixtures/module-mocking/don't-open.mjs
vendored
Normal file
@ -0,0 +1 @@
|
||||
export let string = 'original esm string';
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user