Skip to content

armgit / mocha

Public Imported from GitHub
0

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

chore: migrate core interfaces and utilities to ESM (#5930)

Mostafa Nazari1ad23ab544ac
diff --git a/docs/src/content/docs/explainers/third-party-uis.mdx b/docs/src/content/docs/explainers/third-party-uis.mdx
index c89431f5..cda10aa5 100644
--- a/docs/src/content/docs/explainers/third-party-uis.mdx
+++ b/docs/src/content/docs/explainers/third-party-uis.mdx
@@ -11,7 +11,7 @@ In this first brief example, we'll create an interface with only a single functi
```javascript
import Mocha from "mocha";
-import MochaInterface from "mocha/lib/interfaces/common.js";
+import { createCommon as MochaInterface } from "mocha/lib/interfaces/common.mjs";
const Test = Mocha.Test;
/**
@@ -74,7 +74,7 @@ In this next example, we'll be extending the [TDD interface](https://github.com/
```javascript
import Mocha from "mocha";
-import MochaInterface from "mocha/lib/interfaces/common.js";
+import { createCommon as MochaInterface } from "mocha/lib/interfaces/common.mjs";
import escapeStringRegexp from "escape-string-regexp";
const Test = Mocha.Test;
diff --git a/lib/cli/config.js b/lib/cli/config.js
index bc163ddc..678e06c7 100644
--- a/lib/cli/config.js
+++ b/lib/cli/config.js
@@ -11,7 +11,7 @@ const fs = require("node:fs");
const path = require("node:path");
const debug = require("debug")("mocha:cli:config");
const findUp = require("find-up");
-const { createUnparsableFileError } = require("../errors");
+const { createUnparsableFileError } = require("../errors.mjs");
const utils = require("../utils");
/**
diff --git a/lib/cli/lookup-files.mjs b/lib/cli/lookup-files.mjs
index ce542af0..940b71c2 100644
--- a/lib/cli/lookup-files.mjs
+++ b/lib/cli/lookup-files.mjs
@@ -10,7 +10,7 @@ import * as glob from "glob";
import {
createNoFilesMatchPatternError,
createMissingArgumentError,
-} from "../errors.js";
+} from "../errors.mjs";
import debugModule from "debug";
const debug = debugModule("mocha:cli:lookup-files");
diff --git a/lib/cli/one-and-dones.js b/lib/cli/one-and-dones.mjs
similarity index 87%
rename from lib/cli/one-and-dones.js
rename to lib/cli/one-and-dones.mjs
index dc3a738f..22ee9578 100644
--- a/lib/cli/one-and-dones.js
+++ b/lib/cli/one-and-dones.mjs
@@ -1,5 +1,3 @@
-"use strict";
-
/**
* Contains "command" code for "one-and-dones"--options passed
* to Mocha which cause it to just dump some info and exit.
@@ -8,7 +6,7 @@
* @private
*/
-const Mocha = require("../mocha");
+import Mocha from "../mocha.js";
/**
* Dumps a sorted list of the enumerable, lower-case keys of some object
@@ -43,7 +41,7 @@ const showKeys = (obj) => {
* @namespace
* @private
*/
-exports.ONE_AND_DONES = {
+const ONE_AND_DONES = {
/**
* Dump list of built-in interfaces
* @private
@@ -65,6 +63,8 @@ exports.ONE_AND_DONES = {
* @type Set<string>
* @private
*/
-exports.ONE_AND_DONE_ARGS = new Set(
- ["help", "h", "version", "V"].concat(Object.keys(exports.ONE_AND_DONES)),
+const ONE_AND_DONE_ARGS = new Set(
+ ["help", "h", "version", "V"].concat(Object.keys(ONE_AND_DONES)),
);
+
+export { ONE_AND_DONES, ONE_AND_DONE_ARGS };
diff --git a/lib/cli/options.mjs b/lib/cli/options.mjs
index b182b54e..144297db 100644
--- a/lib/cli/options.mjs
+++ b/lib/cli/options.mjs
@@ -14,7 +14,7 @@ import {
isMochaFlag,
expectedTypeForFlag,
} from "./run-option-metadata.mjs";
-import { ONE_AND_DONE_ARGS } from "./one-and-dones.js";
+import { ONE_AND_DONE_ARGS } from "./one-and-dones.mjs";
import mocharc from "../mocharc.json" with { type: "json" };
import { list } from "./run-helpers.js";
import { loadConfig, findConfig } from "./config.js";
@@ -26,7 +26,7 @@ import {
createUnparsableFileError,
createInvalidArgumentTypeError,
createUnsupportedError,
-} from "../errors.js";
+} from "../errors.mjs";
import { isNumeric } from "../utils.js";
/**
diff --git a/lib/cli/run-helpers.js b/lib/cli/run-helpers.js
index d83b76f1..d0f16b44 100644
--- a/lib/cli/run-helpers.js
+++ b/lib/cli/run-helpers.js
@@ -21,9 +21,9 @@ const debug = require("debug")("mocha:cli:run:helpers");
const { watchRun, watchParallelRun } = require("./watch-run");
const collectFiles = require("./collect-files");
const { format } = require("node:util");
-const { createInvalidLegacyPluginError } = require("../errors");
+const { createInvalidLegacyPluginError } = require("../errors.mjs");
const { requireOrImport } = require("../nodejs/esm-utils");
-const PluginLoader = require("../plugin-loader");
+const { PluginLoader } = require("../plugin-loader.mjs");
/**
* Exits Mocha when tests + code under test has finished execution (default)
diff --git a/lib/cli/run.js b/lib/cli/run.js
index c1846186..bdca79b4 100644
--- a/lib/cli/run.js
+++ b/lib/cli/run.js
@@ -13,7 +13,7 @@ const {
createUnsupportedError,
createInvalidArgumentValueError,
createMissingArgumentError,
-} = require("../errors");
+} = require("../errors.mjs");
const {
list,
@@ -21,7 +21,7 @@ const {
validateLegacyPlugin,
runMocha,
} = require("./run-helpers");
-const { ONE_AND_DONES, ONE_AND_DONE_ARGS } = require("./one-and-dones");
+const { ONE_AND_DONES, ONE_AND_DONE_ARGS } = require("./one-and-dones.mjs");
const debug = require("debug")("mocha:cli:run");
const defaults = require("../mocharc.json");
const { types, aliases } = require("./run-option-metadata.mjs");
diff --git a/lib/context.mjs b/lib/context.mjs
index a5f0b76a..885a065a 100644
--- a/lib/context.mjs
+++ b/lib/context.mjs
@@ -1,5 +1,5 @@
/**
- * @typedef {import('./runnable.js')} Runnable
+ * @typedef {import('./runnable.mjs')} Runnable
*/
/**
diff --git a/lib/errors.js b/lib/errors.mjs
similarity index 98%
rename from lib/errors.js
rename to lib/errors.mjs
index 97509f49..3f3ae056 100644
--- a/lib/errors.js
+++ b/lib/errors.mjs
@@ -1,15 +1,13 @@
-"use strict";
-
/**
* @typedef {import('./mocha.js')} Mocha
- * @typedef {import('./runnable.js')} Runnable
+ * @typedef {import('./runnable.mjs')} Runnable
* @typedef {import('./types.d.ts').MochaTimeoutError} MochaTimeoutError
* @typedef {import('./types.d.ts').PluginDefinition} PluginDefinition
*/
-const { format } = require("node:util");
-const { constants } = require("./error-constants.mjs");
-const { isCI } = require("./utils");
+import { format } from "node:util";
+import { constants } from "./error-constants.mjs";
+import { isCI } from "./utils.js";
/**
* Contains error codes and factory functions to create throwable error objects
@@ -359,7 +357,7 @@ function createUnparsableFileError(message) {
const isMochaError = (err) =>
Boolean(err && typeof err === "object" && MOCHA_ERRORS.has(err.code));
-module.exports = {
+export {
createFatalError,
createForbiddenExclusivityError,
createInvalidArgumentTypeError,
diff --git a/lib/hook.mjs b/lib/hook.mjs
index 15175cb8..776a1b99 100644
--- a/lib/hook.mjs
+++ b/lib/hook.mjs
@@ -1,4 +1,4 @@
-import Runnable from "./runnable.js";
+import { Runnable } from "./runnable.mjs";
import utils from "./utils.js";
const { MOCHA_ID_PROP_NAME } = utils.constants;
diff --git a/lib/interfaces/bdd.js b/lib/interfaces/bdd.mjs
similarity index 84%
rename from lib/interfaces/bdd.js
rename to lib/interfaces/bdd.mjs
index 231fa907..c0a31a0a 100644
--- a/lib/interfaces/bdd.js
+++ b/lib/interfaces/bdd.mjs
@@ -1,12 +1,8 @@
-"use strict";
+import { Test } from "../test.mjs";
+import { Suite } from "../suite.mjs";
+import { createCommon } from "./common.mjs";
-/**
- * @typedef {import('../suite.js')} Suite
- */
-
-var Test = require("../test");
-var EVENT_FILE_PRE_REQUIRE =
- require("../suite").constants.EVENT_FILE_PRE_REQUIRE;
+const EVENT_FILE_PRE_REQUIRE = Suite.constants.EVENT_FILE_PRE_REQUIRE;
/**
* BDD-style interface:
@@ -25,11 +21,11 @@ var EVENT_FILE_PRE_REQUIRE =
*
* @param {Suite} suite Root suite.
*/
-module.exports = function bddInterface(suite) {
+function bddInterface(suite) {
var suites = [suite];
suite.on(EVENT_FILE_PRE_REQUIRE, function (context, file, mocha) {
- var common = require("./common")(suites, context, mocha);
+ var common = createCommon(suites, context, mocha);
context.before = common.before;
context.after = common.after;
@@ -113,6 +109,9 @@ module.exports = function bddInterface(suite) {
return context.it(title);
};
});
-};
+}
+
+// Required for `--list-interfaces`
+bddInterface.description = "BDD or RSpec style [default]";
-module.exports.description = "BDD or RSpec style [default]";
+export { bddInterface };
diff --git a/lib/interfaces/common.js b/lib/interfaces/common.mjs
similarity index 90%
rename from lib/interfaces/common.js
rename to lib/interfaces/common.mjs
index 3e656e94..3453791a 100644
--- a/lib/interfaces/common.js
+++ b/lib/interfaces/common.mjs
@@ -1,19 +1,9 @@
-"use strict";
-
-/**
- * @typedef {import('../context.mjs')} Context
- * @typedef {import('../mocha.js')} Mocha
- */
-
-/**
- @module interfaces/common
-*/
-
-var Suite = require("../suite");
-var errors = require("../errors");
-var createMissingArgumentError = errors.createMissingArgumentError;
-var createUnsupportedError = errors.createUnsupportedError;
-var createForbiddenExclusivityError = errors.createForbiddenExclusivityError;
+import { Suite } from "../suite.mjs";
+import {
+ createMissingArgumentError,
+ createUnsupportedError,
+ createForbiddenExclusivityError,
+} from "../errors.mjs";
/**
* Functions common to more than one interface.
@@ -24,7 +14,7 @@ var createForbiddenExclusivityError = errors.createForbiddenExclusivityError;
* @param {Mocha} mocha
* @return {Object} An object containing common functions.
*/
-module.exports = function (suites, context, mocha) {
+function createCommon(suites, context, mocha) {
/**
* Check if the suite should be tested.
*
@@ -195,4 +185,6 @@ module.exports = function (suites, context, mocha) {
},
},
};
-};
+}
+
+export { createCommon };
diff --git a/lib/interfaces/exports.mjs b/lib/interfaces/exports.mjs
index bdb58001..8021d634 100644
--- a/lib/interfaces/exports.mjs
+++ b/lib/interfaces/exports.mjs
@@ -1,5 +1,5 @@
-import Suite from "../suite.js";
-import Test from "../test.js";
+import { Suite } from "../suite.mjs";
+import { Test } from "../test.mjs";
/**
* Exports-style (as Node.js module) interface:
@@ -56,6 +56,7 @@ function exportsInterface(suite) {
}
}
+// Required for `--list-interfaces`
exportsInterface.description = 'Node.js module ("exports") style';
export { exportsInterface };
diff --git a/lib/interfaces/index.js b/lib/interfaces/index.js
deleted file mode 100644
index c3d245cb..00000000
--- a/lib/interfaces/index.js
+++ /dev/null
@@ -1,7 +0,0 @@
-"use strict";
-
-exports.bdd = require("./bdd");
-exports.tdd = require("./tdd");
-exports.qunit = require("./qunit");
-const { exportsInterface } = require("./exports.mjs");
-exports.exports = exportsInterface;
diff --git a/lib/interfaces/index.mjs b/lib/interfaces/index.mjs
new file mode 100644
index 00000000..91dfbde4
--- /dev/null
+++ b/lib/interfaces/index.mjs
@@ -0,0 +1,11 @@
+import { bddInterface } from "./bdd.mjs";
+import { tddInterface } from "./tdd.mjs";
+import { qUnitInterface } from "./qunit.mjs";
+import { exportsInterface } from "./exports.mjs";
+
+export {
+ bddInterface as bdd,
+ tddInterface as tdd,
+ qUnitInterface as qunit,
+ exportsInterface as exports,
+};
diff --git a/lib/interfaces/qunit.js b/lib/interfaces/qunit.mjs
similarity index 82%
rename from lib/interfaces/qunit.js
rename to lib/interfaces/qunit.mjs
index c47d48d9..95573732 100644
--- a/lib/interfaces/qunit.js
+++ b/lib/interfaces/qunit.mjs
@@ -1,11 +1,8 @@
-"use strict";
-/**
- * @typedef {import('../suite.js')} Suite
- */
+import { Test } from "../test.mjs";
+import { Suite } from "../suite.mjs";
+import { createCommon } from "./common.mjs";
-var Test = require("../test");
-var EVENT_FILE_PRE_REQUIRE =
- require("../suite").constants.EVENT_FILE_PRE_REQUIRE;
+const EVENT_FILE_PRE_REQUIRE = Suite.constants.EVENT_FILE_PRE_REQUIRE;
/**
* QUnit-style interface:
@@ -32,11 +29,11 @@ var EVENT_FILE_PRE_REQUIRE =
*
* @param {Suite} suite Root suite.
*/
-module.exports = function qUnitInterface(suite) {
+function qUnitInterface(suite) {
var suites = [suite];
suite.on(EVENT_FILE_PRE_REQUIRE, function (context, file, mocha) {
- var common = require("./common")(suites, context, mocha);
+ var common = createCommon(suites, context, mocha);
context.before = common.before;
context.after = common.after;
@@ -96,6 +93,9 @@ module.exports = function qUnitInterface(suite) {
context.test.skip = common.test.skip;
});
-};
+}
+
+// Required for `--list-interfaces`
+qUnitInterface.description = "QUnit style";
-module.exports.description = "QUnit style";
+export { qUnitInterface };
diff --git a/lib/interfaces/tdd.js b/lib/interfaces/tdd.mjs
similarity index 85%
rename from lib/interfaces/tdd.js
rename to lib/interfaces/tdd.mjs
index 6b669814..88657111 100644
--- a/lib/interfaces/tdd.js
+++ b/lib/interfaces/tdd.mjs
@@ -1,12 +1,8 @@
-"use strict";
+import { Test } from "../test.mjs";
+import { Suite } from "../suite.mjs";
+import { createCommon } from "./common.mjs";
-/**
- * @typedef {import('../suite.js')} Suite
- */
-
-var Test = require("../test");
-var EVENT_FILE_PRE_REQUIRE =
- require("../suite").constants.EVENT_FILE_PRE_REQUIRE;
+const EVENT_FILE_PRE_REQUIRE = Suite.constants.EVENT_FILE_PRE_REQUIRE;
/**
* TDD-style interface:
@@ -33,11 +29,11 @@ var EVENT_FILE_PRE_REQUIRE =
*
* @param {Suite} suite Root suite.
*/
-module.exports = function (suite) {
+function tddInterface(suite) {
var suites = [suite];
suite.on(EVENT_FILE_PRE_REQUIRE, function (context, file, mocha) {
- var common = require("./common")(suites, context, mocha);
+ var common = createCommon(suites, context, mocha);
context.setup = common.beforeEach;
context.teardown = common.afterEach;
@@ -104,7 +100,10 @@ module.exports = function (suite) {
context.test.skip = common.test.skip;
});
-};
+}
-module.exports.description =
+// Required for `--list-interfaces`
+tddInterface.description =
'traditional "suite"/"test" instead of BDD\'s "describe"/"it"';
+
+export { tddInterface };
diff --git a/lib/mocha.js b/lib/mocha.js
index a3bb26b0..3917eb0a 100644
--- a/lib/mocha.js
+++ b/lib/mocha.js
@@ -11,7 +11,7 @@ var path = require("node:path");
var builtinReporters = require("./reporters");
var utils = require("./utils");
var mocharc = require("./mocharc.json");
-var Suite = require("./suite");
+var { Suite } = require("./suite.mjs");
var esmUtils = require("./nodejs/esm-utils");
var createStatsCollector =
require("./stats-collector.mjs").createStatsCollector;
@@ -21,7 +21,7 @@ const {
createMochaInstanceAlreadyDisposedError,
createMochaInstanceAlreadyRunningError,
createUnsupportedError,
-} = require("./errors");
+} = require("./errors.mjs");
const { EVENT_FILE_PRE_REQUIRE, EVENT_FILE_POST_REQUIRE, EVENT_FILE_REQUIRE } =
Suite.constants;
var debug = require("debug")("mocha:mocha");
@@ -80,13 +80,15 @@ if (!utils.isBrowser() && typeof module.paths !== "undefined") {
*/
exports.utils = utils;
-exports.interfaces = require("./interfaces");
+// Must be extensible, see `should add the interface` test
+// in `run-helpers.spec.js`
+exports.interfaces = { ...require("./interfaces/index.mjs") };
/**
* @public
* @memberof Mocha
*/
exports.reporters = builtinReporters;
-exports.Runnable = require("./runnable");
+exports.Runnable = require("./runnable.mjs").Runnable;
exports.Context = require("./context.mjs").Context;
/**
*
@@ -95,7 +97,7 @@ exports.Context = require("./context.mjs").Context;
exports.Runner = require("./runner");
exports.Suite = Suite;
exports.Hook = require("./hook.mjs").Hook;
-exports.Test = require("./test");
+exports.Test = require("./test.mjs").Test;
let currentContext;
exports.afterEach = function (...args) {
diff --git a/lib/nodejs/buffered-worker-pool.js b/lib/nodejs/buffered-worker-pool.js
index c8b3f3a3..4523250a 100644
--- a/lib/nodejs/buffered-worker-pool.js
+++ b/lib/nodejs/buffered-worker-pool.js
@@ -17,7 +17,7 @@ const serializeJavascript = require("serialize-javascript");
const workerpool = require("workerpool");
const { deserialize } = require("./serializer");
const debug = require("debug")("mocha:parallel:buffered-worker-pool");
-const { createInvalidArgumentTypeError } = require("../errors");
+const { createInvalidArgumentTypeError } = require("../errors.mjs");
const WORKER_PATH = require.resolve("./worker.js");
diff --git a/lib/nodejs/esm-utils.js b/lib/nodejs/esm-utils.js
index fa5b4888..16a47767 100644
--- a/lib/nodejs/esm-utils.js
+++ b/lib/nodejs/esm-utils.js
@@ -1,7 +1,7 @@
const path = require("node:path");
const url = require("node:url");
const debug = require("debug")("mocha:esm-utils");
-const { isMochaError } = require("../errors");
+const { isMochaError } = require("../errors.mjs");
const forward = (x) => x;
diff --git a/lib/nodejs/parallel-buffered-runner.js b/lib/nodejs/parallel-buffered-runner.js
index 94b36c56..9d5e1b72 100644
--- a/lib/nodejs/parallel-buffered-runner.js
+++ b/lib/nodejs/parallel-buffered-runner.js
@@ -20,7 +20,7 @@ const { BufferedWorkerPool } = require("./buffered-worker-pool");
const { setInterval, clearInterval } = global;
const { createMap, constants } = require("../utils");
const { MOCHA_ID_PROP_NAME } = constants;
-const { createFatalError } = require("../errors");
+const { createFatalError } = require("../errors.mjs");
const DEFAULT_WORKER_REPORTER =
require.resolve("./reporters/parallel-buffered");
diff --git a/lib/nodejs/serializer.js b/lib/nodejs/serializer.js
index 8e6253ff..29e4fff6 100644
--- a/lib/nodejs/serializer.js
+++ b/lib/nodejs/serializer.js
@@ -12,7 +12,7 @@
*/
const { type, breakCircularDeps } = require("../utils");
-const { createInvalidArgumentTypeError } = require("../errors");
+const { createInvalidArgumentTypeError } = require("../errors.mjs");
// this is not named `mocha:parallel:serializer` because it's noisy and it's
// helpful to be able to write `DEBUG=mocha:parallel*` and get everything else.
const debug = require("debug")("mocha:serializer");
diff --git a/lib/nodejs/worker.js b/lib/nodejs/worker.js
index 0f22b6cf..05c8887b 100644
--- a/lib/nodejs/worker.js
+++ b/lib/nodejs/worker.js
@@ -14,7 +14,7 @@
const {
createInvalidArgumentTypeError,
createInvalidArgumentValueError,
-} = require("../errors");
+} = require("../errors.mjs");
const workerpool = require("workerpool");
const Mocha = require("../mocha");
const { handleRequires, validateLegacyPlugin } = require("../cli/run-helpers");
diff --git a/lib/plugin-loader.js b/lib/plugin-loader.mjs
similarity index 97%
rename from lib/plugin-loader.js
rename to lib/plugin-loader.mjs
index 4469621a..0bf903c0 100644
--- a/lib/plugin-loader.js
+++ b/lib/plugin-loader.mjs
@@ -13,14 +13,13 @@
* @module plugin
*/
-"use strict";
-
-const debug = require("debug")("mocha:plugin-loader");
-const {
+import debugModule from "debug";
+import {
createInvalidPluginDefinitionError,
createInvalidPluginImplementationError,
-} = require("./errors");
-const { castArray } = require("./utils");
+} from "./errors.mjs";
+import { castArray } from "./utils.js";
+const debug = debugModule("mocha:plugin-loader");
/**
* @typedef {import('./types.d.ts').PluginLoaderOptions} PluginLoaderOptions
@@ -284,4 +283,4 @@ class PluginLoader {
}
}
-module.exports = PluginLoader;
+export { PluginLoader };
diff --git a/lib/reporters/base.js b/lib/reporters/base.js
index 81b427ac..296f5871 100644
--- a/lib/reporters/base.js
+++ b/lib/reporters/base.js
@@ -2,7 +2,7 @@
/**
* @typedef {import('../runner.js')} Runner
- * @typedef {import('../test.js')} Test
+ * @typedef {import('../test.mjs')} Test
* @typedef {import('../types.d.ts').FullErrorStack} FullErrorStack
*/
diff --git a/lib/reporters/json-stream.mjs b/lib/reporters/json-stream.mjs
index ac273f90..b82f2295 100644
--- a/lib/reporters/json-stream.mjs
+++ b/lib/reporters/json-stream.mjs
@@ -1,6 +1,6 @@
/**
* @typedef {import('../runner.js')} Runner
- * @typedef {import('../test.js')} Test
+ * @typedef {import('../test.mjs')} Test
*/
/**
diff --git a/lib/reporters/json.mjs b/lib/reporters/json.mjs
index abffdcc8..a8058066 100644
--- a/lib/reporters/json.mjs
+++ b/lib/reporters/json.mjs
@@ -12,7 +12,7 @@
import Base from "./base.js";
import fs from "node:fs";
import path from "node:path";
-import errors from "../errors.js";
+import * as errors from "../errors.mjs";
import utils from "../utils.js";
import Runner from "../runner.js";
const createUnsupportedError = errors.createUnsupportedError;
diff --git a/lib/reporters/landing.mjs b/lib/reporters/landing.mjs
index 1dd9761d..118f5f5d 100644
--- a/lib/reporters/landing.mjs
+++ b/lib/reporters/landing.mjs
@@ -11,7 +11,7 @@
import Base from "./base.js";
import Runner from "../runner.js";
-import Runnable from "../runnable.js";
+import { Runnable } from "../runnable.mjs";
var constants = Runner.constants;
var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
var EVENT_RUN_END = constants.EVENT_RUN_END;
diff --git a/lib/reporters/spec.mjs b/lib/reporters/spec.mjs
index f8212a9a..619be194 100644
--- a/lib/reporters/spec.mjs
+++ b/lib/reporters/spec.mjs
@@ -1,6 +1,6 @@
/**
* @typedef {import('../runner.js')} Runner
- * @typedef {import('../test.js')} Test
+ * @typedef {import('../test.mjs')} Test
*/
/**
diff --git a/lib/reporters/tap.mjs b/lib/reporters/tap.mjs
index b2eae61e..180f56ee 100644
--- a/lib/reporters/tap.mjs
+++ b/lib/reporters/tap.mjs
@@ -1,6 +1,6 @@
/**
* @typedef {import('../runner.js')} Runner
- * @typedef {import('../test.js')} Test
+ * @typedef {import('../test.mjs')} Test
*/
/**
diff --git a/lib/reporters/xunit.mjs b/lib/reporters/xunit.mjs
index 96024b93..d0847567 100644
--- a/lib/reporters/xunit.mjs
+++ b/lib/reporters/xunit.mjs
@@ -1,6 +1,6 @@
/**
* @typedef {import('../runner.js')} Runner
- * @typedef {import('../test.js')} Test
+ * @typedef {import('../test.mjs')} Test
*/
/**
@@ -14,9 +14,9 @@ import Base from "./base.js";
import utils from "../utils.js";
import fs from "node:fs";
import path from "node:path";
-import errors from "../errors.js";
+import * as errors from "../errors.mjs";
import Runner from "../runner.js";
-import Runnable from "../runnable.js";
+import { Runnable } from "../runnable.mjs";
var createUnsupportedError = errors.createUnsupportedError;
var constants = Runner.constants;
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
diff --git a/lib/runnable.js b/lib/runnable.mjs
similarity index 97%
rename from lib/runnable.js
rename to lib/runnable.mjs
index f250c7e5..0d83917f 100644
--- a/lib/runnable.js
+++ b/lib/runnable.mjs
@@ -1,15 +1,15 @@
-"use strict";
-
-var EventEmitter = require("node:events").EventEmitter;
-var { PendingError } = require("./pending.mjs");
-var debug = require("debug")("mocha:runnable");
-var milliseconds = require("ms");
-var utils = require("./utils");
-const {
+import { EventEmitter } from "node:events";
+import { PendingError } from "./pending.mjs";
+import debugModule from "debug";
+import milliseconds from "ms";
+import utils from "./utils.js";
+import {
createInvalidExceptionError,
createMultipleDoneError,
createTimeoutError,
-} = require("./errors");
+} from "./errors.mjs";
+
+const debug = debugModule("mocha:runnable");
/**
* Save timer references to avoid Sinon interfering (see GH-237).
@@ -472,4 +472,4 @@ class Runnable extends EventEmitter {
}
}
-module.exports = Runnable;
+export { Runnable };
diff --git a/lib/runner.js b/lib/runner.js
index 6c938f90..dfa5f373 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -12,8 +12,8 @@ var EventEmitter = require("node:events").EventEmitter;
var { PendingError } = require("./pending.mjs");
var utils = require("./utils");
var debug = require("debug")("mocha:runner");
-var Runnable = require("./runnable");
-var Suite = require("./suite");
+var { Runnable } = require("./runnable.mjs");
+var { Suite } = require("./suite.mjs");
var HOOK_TYPE_BEFORE_EACH = Suite.constants.HOOK_TYPE_BEFORE_EACH;
var HOOK_TYPE_AFTER_EACH = Suite.constants.HOOK_TYPE_AFTER_EACH;
var HOOK_TYPE_AFTER_ALL = Suite.constants.HOOK_TYPE_AFTER_ALL;
@@ -30,7 +30,7 @@ const {
createUnsupportedError,
createFatalError,
isMochaError,
-} = require("./errors");
+} = require("./errors.mjs");
const { constants: errorConstants } = require("./error-constants.mjs");
/**
diff --git a/lib/suite.js b/lib/suite.mjs
similarity index 97%
rename from lib/suite.js
rename to lib/suite.mjs
index ffe63592..50b5e494 100644
--- a/lib/suite.js
+++ b/lib/suite.mjs
@@ -1,26 +1,27 @@
-"use strict";
-
/**
- * @typedef {import('./test.js')} Test
+ * @typedef {import('./test.mjs')} Test
*/
/**
* Module dependencies.
* @private
*/
-const { EventEmitter } = require("node:events");
-const Hook = require("./hook.mjs").Hook;
-var {
+import { EventEmitter } from "node:events";
+import { Hook } from "./hook.mjs";
+import utils from "./utils.js";
+import debugModule from "debug";
+import milliseconds from "ms";
+import * as errors from "./errors.mjs";
+
+const {
assignNewMochaID,
clamp,
constants: utilsConstants,
defineConstants,
getMochaID,
isString,
-} = require("./utils");
-const debug = require("debug")("mocha:suite");
-const milliseconds = require("ms");
-const errors = require("./errors");
+} = utils;
+const debug = debugModule("mocha:suite");
const { MOCHA_ID_PROP_NAME } = utilsConstants;
@@ -666,4 +667,4 @@ class Suite extends EventEmitter {
}
}
-exports = module.exports = Suite;
+export { Suite };
diff --git a/lib/test.js b/lib/test.mjs
similarity index 90%
rename from lib/test.js
rename to lib/test.mjs
index 499b52b0..21520027 100644
--- a/lib/test.js
+++ b/lib/test.mjs
@@ -1,10 +1,8 @@
-"use strict";
-var Runnable = require("./runnable");
-var utils = require("./utils");
-var errors = require("./errors");
-var createInvalidArgumentTypeError = errors.createInvalidArgumentTypeError;
-var isString = utils.isString;
+import { Runnable } from "./runnable.mjs";
+import utils from "./utils.js";
+import { createInvalidArgumentTypeError } from "./errors.mjs";
+const { isString } = utils;
const { MOCHA_ID_PROP_NAME } = utils.constants;
class Test extends Runnable {
@@ -106,4 +104,4 @@ class Test extends Runnable {
}
}
-module.exports = Test;
+export { Test };
diff --git a/test/integration/fixtures/simple-ui.fixture.js b/test/integration/fixtures/simple-ui.fixture.js
index eb4f787f..4e8ba379 100644
--- a/test/integration/fixtures/simple-ui.fixture.js
+++ b/test/integration/fixtures/simple-ui.fixture.js
@@ -13,7 +13,7 @@ module.exports = Mocha.interfaces['simple-ui'] = function(suite) {
file,
mocha
) {
- var common = require('../../../lib/interfaces/common')(
+ var common = require('../../../lib/interfaces/common.mjs').createCommon(
[suite],
context
);
diff --git a/test/integration/fixtures/simple-ui.fixture.mjs b/test/integration/fixtures/simple-ui.fixture.mjs
index 0fa82096..6c392195 100644
--- a/test/integration/fixtures/simple-ui.fixture.mjs
+++ b/test/integration/fixtures/simple-ui.fixture.mjs
@@ -1,5 +1,5 @@
import Mocha from '../../../lib/mocha.js'
-import MochaInterface from '../../../lib/interfaces/common.js';
+import { createCommon as MochaInterface } from '../../../lib/interfaces/common.mjs';
const Test = Mocha.Test;
const EVENT_FILE_PRE_REQUIRE = Mocha.Suite.constants.EVENT_FILE_PRE_REQUIRE;
diff --git a/test/integration/options/listInterfaces.spec.js b/test/integration/options/listInterfaces.spec.js
index 3502b22a..72608d7b 100644
--- a/test/integration/options/listInterfaces.spec.js
+++ b/test/integration/options/listInterfaces.spec.js
@@ -14,6 +14,7 @@ describe("--list-interfaces", function () {
.map(function (name) {
return {
name: escapeRegExp(name),
+ // the description prop is required
description: escapeRegExp(interfaces[name].description),
};
});
diff --git a/test/node-unit/cli/options.spec.mjs b/test/node-unit/cli/options.spec.mjs
index 87697de3..138f9485 100644
--- a/test/node-unit/cli/options.spec.mjs
+++ b/test/node-unit/cli/options.spec.mjs
@@ -1,6 +1,6 @@
import sinon from "sinon";
import { proxy } from "rewiremock/node";
-import { ONE_AND_DONE_ARGS } from "../../../lib/cli/one-and-dones";
+import { ONE_AND_DONE_ARGS } from "../../../lib/cli/one-and-dones.mjs";
import { constants } from "../../../lib/error-constants.mjs";
const modulePath = require.resolve("../../../lib/cli/options.mjs");
diff --git a/test/node-unit/cli/run-helpers.spec.js b/test/node-unit/cli/run-helpers.spec.js
index 34268c4e..eabb0b92 100644
--- a/test/node-unit/cli/run-helpers.spec.js
+++ b/test/node-unit/cli/run-helpers.spec.js
@@ -56,6 +56,8 @@ describe("helpers", function () {
});
describe("when used with a third-party interface", function () {
+ // this test name is referenced in a comment in `lib/mocha.js`
+ // don't change here without also changing there
it('should add the interface to "Mocha.interfaces"', function () {
// let's suppose that `glob` is an interface
const opts = { ui: "glob" };
diff --git a/test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts b/test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts
index e174dfb0..f1eed820 100644
--- a/test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts
+++ b/test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts
@@ -2,7 +2,7 @@
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');
+const { createForbiddenExclusivityError } = require('../../../lib/errors.mjs');
// Simulate a mocha instance in worker mode
const mockMocha = { isWorker: true };
diff --git a/test/node-unit/mocha.spec.js b/test/node-unit/mocha.spec.js
index 076dffee..fb12b5a7 100644
--- a/test/node-unit/mocha.spec.js
+++ b/test/node-unit/mocha.spec.js
@@ -65,12 +65,12 @@ describe("Mocha", function () {
() => require("../../lib/mocha"),
(r) => ({
"../../lib/utils.js": r.with(stubs.utils).callThrough(),
- "../../lib/suite.js": stubs.Suite,
+ "../../lib/suite.mjs": { Suite: stubs.Suite },
"../../lib/nodejs/parallel-buffered-runner.js":
stubs.ParallelBufferedRunner,
"../../lib/nodejs/esm-utils": stubs.esmUtils,
"../../lib/runner.js": stubs.Runner,
- "../../lib/errors.js": stubs.errors,
+ "../../lib/errors.mjs": stubs.errors,
}),
);
delete require.cache[DUMB_FIXTURE_PATH];
diff --git a/test/node-unit/parallel-buffered-runner.spec.js b/test/node-unit/parallel-buffered-runner.spec.js
index cb5f389d..9813c5a1 100644
--- a/test/node-unit/parallel-buffered-runner.spec.js
+++ b/test/node-unit/parallel-buffered-runner.spec.js
@@ -8,7 +8,7 @@ const {
EVENT_SUITE_BEGIN,
} = require("../../lib/runner").constants;
const rewiremock = require("rewiremock/node");
-const Suite = require("../../lib/suite");
+const { Suite } = require("../../lib/suite.mjs");
const Runner = require("../../lib/runner");
const sinon = require("sinon");
const { constants } = require("../../lib/utils");
diff --git a/test/reporters/helpers.js b/test/reporters/helpers.js
index 6477c858..4ae0df8b 100644
--- a/test/reporters/helpers.js
+++ b/test/reporters/helpers.js
@@ -1,7 +1,7 @@
"use strict";
var sinon = require("sinon");
-var errors = require("../../lib/errors");
+var errors = require("../../lib/errors.mjs");
var createStatsCollector =
require("../../lib/stats-collector.mjs").createStatsCollector;
diff --git a/test/unit/errors.spec.js b/test/unit/errors.spec.js
index 2b258aa8..cabfb987 100644
--- a/test/unit/errors.spec.js
+++ b/test/unit/errors.spec.js
@@ -1,9 +1,9 @@
"use strict";
-var errors = require("../../lib/errors");
+var errors = require("../../lib/errors.mjs");
var constants = require("../../lib/error-constants.mjs");
const sinon = require("sinon");
-const { createNoFilesMatchPatternError } = require("../../lib/errors");
+const { createNoFilesMatchPatternError } = require("../../lib/errors.mjs");
describe("Errors", function () {
afterEach(function () {
diff --git a/test/unit/plugin-loader.spec.js b/test/unit/plugin-loader.spec.js
index 625dea0a..034b4a94 100644
--- a/test/unit/plugin-loader.spec.js
+++ b/test/unit/plugin-loader.spec.js
@@ -1,6 +1,6 @@
"use strict";
-const PluginLoader = require("../../lib/plugin-loader");
+const { PluginLoader } = require("../../lib/plugin-loader.mjs");
const sinon = require("sinon");
const { INVALID_PLUGIN_DEFINITION, INVALID_PLUGIN_IMPLEMENTATION } =
require("../../lib/error-constants.mjs").constants;