Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit abe5d05

Browse filesBrowse 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
Copy full SHA for abe5d05

File tree

Expand file treeCollapse file tree

1 file changed

+8
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+8
-4
lines changed
Open diff view settings
Collapse file

‎doc/api/assert.md‎

Copy file name to clipboardExpand all lines: doc/api/assert.md
+8-4Lines changed: 8 additions & 4 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.