Skip to content

Commit

Permalink
doc: update assert's validation functions
Browse files Browse the repository at this point in the history
Using `assert.throws()` or `assert.rejects()` in combination with
validation function can be very powerful. Using them by returning
any value besides `true` makes debugging very difficult though
because there's no context or reason why the error validation failed.
Thus, it is recommended to throw an error in such cases instead of
returning anything besides `true`.

PR-URL: #27781
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR committed Jun 17, 2019
1 parent f03241f commit abe5d05
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -1210,10 +1210,14 @@ assert.throws(
() => {
throw new Error('Wrong value');
},
function(err) {
if ((err instanceof Error) && /value/.test(err)) {
return true;
}
(err) => {
assert(err instanceof Error);
assert(/value/.test(err));
// Returning anything from validation functions besides `true` is not
// recommended. Doing so results in the caught error being thrown again.
// That is usually not the desired outcome. Throw an error about the
// specific validation that failed instead (as done in this example).
return true;
},
'unexpected error'
);
Expand Down

0 comments on commit abe5d05

Please sign in to comment.