Skip to content

Commit

Permalink
squash
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed May 16, 2024
1 parent d5bcd75 commit 4983546
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/test/mock_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ const {
kMockExists,
kMockUnknownMessage,
} = require('internal/test_runner/mock/mock');
const { defaultResolve } = require('internal/modules/esm/resolve');
const { URL } = require('internal/url');
const { pathToFileURL, URL } = require('internal/url');
const { normalizeReferrerURL } = require('internal/modules/helpers');
let debug = require('internal/util/debuglog').debuglog('test_runner', (fn) => {
debug = fn;
});
const { isBuiltin } = require('module');
const { createRequire, isBuiltin } = require('module');

// 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
Expand Down Expand Up @@ -96,8 +95,20 @@ async function resolve(specifier, context, nextResolve) {
if (isBuiltin(specifier)) {
mockSpecifier = ensureNodeScheme(specifier);
} else {
const parentURL = normalizeReferrerURL(context.parentURL);
specifier = await defaultResolve(specifier, { parentURL }).url;
// TODO(cjihrig): This try...catch should be replaced by defaultResolve(),
// but there are some edge cases that caused the tests to fail on Windows.
try {
const req = createRequire(context.parentURL);
specifier = pathToFileURL(req.resolve(specifier)).href;
} catch {
const parentURL = normalizeReferrerURL(context.parentURL);
const parsedURL = URL.parse(specifier, parentURL)?.href;

if (parsedURL) {
specifier = parsedURL;
}
}

mockSpecifier = specifier;
}

Expand Down

0 comments on commit 4983546

Please sign in to comment.