Skip to content

armgit / mocha

Public Imported from GitHub
0

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

chore: convert context, hook, and stats-collector to ESM (#5853)

Mark Wiemer5ac40707fd21
diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.js
index 109997b2..495d1e28 100644
--- a/lib/cli/watch-run.js
+++ b/lib/cli/watch-run.js
@@ -6,7 +6,7 @@ const chokidar = require("chokidar");
const glob = require("glob");
const isPathInside = require("is-path-inside");
const { minimatch } = require("minimatch");
-const Context = require("../context");
+const Context = require("../context.mjs").Context;
const collectFiles = require("./collect-files");
const { logSymbols } = require("../utils");
diff --git a/lib/context.js b/lib/context.js
deleted file mode 100644
index 13ad1f56..00000000
--- a/lib/context.js
+++ /dev/null
@@ -1,91 +0,0 @@
-"use strict";
-
-/**
- * @typedef {import('./runnable.js')} Runnable
- */
-
-/**
- * @module Context
- */
-/**
- * Expose `Context`.
- */
-
-module.exports = Context;
-
-/**
- * Initialize a new `Context`.
- *
- * @private
- */
-function Context() {}
-
-/**
- * Set or get the context `Runnable` to `runnable`.
- *
- * @private
- * @param {Runnable} runnable
- * @return {Context} context
- */
-Context.prototype.runnable = function (runnable) {
- if (!arguments.length) {
- return this._runnable;
- }
- this.test = this._runnable = runnable;
- return this;
-};
-
-/**
- * Set or get test timeout `ms`.
- *
- * @private
- * @param {number} ms
- * @return {Context} self
- */
-Context.prototype.timeout = function (ms) {
- if (!arguments.length) {
- return this.runnable().timeout();
- }
- this.runnable().timeout(ms);
- return this;
-};
-
-/**
- * Set or get test slowness threshold `ms`.
- *
- * @private
- * @param {number} ms
- * @return {Context} self
- */
-Context.prototype.slow = function (ms) {
- if (!arguments.length) {
- return this.runnable().slow();
- }
- this.runnable().slow(ms);
- return this;
-};
-
-/**
- * Mark a test as skipped.
- *
- * @private
- * @throws PendingError
- */
-Context.prototype.skip = function () {
- this.runnable().skip();
-};
-
-/**
- * Set or get a number of allowed retries on failed tests
- *
- * @private
- * @param {number} n
- * @return {Context} self
- */
-Context.prototype.retries = function (n) {
- if (!arguments.length) {
- return this.runnable().retries();
- }
- this.runnable().retries(n);
- return this;
-};
diff --git a/lib/context.mjs b/lib/context.mjs
new file mode 100644
index 00000000..a5f0b76a
--- /dev/null
+++ b/lib/context.mjs
@@ -0,0 +1,79 @@
+/**
+ * @typedef {import('./runnable.js')} Runnable
+ */
+
+/**
+ * Initialize a new `Context`.
+ *
+ * @private
+ */
+class Context {
+ constructor() {}
+ /**
+ * Set or get the context `Runnable` to `runnable`.
+ *
+ * @private
+ * @param {Runnable} runnable
+ * @return {Context} context
+ */
+ runnable(runnable) {
+ if (!arguments.length) {
+ return this._runnable;
+ }
+ this.test = this._runnable = runnable;
+ return this;
+ }
+ /**
+ * Set or get test timeout `ms`.
+ *
+ * @private
+ * @param {number} ms
+ * @return {Context} self
+ */
+ timeout(ms) {
+ if (!arguments.length) {
+ return this.runnable().timeout();
+ }
+ this.runnable().timeout(ms);
+ return this;
+ }
+ /**
+ * Set or get test slowness threshold `ms`.
+ *
+ * @private
+ * @param {number} ms
+ * @return {Context} self
+ */
+ slow(ms) {
+ if (!arguments.length) {
+ return this.runnable().slow();
+ }
+ this.runnable().slow(ms);
+ return this;
+ }
+ /**
+ * Mark a test as skipped.
+ *
+ * @private
+ * @throws PendingError
+ */
+ skip() {
+ this.runnable().skip();
+ }
+ /**
+ * Set or get a number of allowed retries on failed tests
+ *
+ * @private
+ * @param {number} n
+ * @return {Context} self
+ */
+ retries(n) {
+ if (!arguments.length) {
+ return this.runnable().retries();
+ }
+ this.runnable().retries(n);
+ return this;
+ }
+}
+
+export { Context };
diff --git a/lib/hook.js b/lib/hook.mjs
similarity index 90%
rename from lib/hook.js
rename to lib/hook.mjs
index 483251b3..15175cb8 100644
--- a/lib/hook.js
+++ b/lib/hook.mjs
@@ -1,8 +1,7 @@
-"use strict";
+import Runnable from "./runnable.js";
+import utils from "./utils.js";
-var Runnable = require("./runnable");
-const { constants } = require("./utils");
-const { MOCHA_ID_PROP_NAME } = constants;
+const { MOCHA_ID_PROP_NAME } = utils.constants;
class Hook extends Runnable {
/**
@@ -78,4 +77,4 @@ class Hook extends Runnable {
}
}
-module.exports = Hook;
+export { Hook };
diff --git a/lib/interfaces/common.js b/lib/interfaces/common.js
index a1406225..3e656e94 100644
--- a/lib/interfaces/common.js
+++ b/lib/interfaces/common.js
@@ -1,7 +1,7 @@
"use strict";
/**
- * @typedef {import('../context.js')} Context
+ * @typedef {import('../context.mjs')} Context
* @typedef {import('../mocha.js')} Mocha
*/
diff --git a/lib/mocha.js b/lib/mocha.js
index 4cbbb0c7..c3ccb46a 100644
--- a/lib/mocha.js
+++ b/lib/mocha.js
@@ -13,7 +13,8 @@ var utils = require("./utils");
var mocharc = require("./mocharc.json");
var Suite = require("./suite");
var esmUtils = require("./nodejs/esm-utils");
-var createStatsCollector = require("./stats-collector");
+var createStatsCollector =
+ require("./stats-collector.mjs").createStatsCollector;
const {
createInvalidReporterError,
createInvalidInterfaceError,
@@ -86,14 +87,14 @@ exports.interfaces = require("./interfaces");
*/
exports.reporters = builtinReporters;
exports.Runnable = require("./runnable");
-exports.Context = require("./context");
+exports.Context = require("./context.mjs").Context;
/**
*
* @memberof Mocha
*/
exports.Runner = require("./runner");
exports.Suite = Suite;
-exports.Hook = require("./hook");
+exports.Hook = require("./hook.mjs").Hook;
exports.Test = require("./test");
let currentContext;
diff --git a/lib/stats-collector.js b/lib/stats-collector.mjs
similarity index 73%
rename from lib/stats-collector.js
rename to lib/stats-collector.mjs
index eb3a3339..00c07c0d 100644
--- a/lib/stats-collector.js
+++ b/lib/stats-collector.mjs
@@ -1,5 +1,3 @@
-"use strict";
-
/**
* @typedef {import('./types.d.ts').StatsCollector} StatsCollector
* @typedef {import('./runner.js')} Runner
@@ -10,16 +8,17 @@
* @module
*/
-var constants = require("./runner").constants;
-var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
-var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
-var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN;
-var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
-var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
-var EVENT_RUN_END = constants.EVENT_RUN_END;
-var EVENT_TEST_END = constants.EVENT_TEST_END;
+import Runner from "./runner.js";
-var Date = global.Date;
+const {
+ EVENT_TEST_PASS,
+ EVENT_TEST_FAIL,
+ EVENT_SUITE_BEGIN,
+ EVENT_RUN_BEGIN,
+ EVENT_TEST_PENDING,
+ EVENT_RUN_END,
+ EVENT_TEST_END,
+} = Runner.constants;
/**
* Provides stats such as test duration, number of tests passed / failed etc., by listening for events emitted by `runner`.
@@ -70,4 +69,4 @@ function createStatsCollector(runner) {
});
}
-module.exports = createStatsCollector;
+export { createStatsCollector };
diff --git a/lib/suite.js b/lib/suite.js
index f65c56ec..ffe63592 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -9,7 +9,7 @@
* @private
*/
const { EventEmitter } = require("node:events");
-const Hook = require("./hook");
+const Hook = require("./hook.mjs").Hook;
var {
assignNewMochaID,
clamp,
diff --git a/test/reporters/helpers.js b/test/reporters/helpers.js
index 859a4118..6477c858 100644
--- a/test/reporters/helpers.js
+++ b/test/reporters/helpers.js
@@ -2,7 +2,8 @@
var sinon = require("sinon");
var errors = require("../../lib/errors");
-var createStatsCollector = require("../../lib/stats-collector");
+var createStatsCollector =
+ require("../../lib/stats-collector.mjs").createStatsCollector;
var createUnsupportedError = errors.createUnsupportedError;
diff --git a/test/reporters/xunit.spec.js b/test/reporters/xunit.spec.js
index 9301358d..7052075d 100644
--- a/test/reporters/xunit.spec.js
+++ b/test/reporters/xunit.spec.js
@@ -4,7 +4,8 @@ var EventEmitter = require("events").EventEmitter;
var fs = require("fs");
var path = require("path");
var sinon = require("sinon");
-var createStatsCollector = require("../../lib/stats-collector");
+var createStatsCollector =
+ require("../../lib/stats-collector.mjs").createStatsCollector;
var events = require("../../").Runner.constants;
var reporters = require("../../").reporters;
var states = require("../../").Runnable.constants;