Skip to content

Commit

Permalink
test_runner: refactor mock_loader
Browse files Browse the repository at this point in the history
PR-URL: #54223
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
aduh95 authored and RafaelGSS committed Aug 30, 2024
1 parent a5ce241 commit 0cf78aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 42 deletions.
48 changes: 8 additions & 40 deletions lib/internal/test_runner/mock/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@ const {
},
} = primordials;
const {
ensureNodeScheme,
kBadExportsMessage,
kMockSearchParam,
kMockSuccess,
kMockExists,
kMockUnknownMessage,
} = require('internal/test_runner/mock/mock');
const { pathToFileURL, URL } = require('internal/url');
const { normalizeReferrerURL } = require('internal/modules/helpers');
const { URL, URLParse } = require('internal/url');
let debug = require('internal/util/debuglog').debuglog('test_runner', (fn) => {
debug = fn;
});
const { createRequire, isBuiltin } = require('module');
const { defaultGetFormatWithoutErrors } = require('internal/modules/esm/get_format');
const { defaultResolve } = require('internal/modules/esm/resolve');

// TODO(cjihrig): The mocks need to be thread aware because the exports are
// evaluated on the thread that creates the mock. Before marking this API as
Expand Down Expand Up @@ -79,46 +74,18 @@ async function initialize(data) {

async function resolve(specifier, context, nextResolve) {
debug('resolve hook entry, specifier = "%s", context = %o', specifier, context);
let mockSpecifier;

if (isBuiltin(specifier)) {
mockSpecifier = ensureNodeScheme(specifier);
} else {
let format;

if (context.parentURL) {
format = defaultGetFormatWithoutErrors(pathToFileURL(context.parentURL));
}

try {
if (format === 'module') {
specifier = defaultResolve(specifier, context).url;
} else {
specifier = pathToFileURL(
createRequire(context.parentURL).resolve(specifier),
).href;
}
} catch {
const parentURL = normalizeReferrerURL(context.parentURL);
const parsedURL = URL.parse(specifier, parentURL)?.href;

if (parsedURL) {
specifier = parsedURL;
}
}

mockSpecifier = specifier;
}
const nextResolveResult = await nextResolve(specifier, context);
const mockSpecifier = nextResolveResult.url;

const mock = mocks.get(mockSpecifier);
debug('resolve hook, specifier = "%s", mock = %o', specifier, mock);

if (mock?.active !== true) {
return nextResolve(specifier, context);
return nextResolveResult;
}

const url = new URL(mockSpecifier);

url.searchParams.set(kMockSearchParam, mock.localVersion);

if (!mock.cache) {
Expand All @@ -127,13 +94,14 @@ async function resolve(specifier, context, nextResolve) {
mock.localVersion++;
}

debug('resolve hook finished, url = "%s"', url.href);
return nextResolve(url.href, context);
const { href } = url;
debug('resolve hook finished, url = "%s"', href);
return { __proto__: null, url: href, format: nextResolveResult.format };
}

async function load(url, context, nextLoad) {
debug('load hook entry, url = "%s", context = %o', url, context);
const parsedURL = URL.parse(url);
const parsedURL = URLParse(url);
if (parsedURL) {
parsedURL.searchParams.delete(kMockSearchParam);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/internal/test_runner/mock/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ const {
} = require('internal/errors');
const esmLoader = require('internal/modules/esm/loader');
const { getOptionValue } = require('internal/options');
const { fileURLToPath, toPathIfFileURL, URL, isURL } = require('internal/url');
const {
fileURLToPath,
isURL,
pathToFileURL,
toPathIfFileURL,
URL,
} = require('internal/url');
const {
emitExperimentalWarning,
getStructuredStack,
Expand Down Expand Up @@ -511,7 +517,7 @@ class MockTracker {

// Get the file that called this function. We need four stack frames:
// vm context -> getStructuredStack() -> this function -> actual caller.
const caller = getStructuredStack()[3]?.getFileName();
const caller = pathToFileURL(getStructuredStack()[3]?.getFileName()).href;
const { format, url } = sharedState.moduleLoader.resolveSync(
mockSpecifier, caller, null,
);
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-runner-module-mocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ test('modules cannot be mocked multiple times at once', async (t) => {

assert.strictEqual(mocked.fn(), 42);
});

await t.test('Importing a Windows path should fail', { skip: !common.isWindows }, async (t) => {
const fixture = fixtures.path('module-mocking', 'wrong-path.js');
t.mock.module(fixture, { namedExports: { fn() { return 42; } } });
await assert.rejects(import(fixture), { code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME' });
});
});

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

0 comments on commit 0cf78aa

Please sign in to comment.