diff options
author | Rich Trott <rtrott@gmail.com> | 2017-04-09 10:36:14 -0700 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2017-04-12 14:25:22 -0700 |
commit | 758b8b6e5d1aa171827759e51847bcc4f2eea7a3 (patch) | |
tree | 2b362d102ce63b36f7fd9e1f8bb43df410d669e0 /test/parallel/test-assert-fail.js | |
parent | b3f2e3b7e2793e1f74cd296e85a549d926054225 (diff) | |
download | node-new-758b8b6e5d1aa171827759e51847bcc4f2eea7a3.tar.gz |
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: https://github.com/nodejs/node/pull/12293
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-assert-fail.js')
-rw-r--r-- | test/parallel/test-assert-fail.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/parallel/test-assert-fail.js b/test/parallel/test-assert-fail.js new file mode 100644 index 0000000000..5a321c6bb3 --- /dev/null +++ b/test/parallel/test-assert-fail.js @@ -0,0 +1,33 @@ +'use strict'; +require('../common'); +const assert = require('assert'); + +// no args +assert.throws( + () => { assert.fail(); }, + /^AssertionError: undefined undefined undefined$/ +); + +// one arg = message +assert.throws( + () => { assert.fail('custom message'); }, + /^AssertionError: custom message$/ +); + +// two args only, operator defaults to '!=' +assert.throws( + () => { assert.fail('first', 'second'); }, + /^AssertionError: 'first' != 'second'$/ +); + +// three args +assert.throws( + () => { assert.fail('ignored', 'ignored', 'another custom message'); }, + /^AssertionError: another custom message$/ +); + +// no third arg (but a fourth arg) +assert.throws( + () => { assert.fail('first', 'second', undefined, 'operator'); }, + /^AssertionError: 'first' operator 'second'$/ +); |