Skip to content

Commit 4917d8c

Browse files
TrottMylesBorins
authored andcommitted
assert: improve assert.fail() API
assert.fail() has two possible function signatures, both of which are not intuitive. It virtually guarantees that people who try to use assert.fail() without carefully reading the docs will end up using it incorrectly. This change maintains backwards compatibility with the two valid uses (arguments 1 2 and 4 supplied but argument 3 falsy, and argument 3 supplied but arguments 1 2 and 4 all falsy) but also adds the far more intuitive first-argument-only and first-two-arguments-only possibilities. assert.fail('boom'); // AssertionError: boom assert.fail('a', 'b'); // AssertionError: 'a' != 'b' Backport-PR-URL: #15479 PR-URL: #12293 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent a7d4cad commit 4917d8c

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

doc/api/assert.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,15 @@ If the values are not equal, an `AssertionError` is thrown with a `message`
194194
property set equal to the value of the `message` parameter. If the `message`
195195
parameter is undefined, a default error message is assigned.
196196

197+
## assert.fail(message)
197198
## assert.fail(actual, expected[, message[, operator[, stackStartFunction]]])
198199
<!-- YAML
199200
added: v0.1.21
200201
-->
201202
* `actual` {any}
202203
* `expected` {any}
203204
* `message` {any}
204-
* `operator` {string}
205+
* `operator` {string} (default: '!=')
205206
* `stackStartFunction` {function} (default: `assert.fail`)
206207

207208
Throws an `AssertionError`. If `message` is falsy, the error message is set as
@@ -221,6 +222,12 @@ assert.fail(1, 2, 'fail');
221222

222223
assert.fail(1, 2, 'whoops', '>');
223224
// AssertionError: whoops
225+
226+
assert.fail('boom');
227+
// AssertionError: boom
228+
229+
assert.fail('a', 'b');
230+
// AssertionError: 'a' != 'b'
224231
```
225232

226233
Example use of `stackStartFunction` for truncating the exception's stacktrace:

lib/assert.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ function getMessage(self) {
7777
// display purposes.
7878

7979
function fail(actual, expected, message, operator, stackStartFunction) {
80+
if (arguments.length === 1)
81+
message = actual;
82+
if (arguments.length === 2)
83+
operator = '!=';
8084
throw new assert.AssertionError({
8185
message: message,
8286
actual: actual,

test/parallel/test-assert-fail.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,36 @@
33
require('../common');
44
const assert = require('assert');
55

6+
// no args
7+
assert.throws(
8+
() => { assert.fail(); },
9+
/^AssertionError: undefined undefined undefined$/
10+
);
11+
12+
// one arg = message
13+
assert.throws(
14+
() => { assert.fail('custom message'); },
15+
/^AssertionError: custom message$/
16+
);
17+
18+
// two args only, operator defaults to '!='
19+
assert.throws(
20+
() => { assert.fail('first', 'second'); },
21+
/^AssertionError: 'first' != 'second'$/
22+
);
23+
24+
// three args
25+
assert.throws(
26+
() => { assert.fail('ignored', 'ignored', 'another custom message'); },
27+
/^AssertionError: another custom message$/
28+
);
29+
30+
// no third arg (but a fourth arg)
31+
assert.throws(
32+
() => { assert.fail('first', 'second', undefined, 'operator'); },
33+
/^AssertionError: 'first' operator 'second'$/
34+
);
35+
636
// The stackFrameFunction should exclude the foo frame
737
assert.throws(
838
function foo() { assert.fail('first', 'second', 'message', '!==', foo); },

0 commit comments

Comments
 (0)