Skip to content

armgit / mocha

Public Imported from GitHub
0

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

fix: use find-up-simple to workaround ERR_PACKAGE_PATH_NOT_EXPORTED (#6342)

Mark Wiemera652ff5c1b14
diff --git a/lib/cli/config.cjs b/lib/cli/config.cjs
index ccac4eca..70b762fc 100644
--- a/lib/cli/config.cjs
+++ b/lib/cli/config.cjs
@@ -10,10 +10,21 @@
const fs = require("node:fs");
const path = require("node:path");
const debug = require("debug")("mocha:cli:config");
-const { findUpSync } = require("find-up");
+const { findUpSync } = require("find-up-simple");
const { createUnparsableFileError } = require("../errors.js");
const utils = require("../utils.cjs");
+const findUpSyncAny = (files, options) => {
+ for (const file of files) {
+ const filepath = findUpSync(file, options);
+ if (filepath) {
+ return filepath;
+ }
+ }
+
+ return undefined;
+};
+
/**
* These are the valid config files, in order of precedence;
* e.g., if `.mocharc.js` is present, then `.mocharc.yaml` and the rest
@@ -96,7 +107,7 @@ exports.loadConfig = (filepath) => {
* @returns {string|null} Filepath to config, if found
*/
exports.findConfig = (cwd = utils.cwd()) => {
- const filepath = findUpSync(exports.CONFIG_FILES, { cwd });
+ const filepath = findUpSyncAny(exports.CONFIG_FILES, { cwd });
if (filepath) {
debug("findConfig: found config file %s", filepath);
}
diff --git a/lib/cli/options.cjs b/lib/cli/options.cjs
index b2f991c3..27c01a58 100644
--- a/lib/cli/options.cjs
+++ b/lib/cli/options.cjs
@@ -13,7 +13,7 @@ const { parseMochaArgs } = require("./parse-args.js");
const { ONE_AND_DONE_ARGS } = require("./one-and-dones.js");
const mocharc = require("../mocharc.json");
const { loadConfig, findConfig } = require("./config.cjs");
-const { findUpSync } = require("find-up");
+const { findUpSync } = require("find-up-simple");
const debugModule = require("debug");
const { createUnparsableFileError, isMochaError } = require("../errors.js");
diff --git a/package-lock.json b/package-lock.json
index 8c2f6a0d..7ab71132 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -13,7 +13,7 @@
"chokidar": "^5.0.0",
"debug": "^4.3.5",
"diff": "^9.0.0",
- "find-up": "^8.0.0",
+ "find-up-simple": "^1.0.1",
"glob": "^13.0.0",
"is-path-inside": "^4.0.0",
"is-unicode-supported": "^0.1.0",
@@ -907,9 +907,6 @@
"x64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1185,9 +1182,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1205,9 +1199,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1225,9 +1216,6 @@
"ppc64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1245,9 +1233,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1265,9 +1250,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1285,9 +1267,6 @@
"s390x"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1305,9 +1284,6 @@
"x64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1325,9 +1301,6 @@
"x64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1521,9 +1494,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1538,9 +1508,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1555,9 +1522,6 @@
"ppc64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1572,9 +1536,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1589,9 +1550,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1606,9 +1564,6 @@
"s390x"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1623,9 +1578,6 @@
"x64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1640,9 +1592,6 @@
"x64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2013,9 +1962,6 @@
"arm"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2030,9 +1976,6 @@
"arm"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2047,9 +1990,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2064,9 +2004,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2081,9 +2018,6 @@
"loong64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2098,9 +2032,6 @@
"loong64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2115,9 +2046,6 @@
"ppc64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2132,9 +2060,6 @@
"ppc64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2149,9 +2074,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2166,9 +2088,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2183,9 +2102,6 @@
"s390x"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2200,9 +2116,6 @@
"x64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2217,9 +2130,6 @@
"x64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -4339,71 +4249,10 @@
"url": "https://github.com/avajs/find-cache-dir?sponsor=1"
}
},
- "node_modules/find-up": {
- "version": "8.0.0",
- "resolved": "https://registry.npmjs.org/find-up/-/find-up-8.0.0.tgz",
- "integrity": "sha512-JGG8pvDi2C+JxidYdIwQDyS/CgcrIdh18cvgxcBge3wSHRQOrooMD3GlFBcmMJAN9M42SAZjDp5zv1dglJjwww==",
- "license": "MIT",
- "dependencies": {
- "locate-path": "^8.0.0",
- "unicorn-magic": "^0.3.0"
- },
- "engines": {
- "node": ">=20"
- },
- "funding": {
- "url": "https://github.com/sponsors/sindresorhus"
- }
- },
- "node_modules/find-up/node_modules/locate-path": {
- "version": "8.0.0",
- "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-8.0.0.tgz",
- "integrity": "sha512-XT9ewWAC43tiAV7xDAPflMkG0qOPn2QjHqlgX8FOqmWa/rxnyYDulF9T0F7tRy1u+TVTmK/M//6VIOye+2zDXg==",
- "license": "MIT",
- "dependencies": {
- "p-locate": "^6.0.0"
- },
- "engines": {
- "node": ">=20"
- },
- "funding": {
- "url": "https://github.com/sponsors/sindresorhus"
- }
- },
- "node_modules/find-up/node_modules/p-limit": {
- "version": "4.0.0",
- "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-4.0.0.tgz",
- "integrity": "sha512-5b0R4txpzjPWVw/cXXUResoD4hb6U/x9BH08L7nw+GN1sezDzPdxeRvpc9c433fZhBan/wusjbCsqwqm4EIBIQ==",
- "license": "MIT",
- "dependencies": {
- "yocto-queue": "^1.0.0"
- },
- "engines": {
- "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
- },
- "funding": {
- "url": "https://github.com/sponsors/sindresorhus"
- }
- },
- "node_modules/find-up/node_modules/p-locate": {
- "version": "6.0.0",
- "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-6.0.0.tgz",
- "integrity": "sha512-wPrq66Llhl7/4AGC6I+cqxT07LhXvWL08LNXz1fENOw0Ap4sRZZ/gZpTTJ5jpurzzzfS2W/Ge9BY3LgLjCShcw==",
- "license": "MIT",
- "dependencies": {
- "p-limit": "^4.0.0"
- },
- "engines": {
- "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
- },
- "funding": {
- "url": "https://github.com/sponsors/sindresorhus"
- }
- },
- "node_modules/find-up/node_modules/unicorn-magic": {
- "version": "0.3.0",
- "resolved": "https://registry.npmjs.org/unicorn-magic/-/unicorn-magic-0.3.0.tgz",
- "integrity": "sha512-+QBBXBCvifc56fsbuxZQ6Sic3wqqc3WWaqxs58gvJrcOuN83HGTCwz3oS5phzU9LthRNE9VrJCFCLUgHeeFnfA==",
+ "node_modules/find-up-simple": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/find-up-simple/-/find-up-simple-1.0.1.tgz",
+ "integrity": "sha512-afd4O7zpqHeRyg4PfDQsXmlDe2PfdHtJt6Akt8jOWaApLOZk5JXs6VMR29lz03pRe9mpykrRCYIYxaJYcfpncQ==",
"license": "MIT",
"engines": {
"node": ">=18"
@@ -4412,18 +4261,6 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
- "node_modules/find-up/node_modules/yocto-queue": {
- "version": "1.2.2",
- "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-1.2.2.tgz",
- "integrity": "sha512-4LCcse/U2MHZ63HAJVE+v71o7yOdIe4cZ70Wpf8D/IyjDKYQLV5GD46B+hSTjJsvV5PztjvHoU580EftxjDZFQ==",
- "license": "MIT",
- "engines": {
- "node": ">=12.20"
- },
- "funding": {
- "url": "https://github.com/sponsors/sindresorhus"
- }
- },
"node_modules/flat": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz",
diff --git a/package.json b/package.json
index 7dad76f4..901fb247 100644
--- a/package.json
+++ b/package.json
@@ -95,7 +95,7 @@
"chokidar": "^5.0.0",
"debug": "^4.3.5",
"diff": "^9.0.0",
- "find-up": "^8.0.0",
+ "find-up-simple": "^1.0.1",
"glob": "^13.0.0",
"is-path-inside": "^4.0.0",
"is-unicode-supported": "^0.1.0",
diff --git a/test/integration/fixtures/options/node-flags/import-break-find-up.mjs b/test/integration/fixtures/options/node-flags/import-break-find-up.mjs
new file mode 100644
index 00000000..add5a042
--- /dev/null
+++ b/test/integration/fixtures/options/node-flags/import-break-find-up.mjs
@@ -0,0 +1,16 @@
+import { createRequire } from "node:module";
+
+const require = createRequire(import.meta.url);
+const Module = require("node:module");
+const originalResolveFilename = Module._resolveFilename;
+
+Module._resolveFilename = function (request, parent, isMain, options) {
+ if (request === "find-up" || request.startsWith("find-up/")) {
+ const err = new Error(
+ 'No "exports" main defined in node_modules/unicorn-magic/package.json',
+ );
+ err.code = "ERR_PACKAGE_PATH_NOT_EXPORTED";
+ throw err;
+ }
+ return originalResolveFilename.call(this, request, parent, isMain, options);
+};
diff --git a/test/integration/options/node-flags.spec.cjs b/test/integration/options/node-flags.spec.cjs
index 1224ed73..0e592028 100644
--- a/test/integration/options/node-flags.spec.cjs
+++ b/test/integration/options/node-flags.spec.cjs
@@ -1,6 +1,9 @@
"use strict";
+var path = require("node:path");
+var pathToFileURL = require("node:url").pathToFileURL;
var invokeMocha = require("../helpers.cjs").invokeMocha;
+var runMochaAsync = require("../helpers.cjs").runMochaAsync;
describe("node flags", function () {
it("should not consider argument values to be node flags", function (done) {
@@ -32,4 +35,19 @@ describe('node flags using "--node-option"', function () {
"pipe",
);
});
+
+ it("should work with import hooks that break find-up resolution", async function () {
+ this.timeout(5000);
+ const importHookPath = path.resolve(
+ __dirname,
+ "../fixtures/options/node-flags/import-break-find-up.mjs",
+ );
+ const importHookSpecifier = pathToFileURL(importHookPath).href;
+ const result = await runMochaAsync("passing.fixture.cjs", [
+ "--node-option",
+ `import=${importHookSpecifier}`,
+ ]);
+
+ expect(result, "to have passed test count", 2);
+ });
});
diff --git a/test/node-unit/cli/config.spec.cjs b/test/node-unit/cli/config.spec.cjs
index c56c4a0b..9801b82a 100644
--- a/test/node-unit/cli/config.spec.cjs
+++ b/test/node-unit/cli/config.spec.cjs
@@ -142,11 +142,11 @@ describe("cli/config", function () {
let CONFIG_FILES;
beforeEach(function () {
- findup = sinon.stub().returns("/some/path/.mocharc.js");
+ findup = sinon.stub();
const config = rewiremock.proxy(
require.resolve("../../../lib/cli/config.cjs"),
(r) => ({
- "find-up": r.by(() => ({ findUpSync: findup })),
+ "find-up-simple": r.by(() => ({ findUpSync: findup })),
}),
);
findConfig = config.findConfig;
@@ -154,20 +154,56 @@ describe("cli/config", function () {
});
it("should look for one of the config files using findup-sync", function () {
+ findup.onFirstCall().returns("/some/path/.mocharc.js");
findConfig();
expect(findup, "to have a call satisfying", {
- args: [CONFIG_FILES, { cwd: process.cwd() }],
+ args: [CONFIG_FILES[0], { cwd: process.cwd() }],
returned: "/some/path/.mocharc.js",
});
});
it("should support an explicit `cwd`", function () {
+ findup.onFirstCall().returns("/some/path/.mocharc.js");
findConfig("/some/path/");
expect(findup, "to have a call satisfying", {
- args: [CONFIG_FILES, { cwd: "/some/path/" }],
+ args: [CONFIG_FILES[0], { cwd: "/some/path/" }],
returned: "/some/path/.mocharc.js",
});
});
+
+ it("should call findup-sync with all config filename args in order", function () {
+ findup.returns(undefined);
+ findConfig("/some/path/");
+ expect(
+ findup,
+ "to have calls satisfying",
+ CONFIG_FILES.map((file) => ({ args: [file, { cwd: "/some/path/" }] })),
+ );
+ });
+
+ it("should not make extra calls once an item is found", function () {
+ const expected = "/some/path/.mocharc.mjs";
+ findup
+ .onFirstCall()
+ .returns(undefined)
+ .onSecondCall()
+ .returns(undefined)
+ .onThirdCall()
+ .returns(expected);
+
+ expect(findConfig("/some/path/"), "to equal", expected);
+ expect(findup, "was called times", 3);
+ expect(findup.getCalls(), "to satisfy", [
+ { args: [CONFIG_FILES[0], { cwd: "/some/path/" }] },
+ { args: [CONFIG_FILES[1], { cwd: "/some/path/" }] },
+ { args: [CONFIG_FILES[2], { cwd: "/some/path/" }] },
+ ]);
+ });
+
+ it("should return undefined if no item is found", function () {
+ findup.returns(undefined);
+ expect(findConfig("/some/path/"), "to be undefined");
+ });
});
describe("parsers()", function () {
diff --git a/test/node-unit/cli/options.spec.js b/test/node-unit/cli/options.spec.js
index db0abf56..82ddaad7 100644
--- a/test/node-unit/cli/options.spec.js
+++ b/test/node-unit/cli/options.spec.js
@@ -20,7 +20,7 @@ const proxyLoadOptions = ({
proxy(modulePath, (r) => ({
"node:fs": r.with({ readFileSync }).directChildOnly(),
[mocharcPath]: defaults,
- "find-up": r
+ "find-up-simple": r
.by(() => (findupSync ? { findUpSync: findupSync } : {}))
.directChildOnly(),
[configPath]: r.with({ findConfig, loadConfig }).directChildOnly(),