Skip to content

Commit d33f4b5

Browse files
authored
assert: move assert.fail with multiple arguments to eol
Calling `assert.fail` with multiple arguments has been deprecated for years. Remove it finally. PR-URL: #58532 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 4e06a64 commit d33f4b5

File tree

4 files changed

+12
-205
lines changed

4 files changed

+12
-205
lines changed

doc/api/assert.md

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,107 +1071,6 @@ assert.fail(new TypeError('need array'));
10711071
// TypeError: need array
10721072
```
10731073

1074-
Using `assert.fail()` with more than two arguments is possible but deprecated.
1075-
See below for further details.
1076-
1077-
## `assert.fail(actual, expected[, message[, operator[, stackStartFn]]])`
1078-
1079-
<!-- YAML
1080-
added: v0.1.21
1081-
changes:
1082-
- version: v10.0.0
1083-
pr-url: https://github.com/nodejs/node/pull/18418
1084-
description: Calling `assert.fail()` with more than one argument is
1085-
deprecated and emits a warning.
1086-
-->
1087-
1088-
> Stability: 0 - Deprecated: Use `assert.fail([message])` or other assert
1089-
> functions instead.
1090-
1091-
* `actual` {any}
1092-
* `expected` {any}
1093-
* `message` {string|Error}
1094-
* `operator` {string} **Default:** `'!='`
1095-
* `stackStartFn` {Function} **Default:** `assert.fail`
1096-
1097-
If `message` is falsy, the error message is set as the values of `actual` and
1098-
`expected` separated by the provided `operator`. If just the two `actual` and
1099-
`expected` arguments are provided, `operator` will default to `'!='`. If
1100-
`message` is provided as third argument it will be used as the error message and
1101-
the other arguments will be stored as properties on the thrown object. If
1102-
`stackStartFn` is provided, all stack frames above that function will be
1103-
removed from stacktrace (see [`Error.captureStackTrace`][]). If no arguments are
1104-
given, the default message `Failed` will be used.
1105-
1106-
```mjs
1107-
import assert from 'node:assert/strict';
1108-
1109-
assert.fail('a', 'b');
1110-
// AssertionError [ERR_ASSERTION]: 'a' != 'b'
1111-
1112-
assert.fail(1, 2, undefined, '>');
1113-
// AssertionError [ERR_ASSERTION]: 1 > 2
1114-
1115-
assert.fail(1, 2, 'fail');
1116-
// AssertionError [ERR_ASSERTION]: fail
1117-
1118-
assert.fail(1, 2, 'whoops', '>');
1119-
// AssertionError [ERR_ASSERTION]: whoops
1120-
1121-
assert.fail(1, 2, new TypeError('need array'));
1122-
// TypeError: need array
1123-
```
1124-
1125-
```cjs
1126-
const assert = require('node:assert/strict');
1127-
1128-
assert.fail('a', 'b');
1129-
// AssertionError [ERR_ASSERTION]: 'a' != 'b'
1130-
1131-
assert.fail(1, 2, undefined, '>');
1132-
// AssertionError [ERR_ASSERTION]: 1 > 2
1133-
1134-
assert.fail(1, 2, 'fail');
1135-
// AssertionError [ERR_ASSERTION]: fail
1136-
1137-
assert.fail(1, 2, 'whoops', '>');
1138-
// AssertionError [ERR_ASSERTION]: whoops
1139-
1140-
assert.fail(1, 2, new TypeError('need array'));
1141-
// TypeError: need array
1142-
```
1143-
1144-
In the last three cases `actual`, `expected`, and `operator` have no
1145-
influence on the error message.
1146-
1147-
Example use of `stackStartFn` for truncating the exception's stacktrace:
1148-
1149-
```mjs
1150-
import assert from 'node:assert/strict';
1151-
1152-
function suppressFrame() {
1153-
assert.fail('a', 'b', undefined, '!==', suppressFrame);
1154-
}
1155-
suppressFrame();
1156-
// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
1157-
// at repl:1:1
1158-
// at ContextifyScript.Script.runInThisContext (vm.js:44:33)
1159-
// ...
1160-
```
1161-
1162-
```cjs
1163-
const assert = require('node:assert/strict');
1164-
1165-
function suppressFrame() {
1166-
assert.fail('a', 'b', undefined, '!==', suppressFrame);
1167-
}
1168-
suppressFrame();
1169-
// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
1170-
// at repl:1:1
1171-
// at ContextifyScript.Script.runInThisContext (vm.js:44:33)
1172-
// ...
1173-
```
1174-
11751074
## `assert.ifError(value)`
11761075

11771076
<!-- YAML
@@ -2437,7 +2336,6 @@ assert.partialDeepStrictEqual(
24372336
[`AssertionError`]: #class-assertassertionerror
24382337
[`Class`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
24392338
[`ERR_INVALID_RETURN_VALUE`]: errors.md#err_invalid_return_value
2440-
[`Error.captureStackTrace`]: errors.md#errorcapturestacktracetargetobject-constructoropt
24412339
[`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
24422340
[`assert.deepEqual()`]: #assertdeepequalactual-expected-message
24432341
[`assert.deepStrictEqual()`]: #assertdeepstrictequalactual-expected-message

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2068,12 +2068,15 @@ and `crypto.getFips()` instead.
20682068

20692069
<!-- YAML
20702070
changes:
2071+
- version: REPLACEME
2072+
pr-url: https://github.com/nodejs/node/pull/58532
2073+
description: End-of-Life.
20712074
- version: v10.0.0
20722075
pr-url: https://github.com/nodejs/node/pull/18418
20732076
description: Runtime deprecation.
20742077
-->
20752078

2076-
Type: Runtime
2079+
Type: End-of-Life
20772080

20782081
Using `assert.fail()` with more than one argument is deprecated. Use
20792082
`assert.fail()` with only one argument or use a different `node:assert` module

lib/assert.js

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ function lazyLoadComparison() {
7272
isPartialStrictEqual = comparison.isPartialStrictEqual;
7373
}
7474

75-
let warned = false;
76-
7775
// The assert module provides functions that throw
7876
// AssertionError's when particular conditions are not met. The
7977
// assert module must conform to the following interface.
@@ -95,43 +93,21 @@ function innerFail(obj) {
9593
}
9694

9795
/**
98-
* @param {any} actual
99-
* @param {any} expected
100-
* @param {string | Error} [message]
101-
* @param {string} [operator]
102-
* @param {Function} [stackStartFn]
96+
* Throws an AssertionError with the given message.
97+
* @param {any | Error} [message]
10398
*/
104-
function fail(actual, expected, message, operator, stackStartFn) {
105-
const argsLen = arguments.length;
99+
function fail(message) {
100+
if (isError(message)) throw message;
106101

107102
let internalMessage = false;
108-
if (actual == null && argsLen <= 1) {
109-
internalMessage = true;
103+
if (message === undefined) {
110104
message = 'Failed';
111-
} else if (argsLen === 1) {
112-
message = actual;
113-
actual = undefined;
114-
} else {
115-
if (warned === false) {
116-
warned = true;
117-
process.emitWarning(
118-
'assert.fail() with more than one argument is deprecated. ' +
119-
'Please use assert.strictEqual() instead or only pass a message.',
120-
'DeprecationWarning',
121-
'DEP0094',
122-
);
123-
}
124-
if (argsLen === 2)
125-
operator = '!=';
105+
internalMessage = true;
126106
}
127107

128-
if (message instanceof Error) throw message;
129-
130108
const errArgs = {
131-
actual,
132-
expected,
133-
operator: operator === undefined ? 'fail' : operator,
134-
stackStartFn: stackStartFn || fail,
109+
operator: 'fail',
110+
stackStartFn: fail,
135111
message,
136112
};
137113
const err = new AssertionError(errArgs);

test/parallel/test-assert-fail-deprecation.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)