Skip to content

armgit / mocha

Public Imported from GitHub
0

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

fix: change Pending to properly extend Error (#5679)

Josh Goldberg ✨158effd7de67
diff --git a/lib/context.js b/lib/context.js
index 900a0f4d..13ad1f56 100644
--- a/lib/context.js
+++ b/lib/context.js
@@ -69,7 +69,7 @@ Context.prototype.slow = function (ms) {
* Mark a test as skipped.
*
* @private
- * @throws Pending
+ * @throws PendingError
*/
Context.prototype.skip = function () {
this.runnable().skip();
diff --git a/lib/pending.js b/lib/pending.js
index dedea5eb..336a29bf 100644
--- a/lib/pending.js
+++ b/lib/pending.js
@@ -4,13 +4,16 @@
@module Pending
*/
-module.exports = Pending;
-
/**
- * Initialize a new `Pending` error with the given message.
+ * Initialize a new `PendingError` error with the given message.
*
* @param {string} message
*/
-function Pending(message) {
- this.message = message;
+class PendingError extends Error {
+ constructor(message) {
+ super(message);
+ this.name = "PendingError";
+ }
}
+
+module.exports = PendingError;
diff --git a/lib/runnable.js b/lib/runnable.js
index 97097dda..3224d47a 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -1,7 +1,7 @@
"use strict";
var EventEmitter = require("node:events").EventEmitter;
-var Pending = require("./pending");
+var PendingError = require("./pending");
var debug = require("debug")("mocha:runnable");
var milliseconds = require("ms");
var utils = require("./utils");
@@ -143,7 +143,7 @@ Runnable.prototype.slow = function (ms) {
*/
Runnable.prototype.skip = function () {
this.pending = true;
- throw new Pending("sync skip; aborting execution");
+ throw new PendingError("sync skip; aborting execution");
};
/**
@@ -331,7 +331,7 @@ Runnable.prototype.run = function (fn) {
this.pending = true;
done();
// halt execution, the uncaught handler will ignore the failure.
- throw new Pending("async skip; aborting execution");
+ throw new PendingError("async skip; aborting execution");
};
try {
@@ -339,7 +339,7 @@ Runnable.prototype.run = function (fn) {
} catch (err) {
// handles async runnables which actually run synchronously
errorWasHandled = true;
- if (err instanceof Pending) {
+ if (err instanceof PendingError) {
return; // done() is already called in this.skip()
} else if (this.allowUncaught) {
throw err;
@@ -354,7 +354,7 @@ Runnable.prototype.run = function (fn) {
callFn(this.fn);
} catch (err) {
errorWasHandled = true;
- if (err instanceof Pending) {
+ if (err instanceof PendingError) {
return done();
} else if (this.allowUncaught) {
throw err;
diff --git a/lib/runner.js b/lib/runner.js
index 89220b91..658a9f2f 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -9,7 +9,7 @@
* @private
*/
var EventEmitter = require("node:events").EventEmitter;
-var Pending = require("./pending");
+var PendingError = require("./pending");
var utils = require("./utils");
var debug = require("debug")("mocha:runner");
var Runnable = require("./runnable");
@@ -1105,8 +1105,8 @@ Runner.prototype._uncaught = function (err) {
this,
);
}
- if (err instanceof Pending) {
- debug("uncaught(): caught a Pending");
+ if (err instanceof PendingError) {
+ debug("uncaught(): caught a PendingError");
return;
}
// browser does not exit script when throwing in global.onerror()
diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js
index 6b5206c4..c0adcf69 100644
--- a/test/unit/runner.spec.js
+++ b/test/unit/runner.spec.js
@@ -3,7 +3,7 @@
const path = require("node:path");
const sinon = require("sinon");
const Mocha = require("../../lib/mocha");
-const Pending = require("../../lib/pending");
+const PendingError = require("../../lib/pending");
const { Suite, Runner, Test, Hook, Runnable } = Mocha;
const { noop } = Mocha.utils;
const { FATAL, MULTIPLE_DONE, UNSUPPORTED } =
@@ -966,9 +966,9 @@ describe("Runner", function () {
});
});
- describe("when argument is a Pending", function () {
+ describe("when argument is a PendingError", function () {
it("should ignore argument and return", function () {
- var err = new Pending();
+ var err = new PendingError();
expect(runner.uncaught(err), "to be undefined");
});
});