Skip to content

Commit e83ea62

Browse files
lib: add innerFail as private method to Assert class
1 parent 2cec1d7 commit e83ea62

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

lib/assert.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class Assert {
8787
this.AssertionError = AssertionError;
8888
}
8989

90+
#innerFail(obj) {
91+
if (obj.message instanceof Error) throw obj.message;
92+
93+
throw new AssertionError(obj);
94+
}
95+
9096
#internalMatch(string, regexp, message, fn) {
9197
if (!isRegExp(regexp)) {
9298
throw new ERR_INVALID_ARG_TYPE(
@@ -140,7 +146,7 @@ class Assert {
140146
err.operator = fn.name;
141147
throw err;
142148
}
143-
innerFail({
149+
this.#innerFail({
144150
actual,
145151
expected,
146152
message,
@@ -293,7 +299,7 @@ class Assert {
293299
}
294300
details += message ? `: ${message}` : '.';
295301
const fnType = stackStartFn === Assert.prototype.rejects ? 'rejection' : 'exception';
296-
innerFail({
302+
this.#innerFail({
297303
actual: undefined,
298304
expected: error,
299305
operator: stackStartFn.name,
@@ -321,7 +327,7 @@ class Assert {
321327
const details = message ? `: ${message}` : '.';
322328
const fnType = stackStartFn === Assert.prototype.doesNotReject ?
323329
'rejection' : 'exception';
324-
innerFail({
330+
this.#innerFail({
325331
actual,
326332
expected: error,
327333
operator: stackStartFn.name,
@@ -428,7 +434,7 @@ class Assert {
428434
}
429435
// eslint-disable-next-line eqeqeq
430436
if (actual != expected && (!NumberIsNaN(actual) || !NumberIsNaN(expected))) {
431-
innerFail({
437+
this.#innerFail({
432438
actual,
433439
expected,
434440
message,
@@ -452,7 +458,7 @@ class Assert {
452458
}
453459
// eslint-disable-next-line eqeqeq
454460
if (actual == expected || (NumberIsNaN(actual) && NumberIsNaN(expected))) {
455-
innerFail({
461+
this.#innerFail({
456462
actual,
457463
expected,
458464
message,
@@ -475,7 +481,7 @@ class Assert {
475481
}
476482
if (isDeepEqual === undefined) lazyLoadComparison();
477483
if (!isDeepEqual(actual, expected)) {
478-
innerFail({
484+
this.#innerFail({
479485
actual,
480486
expected,
481487
message,
@@ -498,7 +504,7 @@ class Assert {
498504
}
499505
if (isDeepEqual === undefined) lazyLoadComparison();
500506
if (isDeepEqual(actual, expected)) {
501-
innerFail({
507+
this.#innerFail({
502508
actual,
503509
expected,
504510
message,
@@ -522,7 +528,7 @@ class Assert {
522528
}
523529
if (isDeepEqual === undefined) lazyLoadComparison();
524530
if (!isDeepStrictEqual(actual, expected)) {
525-
innerFail({
531+
this.#innerFail({
526532
actual,
527533
expected,
528534
message,
@@ -546,7 +552,7 @@ class Assert {
546552
}
547553
if (isDeepEqual === undefined) lazyLoadComparison();
548554
if (isDeepStrictEqual(actual, expected)) {
549-
innerFail({
555+
this.#innerFail({
550556
actual,
551557
expected,
552558
message,
@@ -568,7 +574,7 @@ class Assert {
568574
throw new ERR_MISSING_ARGS('actual', 'expected');
569575
}
570576
if (!ObjectIs(actual, expected)) {
571-
innerFail({
577+
this.#innerFail({
572578
actual,
573579
expected,
574580
message,
@@ -590,7 +596,7 @@ class Assert {
590596
throw new ERR_MISSING_ARGS('actual', 'expected');
591597
}
592598
if (ObjectIs(actual, expected)) {
593-
innerFail({
599+
this.#innerFail({
594600
actual,
595601
expected,
596602
message,
@@ -617,7 +623,7 @@ class Assert {
617623
}
618624
if (isDeepEqual === undefined) lazyLoadComparison();
619625
if (!isPartialStrictEqual(actual, expected)) {
620-
innerFail({
626+
this.#innerFail({
621627
actual,
622628
expected,
623629
message,
@@ -775,13 +781,6 @@ assert.ok = ok;
775781
// may be undefined if not provided. All assertion methods provide
776782
// both the actual and expected values to the assertion error for
777783
// display purposes.
778-
779-
function innerFail(obj) {
780-
if (obj.message instanceof Error) throw obj.message;
781-
782-
throw new AssertionError(obj);
783-
}
784-
785784
class Comparison {
786785
constructor(obj, keys, actual) {
787786
for (const key of keys) {

test/fixtures/errors/error_exit.snapshot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Exiting with code=1
22
node:assert:*
3-
throw new AssertionError(obj);
4-
^
3+
throw new AssertionError(obj);
4+
^
55

66
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
77

0 commit comments

Comments
 (0)