Skip to content

Commit 8d78b65

Browse files
committed
chore: convert CLI run command to ESM
Refs #5967.
1 parent 6695fba commit 8d78b65

4 files changed

Lines changed: 36 additions & 25 deletions

File tree

.github/workflows/mocha.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
os: "ubuntu-latest,windows-latest"
7777
# We pin exact versions here per https://github.com/mochajs/mocha/issues/5052
7878
# Ref https://nodejs.org/en/about/previous-releases
79-
node-versions: "20.19.4,22.18.0,24.6.0"
79+
node-versions: "20.19.4,22.22.3,24.6.0"
8080
npm-script: test-node:${{ matrix.test-part }}
8181
coverage: ${{ matrix.coverage }}
8282

lib/cli/commands.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* @module
66
*/
77

8+
import { createRequire } from "node:module";
9+
810
export * as init from "./init.mjs";
911
// default command
10-
export { default as run } from "./run.js";
12+
/* istanbul ignore next */
13+
export const run = createRequire(import.meta.url)("./run.mjs");

lib/cli/run.js renamed to lib/cli/run.mjs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
"use strict";
2-
31
/**
42
* Definition for Mocha's default ("run tests") command
53
*
64
* @module
75
* @private
86
*/
97

10-
const pc = require("picocolors");
11-
const Mocha = require("../mocha");
12-
const {
8+
import pc from "picocolors";
9+
import Mocha from "../mocha.js";
10+
import {
1311
createUnsupportedError,
1412
createInvalidArgumentValueError,
1513
createMissingArgumentError,
16-
} = require("../errors.mjs");
14+
} from "../errors.mjs";
15+
16+
import runHelpers from "./run-helpers.js";
17+
import { ONE_AND_DONES, ONE_AND_DONE_ARGS } from "./one-and-dones.mjs";
18+
import debugModule from "debug";
19+
import defaults from "../mocharc.json" with { type: "json" };
20+
import { types, aliases } from "./run-option-metadata.mjs";
21+
import utils from "../utils.js";
1722

18-
const {
19-
list,
20-
handleRequires,
21-
validateLegacyPlugin,
22-
runMocha,
23-
} = require("./run-helpers");
24-
const { ONE_AND_DONES, ONE_AND_DONE_ARGS } = require("./one-and-dones.mjs");
25-
const debug = require("debug")("mocha:cli:run");
26-
const defaults = require("../mocharc.json");
27-
const { types, aliases } = require("./run-option-metadata.mjs");
28-
const { isCI, logSymbols } = require("../utils");
23+
const { list, handleRequires, validateLegacyPlugin, runMocha } = runHelpers;
24+
const { isCI, logSymbols } = utils;
25+
const debug = debugModule("mocha:cli:run");
2926

3027
/**
3128
* Logical option groups
@@ -40,11 +37,11 @@ const GROUPS = {
4037
CONFIG: "Configuration",
4138
};
4239

43-
exports.command = ["$0 [spec..]", "inspect"];
40+
export const command = ["$0 [spec..]", "inspect"];
4441

45-
exports.describe = "Run tests with Mocha";
42+
export const describe = "Run tests with Mocha";
4643

47-
exports.builder = (yargs) =>
44+
export const builder = (yargs) =>
4845
yargs
4946
.options({
5047
"allow-uncaught": {
@@ -368,7 +365,7 @@ exports.builder = (yargs) =>
368365
.number(types.number)
369366
.alias(aliases);
370367

371-
exports.handler = async function (argv) {
368+
export async function handler(argv) {
372369
debug("post-yargs config", argv);
373370
const mocha = new Mocha(argv);
374371

@@ -378,4 +375,4 @@ exports.handler = async function (argv) {
378375
console.error("\n Exception during run:", err);
379376
process.exit(1);
380377
}
381-
};
378+
}

test/node-unit/cli/run.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const { builder } = require("../../../lib/cli/run");
3+
const { builder } = require("../../../lib/cli/run.mjs");
44
const { types } = require("../../../lib/cli/run-option-metadata.mjs");
55

66
describe("command", function () {
@@ -20,5 +20,16 @@ describe("command", function () {
2020
});
2121
});
2222
});
23+
24+
it("is available as an ESM command module", async function () {
25+
const runModule = await import("../../../lib/cli/run.mjs");
26+
27+
expect(runModule, "to satisfy", {
28+
command: ["$0 [spec..]", "inspect"],
29+
describe: "Run tests with Mocha",
30+
builder: expect.it("to be a function"),
31+
handler: expect.it("to be a function"),
32+
});
33+
});
2334
});
2435
});

0 commit comments

Comments
 (0)