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 6d8a541

Browse filesBrowse files
BridgeARMylesBorins
authored andcommitted
doc: update assert.throws() examples
This updates two outdated examples to the current implementation. Backport-PR-URL: #31431 PR-URL: #28263 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 881ebce commit 6d8a541
Copy full SHA for 6d8a541

File tree

Expand file treeCollapse file tree

1 file changed

+9
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+9
-6
lines changed
Open diff view settings
Collapse file

‎doc/api/assert.md‎

Copy file name to clipboardExpand all lines: doc/api/assert.md
+9-6Lines changed: 9 additions & 6 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1192,10 +1192,15 @@ assert.throws(
11921192
assert.throws(
11931193
() => {
11941194
const otherErr = new Error('Not found');
1195-
otherErr.code = 404;
1195+
// Copy all enumerable properties from `err` to `otherErr`.
1196+
for (const [key, value] of Object.entries(err)) {
1197+
otherErr[key] = value;
1198+
}
11961199
throw otherErr;
11971200
},
1198-
err // This tests for `message`, `name` and `code`.
1201+
// The error's `message` and `name` properties will also be checked when using
1202+
// an error as validation object.
1203+
err
11991204
);
12001205
```
12011206

@@ -1282,11 +1287,9 @@ assert.throws(notThrowing, 'Second');
12821287
// It does not throw because the error messages match.
12831288
assert.throws(throwingSecond, /Second$/);
12841289

1285-
// If the error message does not match, the error from within the function is
1286-
// not caught.
1290+
// If the error message does not match, an AssertionError is thrown.
12871291
assert.throws(throwingFirst, /Second$/);
1288-
// Error: First
1289-
// at throwingFirst (repl:2:9)
1292+
// AssertionError [ERR_ASSERTION]
12901293
```
12911294

12921295
Due to the confusing error-prone notation, avoid a string as the second

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.