Skip to content

armgit / mocha

Public Imported from GitHub
0

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

chore: convert node-flags and options to ESM, fix ESLint issue (#5900)

Mark Wiemercd2e46160196
diff --git a/bin/mocha.js b/bin/mocha.js
index 01a97232..388f0681 100755
--- a/bin/mocha.js
+++ b/bin/mocha.js
@@ -11,12 +11,12 @@
*/
const os = require("node:os");
-const { loadOptions } = require("../lib/cli/options");
+const { loadOptions } = require("../lib/cli/options.mjs");
const {
unparseNodeFlags,
isNodeFlag,
impliesNoTimeouts,
-} = require("../lib/cli/node-flags");
+} = require("../lib/cli/node-flags.mjs");
const unparse = require("yargs-unparser");
const debug = require("debug")("mocha:cli:mocha");
const { aliases } = require("../lib/cli/run-option-metadata");
diff --git a/eslint.config.js b/eslint.config.js
index e2cb442d..4ce377d3 100644
--- a/eslint.config.js
+++ b/eslint.config.js
@@ -56,6 +56,7 @@ module.exports = defineConfig(
],
languageOptions: {
sourceType: "module",
+ ecmaVersion: 2025,
},
},
{
@@ -163,8 +164,6 @@ module.exports = defineConfig(
"mocha.js",
"out/**",
"test/integration/fixtures/**",
- // TODO: ESLint's parser can't parse import attributes
- "rollup.config.mjs",
"scripts/pick-from-package-json.mjs",
]),
);
diff --git a/lib/cli/cli.js b/lib/cli/cli.js
index bc53d59f..dae1f9ef 100755
--- a/lib/cli/cli.js
+++ b/lib/cli/cli.js
@@ -16,7 +16,7 @@ const {
loadPkgRc,
loadOptions,
YARGS_PARSER_CONFIG,
-} = require("./options");
+} = require("./options.mjs");
const { lookupFiles } = require("./lookup-files.mjs");
const commands = require("./commands.mjs");
const pc = require("picocolors");
diff --git a/lib/cli/node-flags.js b/lib/cli/node-flags.mjs
similarity index 90%
rename from lib/cli/node-flags.js
rename to lib/cli/node-flags.mjs
index 8b8ffe02..89059c00 100644
--- a/lib/cli/node-flags.js
+++ b/lib/cli/node-flags.mjs
@@ -1,5 +1,3 @@
-"use strict";
-
/**
* Some settings and code related to Mocha's handling of Node.js/V8 flags.
* @private
@@ -7,8 +5,8 @@
*/
const nodeFlags = process.allowedNodeEnvironmentFlags;
-const { isMochaFlag } = require("./run-option-metadata");
-const unparse = require("yargs-unparser");
+import { isMochaFlag } from "./run-option-metadata.js";
+import unparse from "yargs-unparser";
/**
* These flags are considered "debug" flags.
@@ -34,7 +32,7 @@ const debugFlags = new Set(["inspect", "inspect-brk"]);
* @returns {boolean} If the flag is considered a "Node" flag.
* @private
*/
-exports.isNodeFlag = (flag, bareword = true) => {
+export function isNodeFlag(flag, bareword = true) {
if (!bareword) {
// check if the flag begins with dashes; if not, not a node flag.
if (!/^--?/.test(flag)) {
@@ -53,7 +51,7 @@ exports.isNodeFlag = (flag, bareword = true) => {
flag,
)
);
-};
+}
/**
* Returns `true` if the flag is a "debug-like" flag. These require timeouts
@@ -62,7 +60,9 @@ exports.isNodeFlag = (flag, bareword = true) => {
* @returns {boolean}
* @private
*/
-exports.impliesNoTimeouts = (flag) => debugFlags.has(flag);
+export function impliesNoTimeouts(flag) {
+ return debugFlags.has(flag);
+}
/**
* All non-strictly-boolean arguments to node--those with values--must specify those values using `=`, e.g., `--inspect=0.0.0.0`.
@@ -72,7 +72,7 @@ exports.impliesNoTimeouts = (flag) => debugFlags.has(flag);
* @returns {string[]} Unparsed arguments using `=` to specify values
* @private
*/
-exports.unparseNodeFlags = (opts) => {
+export function unparseNodeFlags(opts) {
var args = unparse(opts);
return args.length
? args
@@ -82,4 +82,4 @@ exports.unparseNodeFlags = (opts) => {
.join("")
.split(" ")
: [];
-};
+}
diff --git a/lib/cli/options.js b/lib/cli/options.mjs
similarity index 88%
rename from lib/cli/options.js
rename to lib/cli/options.mjs
index 007f8943..08207853 100644
--- a/lib/cli/options.js
+++ b/lib/cli/options.mjs
@@ -1,5 +1,3 @@
-"use strict";
-
/**
* Main entry point for handling filesystem-based configuration,
* whether that's a config file or `package.json` or whatever.
@@ -7,28 +5,29 @@
* @private
*/
-const fs = require("node:fs");
-const pc = require("picocolors");
-const yargsParser = require("yargs-parser");
-const {
+import { readFileSync } from "node:fs";
+import pc from "picocolors";
+import yargsParser from "yargs-parser";
+import {
types,
aliases,
isMochaFlag,
expectedTypeForFlag,
-} = require("./run-option-metadata");
-const { ONE_AND_DONE_ARGS } = require("./one-and-dones");
-const mocharc = require("../mocharc.json");
-const { list } = require("./run-helpers");
-const { loadConfig, findConfig } = require("./config");
-const findUp = require("find-up");
-const debug = require("debug")("mocha:cli:options");
-const { isNodeFlag } = require("./node-flags");
-const {
+} from "./run-option-metadata.js";
+import { ONE_AND_DONE_ARGS } from "./one-and-dones.js";
+import mocharc from "../mocharc.json" with { type: "json" };
+import { list } from "./run-helpers.js";
+import { loadConfig, findConfig } from "./config.js";
+import { sync } from "find-up";
+import debugModule from "debug";
+const debug = debugModule("mocha:cli:options");
+import { isNodeFlag } from "./node-flags.mjs";
+import {
createUnparsableFileError,
createInvalidArgumentTypeError,
createUnsupportedError,
-} = require("../errors");
-const { isNumeric } = require("../utils");
+} from "../errors.js";
+import { isNumeric } from "../utils.js";
/**
* The `yargs-parser` namespace
@@ -46,7 +45,7 @@ const { isNumeric } = require("../utils");
* Base yargs parser configuration
* @private
*/
-const YARGS_PARSER_CONFIG = {
+export const YARGS_PARSER_CONFIG = {
"combine-arrays": true,
"short-option-groups": false,
"dot-notation": false,
@@ -210,34 +209,33 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
* @alias module:lib/cli.loadRc
* @returns {external:yargsParser.Arguments|void} Parsed config, or nothing if `args.config` is `false`
*/
-const loadRc = (args = {}) => {
+export const loadRc = (args = {}) => {
if (args.config !== false) {
const config = args.config || findConfig();
return config ? loadConfig(config) : {};
}
};
-module.exports.loadRc = loadRc;
-
/**
* Given path to `package.json` in `args.package`, attempt to load config from `mocha` prop.
+ * If `args.package` is falsy, attempts to load from default package location (`./package.json`)
* @param {Object} [args] - Arguments object
- * @param {string|boolean} [args.config] - Path to `package.json` or `false` to skip
+ * @param {string|boolean} [args.config] - Path to `package.json` or `false` to use default
* @public
* @alias module:lib/cli.loadPkgRc
- * @returns {external:yargsParser.Arguments|void} Parsed config, or nothing if `args.package` is `false`
+ * @returns {external:yargsParser.Arguments|void} Parsed config. Throws if unparsableF. Empty object if file not found.
*/
-const loadPkgRc = (args = {}) => {
+export const loadPkgRc = (args = {}) => {
let result;
if (args.package === false) {
return result;
}
result = {};
- const filepath = args.package || findUp.sync(mocharc.package);
+ const filepath = args.package || sync(mocharc.package);
if (filepath) {
let configData;
try {
- configData = fs.readFileSync(filepath, "utf8");
+ configData = readFileSync(filepath, "utf8");
} catch (err) {
// If `args.package` was explicitly specified, throw an error
if (filepath == args.package) {
@@ -269,8 +267,6 @@ const loadPkgRc = (args = {}) => {
return result;
};
-module.exports.loadPkgRc = loadPkgRc;
-
/**
* Priority list:
*
@@ -287,7 +283,7 @@ module.exports.loadPkgRc = loadPkgRc;
* @alias module:lib/cli.loadOptions
* @returns {external:yargsParser.Arguments} Parsed args from everything
*/
-const loadOptions = (argv = []) => {
+export const loadOptions = (argv = []) => {
let args = parse(argv);
// short-circuit: look for a flag that would abort loading of options
if (
@@ -332,6 +328,3 @@ const loadOptions = (argv = []) => {
return args;
};
-
-module.exports.loadOptions = loadOptions;
-module.exports.YARGS_PARSER_CONFIG = YARGS_PARSER_CONFIG;
diff --git a/test/node-unit/cli/node-flags.spec.js b/test/node-unit/cli/node-flags.spec.js
index 12582b23..ecc3adbb 100644
--- a/test/node-unit/cli/node-flags.spec.js
+++ b/test/node-unit/cli/node-flags.spec.js
@@ -5,7 +5,7 @@ const {
isNodeFlag,
impliesNoTimeouts,
unparseNodeFlags,
-} = require("../../../lib/cli/node-flags");
+} = require("../../../lib/cli/node-flags.mjs");
const { isMochaFlag } = require("../../../lib/cli/run-option-metadata");
diff --git a/test/node-unit/cli/options.spec.js b/test/node-unit/cli/options.spec.mjs
similarity index 98%
rename from test/node-unit/cli/options.spec.js
rename to test/node-unit/cli/options.spec.mjs
index 39aba8f1..87697de3 100644
--- a/test/node-unit/cli/options.spec.js
+++ b/test/node-unit/cli/options.spec.mjs
@@ -1,11 +1,9 @@
-"use strict";
+import sinon from "sinon";
+import { proxy } from "rewiremock/node";
+import { ONE_AND_DONE_ARGS } from "../../../lib/cli/one-and-dones";
+import { constants } from "../../../lib/error-constants.mjs";
-const sinon = require("sinon");
-const rewiremock = require("rewiremock/node");
-const { ONE_AND_DONE_ARGS } = require("../../../lib/cli/one-and-dones");
-const { constants } = require("../../../lib/error-constants.mjs");
-
-const modulePath = require.resolve("../../../lib/cli/options");
+const modulePath = require.resolve("../../../lib/cli/options.mjs");
const mocharcPath = require.resolve("../../../lib/mocharc.json");
const configPath = require.resolve("../../../lib/cli/config");
@@ -15,7 +13,7 @@ const proxyLoadOptions = ({
findConfig = {},
loadConfig = {},
} = {}) =>
- rewiremock.proxy(modulePath, (r) => ({
+ proxy(modulePath, (r) => ({
"node:fs": r.with({ readFileSync }).directChildOnly(),
[mocharcPath]: defaults,
"find-up": r