Skip to content

armgit / mocha

Public Imported from GitHub
0

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

feat: disable timeouts whenever the Node.js inspector is active (#6397)

bytedoef57f4b19b4a4
diff --git a/lib/cli/node-flags.cjs b/lib/cli/node-flags.cjs
index f5b46d24..c92d39cb 100644
--- a/lib/cli/node-flags.cjs
+++ b/lib/cli/node-flags.cjs
@@ -6,6 +6,7 @@
* @module
*/
+const inspector = require("node:inspector");
const nodeFlags = process.allowedNodeEnvironmentFlags;
const {
isMochaFlag,
@@ -69,6 +70,16 @@ function impliesNoTimeouts(flag) {
return debugFlags.has(flag);
}
+/**
+ * Returns `true` if the Node.js inspector is active.
+ * @returns {boolean}
+ * @private
+ */
+function isInspectorActive() {
+ return inspector.url() !== undefined;
+}
+
exports.isNodeFlag = isNodeFlag;
exports.impliesNoTimeouts = impliesNoTimeouts;
+exports.isInspectorActive = isInspectorActive;
exports.unparseNodeFlags = unparseNodeFlags;
diff --git a/lib/cli/run.cjs b/lib/cli/run.cjs
index c9ed2195..0cf22499 100644
--- a/lib/cli/run.cjs
+++ b/lib/cli/run.cjs
@@ -20,6 +20,7 @@ const {
validateLegacyPlugin,
runMocha,
} = require("./run-helpers.cjs");
+const { isInspectorActive } = require("./node-flags.cjs");
const { ONE_AND_DONES } = require("./one-and-dones.js");
const debug = require("debug")("mocha:cli:run");
@@ -136,6 +137,10 @@ exports.normalizeRunOptions = normalizeRunOptions;
exports.handler = async function (argv) {
normalizeRunOptions(argv);
+ if (isInspectorActive()) {
+ debug("inspector is active; disabling timeouts");
+ argv.timeout = 0;
+ }
debug("running with config", argv);
const mocha = new Mocha(argv);
diff --git a/test/integration/fixtures/options/open-inspector.fixture.cjs b/test/integration/fixtures/options/open-inspector.fixture.cjs
new file mode 100644
index 00000000..6b5224af
--- /dev/null
+++ b/test/integration/fixtures/options/open-inspector.fixture.cjs
@@ -0,0 +1,7 @@
+'use strict';
+
+const inspector = require('node:inspector');
+
+if (inspector.url() === undefined) {
+ inspector.open(0);
+}
diff --git a/test/integration/options/timeout.spec.cjs b/test/integration/options/timeout.spec.cjs
index e86fd550..4900b314 100644
--- a/test/integration/options/timeout.spec.cjs
+++ b/test/integration/options/timeout.spec.cjs
@@ -88,6 +88,66 @@ describe("--timeout", function () {
);
});
+ it('should disable timeout with "-n inspect=0"', function (done) {
+ var fixture = "options/slow-test.fixture.js";
+ runMochaJSON(
+ fixture,
+ ["-n", "inspect=0", "--timeout", "200"],
+ function (err, res) {
+ if (err) {
+ done(err);
+ return;
+ }
+ expect(res, "to have passed").and("to have passed test count", 2);
+ done();
+ },
+ );
+ });
+
+ it("should disable timeout when the inspector is enabled via NODE_OPTIONS", function (done) {
+ var fixture = "options/slow-test.fixture.js";
+ var nodeOptions = process.env.NODE_OPTIONS || "";
+ runMochaJSON(
+ fixture,
+ ["--timeout", "200"],
+ function (err, res) {
+ if (err) {
+ done(err);
+ return;
+ }
+ expect(res, "to have passed").and("to have passed test count", 2);
+ done();
+ },
+ {
+ env: {
+ ...process.env,
+ NODE_OPTIONS: `${nodeOptions} --inspect=0`.trim(),
+ },
+ },
+ );
+ });
+
+ it("should disable timeout when the inspector is opened without inspect flags", function (done) {
+ var fixture = "options/slow-test.fixture.js";
+ runMochaJSON(
+ fixture,
+ [
+ "--require",
+ require.resolve("../fixtures/options/open-inspector.fixture.cjs"),
+ "--timeout",
+ "200",
+ ],
+ function (err, res) {
+ if (err) {
+ done(err);
+ return;
+ }
+ expect(res, "to have passed").and("to have passed test count", 2);
+ done();
+ },
+ );
+ });
+
it("should complete tests having unref'd async behavior", function (done) {
runMochaJSON(
"options/timeout-unref.fixture.js",
diff --git a/test/node-unit/cli/node-flags.spec.cjs b/test/node-unit/cli/node-flags.spec.cjs
index 0d8d7a29..5c43044a 100644
--- a/test/node-unit/cli/node-flags.spec.cjs
+++ b/test/node-unit/cli/node-flags.spec.cjs
@@ -1,9 +1,12 @@
"use strict";
+const inspector = require("node:inspector");
+const sinon = require("sinon");
const nodeEnvFlags = [...process.allowedNodeEnvironmentFlags];
const {
isNodeFlag,
impliesNoTimeouts,
+ isInspectorActive,
unparseNodeFlags,
} = require("../../../lib/cli/node-flags.cjs");
@@ -93,6 +96,22 @@ describe("node-flags", function () {
});
});
+ describe("isInspectorActive()", function () {
+ afterEach(function () {
+ sinon.restore();
+ });
+
+ it("should return false if the inspector is not active", function () {
+ sinon.stub(inspector, "url").returns(undefined);
+ expect(isInspectorActive(), "to be false");
+ });
+
+ it("should return true if the inspector is active", function () {
+ sinon.stub(inspector, "url").returns("ws://127.0.0.1:9229/some-uuid");
+ expect(isInspectorActive(), "to be true");
+ });
+ });
+
describe("unparseNodeFlags()", function () {
it("should handle single v8 flags", function () {
expect(unparseNodeFlags({ "v8-numeric": 100 }), "to equal", [