From 29de573ba62bcee0ee46f5b5f6cfddcf4ecd9597 Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Tue, 31 Mar 2026 15:04:23 +0200 Subject: [PATCH 1/2] refactor: remove unused `errors.js` functions - `warn()` is no longer used anywhere - `createInvalidPluginError()` is no longer used anywhere and has been deprecated since #4360, for 6 years now - `deprecate()` was only used by `createInvalidPluginError()` - `emitWarning()` was only used by `warn()` and `deprecate()` and contained an unnecessary polyfill for `process.emitWarning`, a function that has existed since Node 6.0.0 with us now requiring at least Node 20.19.0 Ref: mochajs/mocha#5357 (v12 Release Plan) --- lib/errors.js | 64 ---------------- test/integration/deprecate.spec.js | 23 ------ .../integration/fixtures/deprecate.fixture.js | 9 --- test/unit/errors.spec.js | 76 ------------------- test/unit/mocha.spec.js | 2 - test/unit/suite.spec.js | 5 -- 6 files changed, 179 deletions(-) delete mode 100644 test/integration/deprecate.spec.js delete mode 100644 test/integration/fixtures/deprecate.fixture.js diff --git a/lib/errors.js b/lib/errors.js index 167ecc7b0c..4a0a552d05 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -17,51 +17,6 @@ const { isCI } = require("./utils"); * @module */ -/** - * process.emitWarning or a polyfill - * @see https://nodejs.org/api/process.html#process_process_emitwarning_warning_options - * @ignore - */ -const emitWarning = (msg, type) => { - if (process.emitWarning) { - process.emitWarning(msg, type); - } else { - /* istanbul ignore next */ - process.nextTick(function () { - console.warn(type + ": " + msg); - }); - } -}; - -/** - * Show a deprecation warning. Each distinct message is only displayed once. - * Ignores empty messages. - * - * @param {string} [msg] - Warning to print - * @private - */ -const deprecate = (msg) => { - msg = String(msg); - if (msg && !deprecate.cache[msg]) { - deprecate.cache[msg] = true; - emitWarning(msg, "DeprecationWarning"); - } -}; -deprecate.cache = {}; - -/** - * Show a generic warning. - * Ignores empty messages. - * - * @param {string} [msg] - Warning to print - * @private - */ -const warn = (msg) => { - if (msg) { - emitWarning(msg); - } -}; - /** * A set containing all string values of all Mocha error constants, for use by {@link isMochaError}. * @private @@ -235,22 +190,6 @@ function createInvalidLegacyPluginError(message, pluginType, pluginId) { } } -/** - * **DEPRECATED**. Use {@link createInvalidLegacyPluginError} instead Dynamically creates a plugin-type-specific error based on plugin type - * @deprecated - * @param {string} message - Error message - * @param {"reporter"|"interface"} pluginType - Plugin type. Future: expand as needed - * @param {string} [pluginId] - Name/path of plugin, if any - * @throws When `pluginType` is not known - * @public - * @static - * @returns {Error} - */ -function createInvalidPluginError(...args) { - deprecate("Use createInvalidLegacyPluginError() instead"); - return createInvalidLegacyPluginError(...args); -} - /** * Creates an error object to be thrown when a mocha object's `run` method is executed while it is already disposed. * @param {string} message The error message to be displayed. @@ -430,7 +369,6 @@ module.exports = { createInvalidInterfaceError, createInvalidLegacyPluginError, createInvalidPluginDefinitionError, - createInvalidPluginError, createInvalidPluginImplementationError, createInvalidReporterError, createMissingArgumentError, @@ -441,7 +379,5 @@ module.exports = { createTimeoutError, createUnparsableFileError, createUnsupportedError, - deprecate, isMochaError, - warn, }; diff --git a/test/integration/deprecate.spec.js b/test/integration/deprecate.spec.js deleted file mode 100644 index 20345330e0..0000000000 --- a/test/integration/deprecate.spec.js +++ /dev/null @@ -1,23 +0,0 @@ -"use strict"; - -var assert = require("node:assert"); -var run = require("./helpers").runMocha; -var args = []; - -describe("utils.deprecate test", function () { - it("should print unique deprecation only once", function (done) { - run( - "deprecate.fixture.js", - args, - function (err, res) { - if (err) { - return done(err); - } - var result = res.output.match(/deprecated thing/g) || []; - assert.strictEqual(result.length, 2); - done(); - }, - { stdio: "pipe" }, - ); - }); -}); diff --git a/test/integration/fixtures/deprecate.fixture.js b/test/integration/fixtures/deprecate.fixture.js deleted file mode 100644 index 031ad74a44..0000000000 --- a/test/integration/fixtures/deprecate.fixture.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -var errors = require("../../../lib/errors"); - -it('consolidates identical calls to deprecate', function() { - errors.deprecate("suite foo did a deprecated thing"); - errors.deprecate("suite foo did a deprecated thing"); - errors.deprecate("suite bar did a deprecated thing"); -}); diff --git a/test/unit/errors.spec.js b/test/unit/errors.spec.js index 0847d71321..7b5feaacb8 100644 --- a/test/unit/errors.spec.js +++ b/test/unit/errors.spec.js @@ -83,82 +83,6 @@ describe("Errors", function () { }); }); - describe("deprecate()", function () { - var emitWarning; - - beforeEach(function () { - if (process.emitWarning) { - emitWarning = process.emitWarning; - sinon.stub(process, "emitWarning"); - } else { - process.emitWarning = sinon.spy(); - } - errors.deprecate.cache = {}; - }); - - afterEach(function () { - // if this is not set, then we created it, so we should remove it. - if (!emitWarning) { - delete process.emitWarning; - } - }); - - it("should coerce its parameter to a string", function () { - errors.deprecate(1); - expect(process.emitWarning, "to have a call satisfying", [ - "1", - "DeprecationWarning", - ]); - }); - - it("should cache the message", function () { - errors.deprecate("foo"); - errors.deprecate("foo"); - expect(process.emitWarning, "was called times", 1); - }); - - it("should ignore falsy messages", function () { - errors.deprecate(""); - expect(process.emitWarning, "was not called"); - }); - }); - - describe("warn()", function () { - var emitWarning; - - beforeEach(function () { - if (process.emitWarning) { - emitWarning = process.emitWarning; - sinon.stub(process, "emitWarning"); - } else { - process.emitWarning = sinon.spy(); - } - }); - - afterEach(function () { - // if this is not set, then we created it, so we should remove it. - if (!emitWarning) { - delete process.emitWarning; - } - }); - - it("should call process.emitWarning", function () { - errors.warn("foo"); - expect(process.emitWarning, "was called times", 1); - }); - - it("should not cache messages", function () { - errors.warn("foo"); - errors.warn("foo"); - expect(process.emitWarning, "was called times", 2); - }); - - it("should ignore falsy messages", function () { - errors.warn(""); - expect(process.emitWarning, "was not called"); - }); - }); - describe("isMochaError()", function () { describe("when provided an Error object having a known Mocha error code", function () { it("should return true", function () { diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 511ce653d0..a2efc50539 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -4,7 +4,6 @@ var sinon = require("sinon"); var EventEmitter = require("node:events").EventEmitter; var Mocha = require("../../lib/mocha"); var utils = require("../../lib/utils"); -const errors = require("../../lib/errors"); describe("Mocha", function () { /** @@ -78,7 +77,6 @@ describe("Mocha", function () { Suite = sinon.stub(Mocha, "Suite").returns(suite); Suite.constants = {}; - sinon.stub(errors, "warn"); sinon.stub(utils, "isString"); sinon.stub(utils, "noop"); }); diff --git a/test/unit/suite.spec.js b/test/unit/suite.spec.js index c66cf1493a..daa4ae159b 100644 --- a/test/unit/suite.spec.js +++ b/test/unit/suite.spec.js @@ -3,7 +3,6 @@ const Mocha = require("../../lib/mocha"); const { Suite, Test, Context } = Mocha; const sinon = require("sinon"); -const errors = require("../../lib/errors"); function supportsFunctionNames() { return function foo() {}.name === "foo"; @@ -527,10 +526,6 @@ describe("Suite", function () { }); describe("constructor", function () { - beforeEach(function () { - sinon.stub(errors, "deprecate"); - }); - /* eslint no-new: off */ it("should throw an error if the title isn't a string", function () { expect(function () { From 98ca26da8c7e7b46264169a5239cf472a17367fa Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Tue, 31 Mar 2026 16:42:41 +0200 Subject: [PATCH 2/2] docs: update outdated module level JSDoc comment --- lib/errors.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/errors.js b/lib/errors.js index 4a0a552d05..b2061c2eb9 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -12,8 +12,7 @@ const { constants } = require("./error-constants.js"); const { isCI } = require("./utils"); /** - * Contains error codes, factory functions to create throwable error objects, - * and warning/deprecation functions. + * Contains error codes and factory functions to create throwable error objects * @module */