mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
test_runner: make mock_loader not confuse CJS and ESM resolution
PR-URL: https://github.com/nodejs/node/pull/53846 Fixes: https://github.com/nodejs/node/issues/53807 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
@@ -23,6 +23,8 @@ 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): This file should not be exposed publicly, but register() does
|
||||
// not handle internal loaders. Before marking this API as stable, one of the
|
||||
@@ -88,11 +90,20 @@ async function resolve(specifier, context, nextResolve) {
|
||||
if (isBuiltin(specifier)) {
|
||||
mockSpecifier = ensureNodeScheme(specifier);
|
||||
} else {
|
||||
// TODO(cjihrig): This try...catch should be replaced by defaultResolve(),
|
||||
// but there are some edge cases that caused the tests to fail on Windows.
|
||||
let format;
|
||||
|
||||
if (context.parentURL) {
|
||||
format = defaultGetFormatWithoutErrors(pathToFileURL(context.parentURL));
|
||||
}
|
||||
|
||||
try {
|
||||
const req = createRequire(context.parentURL);
|
||||
specifier = pathToFileURL(req.resolve(specifier)).href;
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user