Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: refactor mock_loader #54223

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading