Skip to content
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
7 changes: 7 additions & 0 deletions lib/nodejs/esm-utils.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ const requireModule = async (file, esmDecorator) => {
throw requireErr;
}

// A directory specifier resolves under CommonJS but not under ESM so this error
// only means the fallback import() never got far enough to say anything useful about the file.
// Check this issue https://github.com/mochajs/mocha/issues/5792
if (importErr.code === "ERR_UNSUPPORTED_DIR_IMPORT") {
throw requireErr;
}

// Similarly, for an exports/imports mismatch such as a missing 'default',
// the require() error will be more informative for users.
// See: https://github.com/mochajs/mocha/issues/5411
Expand Down
27 changes: 27 additions & 0 deletions test/node-unit/esm-utils.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const url = require("node:url");

describe("esm-utils", function () {
describe("requireOrImport", function () {
afterEach(function () {
sinon.restore();
});

it("should show an informative error message for a broken default import", async function () {
return expect(
() =>
Expand Down Expand Up @@ -62,6 +66,29 @@ describe("esm-utils", function () {
},
);
});

it("should surface the require() error when import() fails on a directory specifier", async function () {
sinon
.stub(esmUtils, "doImport")
.rejects(
Object.assign(
new Error("Directory import '/fixtures/lib' is not supported"),
{ code: "ERR_UNSUPPORTED_DIR_IMPORT" },
),
);

return expect(
() =>
esmUtils.requireOrImport(
"../../test/node-unit/fixtures/this-module-does-not-exist.cjs",
),
"to be rejected with error satisfying",
{
code: "MODULE_NOT_FOUND",
message: /this-module-does-not-exist/,
},
);
});
});

describe("loadFilesAsync()", function () {
Expand Down
Loading