Skip to content

Commit 758b8b6

Browse files
committed
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' PR-URL: #12293 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b3f2e3b commit 758b8b6

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

doc/api/assert.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,15 @@ If the values are not equal, an `AssertionError` is thrown with a `message`
256256
property set equal to the value of the `message` parameter. If the `message`
257257
parameter is undefined, a default error message is assigned.
258258

259+
## assert.fail(message)
259260
## assert.fail(actual, expected, message, operator)
260261
<!-- YAML
261262
added: v0.1.21
262263
-->
263264
* `actual` {any}
264265
* `expected` {any}
265266
* `message` {any}
266-
* `operator` {string}
267+
* `operator` {string} (default: '!=')
267268

268269
Throws an `AssertionError`. If `message` is falsy, the error message is set as
269270
the values of `actual` and `expected` separated by the provided `operator`.
@@ -277,6 +278,12 @@ assert.fail(1, 2, undefined, '>');
277278

278279
assert.fail(1, 2, 'whoops', '>');
279280
// AssertionError: whoops
281+
282+
assert.fail('boom');
283+
// AssertionError: boom
284+
285+
assert.fail('a', 'b');
286+
// AssertionError: 'a' != 'b'
280287
```
281288

282289
## assert.ifError(value)

lib/assert.js

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ function getMessage(self) {
7979
// display purposes.
8080

8181
function fail(actual, expected, message, operator, stackStartFunction) {
82+
if (arguments.length === 1)
83+
message = actual;
84+
if (arguments.length === 2)
85+
operator = '!=';
8286
throw new assert.AssertionError({
8387
message: message,
8488
actual: actual,

test/parallel/test-assert-fail.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
// no args
6+
assert.throws(
7+
() => { assert.fail(); },
8+
/^AssertionError: undefined undefined undefined$/
9+
);
10+
11+
// one arg = message
12+
assert.throws(
13+
() => { assert.fail('custom message'); },
14+
/^AssertionError: custom message$/
15+
);
16+
17+
// two args only, operator defaults to '!='
18+
assert.throws(
19+
() => { assert.fail('first', 'second'); },
20+
/^AssertionError: 'first' != 'second'$/
21+
);
22+
23+
// three args
24+
assert.throws(
25+
() => { assert.fail('ignored', 'ignored', 'another custom message'); },
26+
/^AssertionError: another custom message$/
27+
);
28+
29+
// no third arg (but a fourth arg)
30+
assert.throws(
31+
() => { assert.fail('first', 'second', undefined, 'operator'); },
32+
/^AssertionError: 'first' operator 'second'$/
33+
);

0 commit comments

Comments
 (0)