☕️ Classic, reliable, trusted test framework for Node.js and the browser
fix: allow importing ESM interface and reporters (#5563)
Nikolay Vitkovbc9fc842213d
diff --git a/.github/release-please/manifest.json b/.github/release-please/manifest.jsonindex 175a8321..9fa5ff9e 100644--- a/.github/release-please/manifest.json+++ b/.github/release-please/manifest.json@@ -1 +1 @@-{".":"12.0.0-beta-2"}+{ ".": "12.0.0-beta-2" }diff --git a/lib/mocha.js b/lib/mocha.jsindex f95ccabc..9f33b155 100644--- a/lib/mocha.js+++ b/lib/mocha.js@@ -329,6 +329,10 @@ Mocha.prototype.reporter = function (reporterName, reporterOptions) {}}}+ if (reporter.default) {+ reporter = reporter.default;+ }+this._reporter = reporter;}this.options.reporterOption = reporterOptions;@@ -363,6 +367,10 @@ Mocha.prototype.ui = function (ui) {}}}+ if (bindInterface.default) {+ bindInterface = bindInterface.default;+ }+bindInterface(this.suite);this.suite.on(EVENT_FILE_PRE_REQUIRE, function (context) {diff --git a/test/integration/fixtures/options/reporter-esm.fixture.mjs b/test/integration/fixtures/options/reporter-esm.fixture.mjsnew file mode 100644index 00000000..52a9d776--- /dev/null+++ b/test/integration/fixtures/options/reporter-esm.fixture.mjs@@ -0,0 +1,5 @@+function SimplerReporter(runner, options) {+ console.log(JSON.stringify(options.reporterOption));+}++export default SimplerReporter;diff --git a/test/integration/fixtures/simple-ui.fixture.mjs b/test/integration/fixtures/simple-ui.fixture.mjsnew file mode 100644index 00000000..0fa82096--- /dev/null+++ b/test/integration/fixtures/simple-ui.fixture.mjs@@ -0,0 +1,39 @@+import Mocha from '../../../lib/mocha.js'+import MochaInterface from '../../../lib/interfaces/common.js';++const Test = Mocha.Test;+const EVENT_FILE_PRE_REQUIRE = Mocha.Suite.constants.EVENT_FILE_PRE_REQUIRE;++/**+ * A simple UI that only exposes a single function: test+ */+function SimpleUI(suite) {+ suite.on(EVENT_FILE_PRE_REQUIRE, function(+ context,+ file,+ mocha+ ) {+ const common = MochaInterface(+ [suite],+ context+ );++ context.run = mocha.options.delay && common.runWithSuite(suite);++ /**+ * Describes a specification or test-case with the given `title`+ * and callback `fn` acting as a thunk.+ */+ context.test = function(title, fn) {+ const test = new Test(title, fn);+ test.file = file;+ suite.addTest(test);++ return test;+ };+ });+};++Mocha.interfaces['simple-ui'] = SimpleUI;++export default SimpleUI;diff --git a/test/integration/options/reporter.spec.js b/test/integration/options/reporter.spec.jsnew file mode 100644index 00000000..3d6f5773--- /dev/null+++ b/test/integration/options/reporter.spec.js@@ -0,0 +1,30 @@+"use strict";++var runMocha = require("../helpers").runMocha;+var path = require("node:path");++describe("--reporter", function () {+ it("should work for ESM", function (done) {+ runMocha(+ "passing.fixture.js",+ [+ "--reporter",+ path.join(+ __dirname,+ "..",+ "fixtures",+ "options",+ "reporter-esm.fixture.mjs",+ ),+ ],+ function (err, res) {+ if (err) {+ return done(err);+ }+ expect(res, "to have passed");+ done();+ },+ "inherit",+ );+ });+});diff --git a/test/integration/options/ui.spec.js b/test/integration/options/ui.spec.jsindex 870ea4b5..1e3937bb 100644--- a/test/integration/options/ui.spec.js+++ b/test/integration/options/ui.spec.js@@ -4,7 +4,8 @@ var helpers = require("../helpers");var runMochaJSON = helpers.runMochaJSON;describe("--ui", function () {- var simpleUiPath = require.resolve("../fixtures/simple-ui.fixture");+ var simpleUiPath = require.resolve("../fixtures/simple-ui.fixture.js");+ var simpleUiESMPath = require.resolve("../fixtures/simple-ui.fixture.mjs");it("should load interface and run it", function (done) {runMochaJSON(@@ -35,4 +36,34 @@ describe("--ui", function () {},);});++ it("should work for ESM", function (done) {+ runMochaJSON(+ "test-for-simple-ui",+ ["--require", simpleUiESMPath, "--ui", "simple-ui"],+ function (err, res) {+ if (err) {+ done(err);+ return;+ }+ expect(res, "to have passed");+ done();+ },+ );+ });++ it("should work for ESM when imported via path", function (done) {+ runMochaJSON(+ "test-for-simple-ui",+ ["--ui", simpleUiESMPath],+ function (err, res) {+ if (err) {+ done(err);+ return;+ }+ expect(res, "to have passed");+ done();+ },+ );+ });});