Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

fix: import() fallback prevention (#5647)

Shashwat Raj6a78fa39576f
diff --git a/lib/nodejs/esm-utils.js b/lib/nodejs/esm-utils.js
index 7c5c95b8..5b41473c 100644
--- a/lib/nodejs/esm-utils.js
+++ b/lib/nodejs/esm-utils.js
@@ -1,6 +1,7 @@
const path = require("node:path");
const url = require("node:url");
const debug = require("debug")("mocha:esm-utils");
+const { isMochaError } = require("../errors");
const forward = (x) => x;
@@ -98,7 +99,7 @@ const requireModule = async (file, esmDecorator) => {
return require(file);
} catch (requireErr) {
debug("requireModule caught err: %O", requireErr.message);
- if (requireErr.name === "TSError") {
+ if (requireErr.name === "TSError" || isMochaError(requireErr)) {
throw requireErr;
}
try {
diff --git a/test/node-unit/esm-utils.spec.js b/test/node-unit/esm-utils.spec.js
index c86af8a6..4ed8205e 100644
--- a/test/node-unit/esm-utils.spec.js
+++ b/test/node-unit/esm-utils.spec.js
@@ -48,6 +48,20 @@ describe("esm-utils", function () {
},
);
});
+
+ it("should surface Mocha errors rather than falling back to `import(...)`", async function () {
+ return expect(
+ () =>
+ esmUtils.requireOrImport(
+ "../../test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts",
+ ),
+ "to be rejected with error satisfying",
+ {
+ code: "ERR_MOCHA_FORBIDDEN_EXCLUSIVITY",
+ message: /`\.only` is not supported in parallel mode/,
+ },
+ );
+ });
});
describe("loadFilesAsync()", function () {
diff --git a/test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts b/test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts
new file mode 100644
index 00000000..e174dfb0
--- /dev/null
+++ b/test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts
@@ -0,0 +1,10 @@
+/*
+ Mock file that simulates a Mocha FORBIDDEN_EXCLUSIVITY error
+ This error is thrown when .only is used in parallel mode
+ */
+const { createForbiddenExclusivityError } = require('../../../lib/errors');
+
+// Simulate a mocha instance in worker mode
+const mockMocha = { isWorker: true };
+
+throw createForbiddenExclusivityError(mockMocha);