Skip to content

Commit abe5d05

Browse files
committed
doc: update assert's validation functions
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>
1 parent f03241f commit abe5d05

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

doc/api/assert.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,10 +1210,14 @@ assert.throws(
12101210
() => {
12111211
throw new Error('Wrong value');
12121212
},
1213-
function(err) {
1214-
if ((err instanceof Error) && /value/.test(err)) {
1215-
return true;
1216-
}
1213+
(err) => {
1214+
assert(err instanceof Error);
1215+
assert(/value/.test(err));
1216+
// Returning anything from validation functions besides `true` is not
1217+
// recommended. Doing so results in the caught error being thrown again.
1218+
// That is usually not the desired outcome. Throw an error about the
1219+
// specific validation that failed instead (as done in this example).
1220+
return true;
12171221
},
12181222
'unexpected error'
12191223
);

0 commit comments

Comments
 (0)