Skip to content

Commit 158effd

Browse files
fix: change Pending to properly extend Error (#5679)
* fix: change Pending to properly extend Error * npx prettier --write .
1 parent a9c9b90 commit 158effd

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

lib/context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Context.prototype.slow = function (ms) {
6969
* Mark a test as skipped.
7070
*
7171
* @private
72-
* @throws Pending
72+
* @throws PendingError
7373
*/
7474
Context.prototype.skip = function () {
7575
this.runnable().skip();

lib/pending.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
@module Pending
55
*/
66

7-
module.exports = Pending;
8-
97
/**
10-
* Initialize a new `Pending` error with the given message.
8+
* Initialize a new `PendingError` error with the given message.
119
*
1210
* @param {string} message
1311
*/
14-
function Pending(message) {
15-
this.message = message;
12+
class PendingError extends Error {
13+
constructor(message) {
14+
super(message);
15+
this.name = "PendingError";
16+
}
1617
}
18+
19+
module.exports = PendingError;

lib/runnable.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
var EventEmitter = require("node:events").EventEmitter;
4-
var Pending = require("./pending");
4+
var PendingError = require("./pending");
55
var debug = require("debug")("mocha:runnable");
66
var milliseconds = require("ms");
77
var utils = require("./utils");
@@ -143,7 +143,7 @@ Runnable.prototype.slow = function (ms) {
143143
*/
144144
Runnable.prototype.skip = function () {
145145
this.pending = true;
146-
throw new Pending("sync skip; aborting execution");
146+
throw new PendingError("sync skip; aborting execution");
147147
};
148148

149149
/**
@@ -331,15 +331,15 @@ Runnable.prototype.run = function (fn) {
331331
this.pending = true;
332332
done();
333333
// halt execution, the uncaught handler will ignore the failure.
334-
throw new Pending("async skip; aborting execution");
334+
throw new PendingError("async skip; aborting execution");
335335
};
336336

337337
try {
338338
callFnAsync(this.fn);
339339
} catch (err) {
340340
// handles async runnables which actually run synchronously
341341
errorWasHandled = true;
342-
if (err instanceof Pending) {
342+
if (err instanceof PendingError) {
343343
return; // done() is already called in this.skip()
344344
} else if (this.allowUncaught) {
345345
throw err;
@@ -354,7 +354,7 @@ Runnable.prototype.run = function (fn) {
354354
callFn(this.fn);
355355
} catch (err) {
356356
errorWasHandled = true;
357-
if (err instanceof Pending) {
357+
if (err instanceof PendingError) {
358358
return done();
359359
} else if (this.allowUncaught) {
360360
throw err;

lib/runner.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @private
1010
*/
1111
var EventEmitter = require("node:events").EventEmitter;
12-
var Pending = require("./pending");
12+
var PendingError = require("./pending");
1313
var utils = require("./utils");
1414
var debug = require("debug")("mocha:runner");
1515
var Runnable = require("./runnable");
@@ -1105,8 +1105,8 @@ Runner.prototype._uncaught = function (err) {
11051105
this,
11061106
);
11071107
}
1108-
if (err instanceof Pending) {
1109-
debug("uncaught(): caught a Pending");
1108+
if (err instanceof PendingError) {
1109+
debug("uncaught(): caught a PendingError");
11101110
return;
11111111
}
11121112
// browser does not exit script when throwing in global.onerror()

test/unit/runner.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const path = require("node:path");
44
const sinon = require("sinon");
55
const Mocha = require("../../lib/mocha");
6-
const Pending = require("../../lib/pending");
6+
const PendingError = require("../../lib/pending");
77
const { Suite, Runner, Test, Hook, Runnable } = Mocha;
88
const { noop } = Mocha.utils;
99
const { FATAL, MULTIPLE_DONE, UNSUPPORTED } =
@@ -966,9 +966,9 @@ describe("Runner", function () {
966966
});
967967
});
968968

969-
describe("when argument is a Pending", function () {
969+
describe("when argument is a PendingError", function () {
970970
it("should ignore argument and return", function () {
971-
var err = new Pending();
971+
var err = new PendingError();
972972
expect(runner.uncaught(err), "to be undefined");
973973
});
974974
});

0 commit comments

Comments
 (0)