mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
doc: update assert.throws() examples
This updates two outdated examples to the current implementation. Backport-PR-URL: https://github.com/nodejs/node/pull/31431 PR-URL: https://github.com/nodejs/node/pull/28263 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
7d6c963b9d
commit
08b5a2fcb4
1 file changed
+9
-6
+9
-6
@@ -1192,10 +1192,15 @@ assert.throws(
|
||||
assert.throws(
|
||||
() => {
|
||||
const otherErr = new Error('Not found');
|
||||
otherErr.code = 404;
|
||||
// Copy all enumerable properties from `err` to `otherErr`.
|
||||
for (const [key, value] of Object.entries(err)) {
|
||||
otherErr[key] = value;
|
||||
}
|
||||
throw otherErr;
|
||||
},
|
||||
err // This tests for `message`, `name` and `code`.
|
||||
// The error's `message` and `name` properties will also be checked when using
|
||||
// an error as validation object.
|
||||
err
|
||||
);
|
||||
```
|
||||
|
||||
@@ -1282,11 +1287,9 @@ assert.throws(notThrowing, 'Second');
|
||||
// It does not throw because the error messages match.
|
||||
assert.throws(throwingSecond, /Second$/);
|
||||
|
||||
// If the error message does not match, the error from within the function is
|
||||
// not caught.
|
||||
// If the error message does not match, an AssertionError is thrown.
|
||||
assert.throws(throwingFirst, /Second$/);
|
||||
// Error: First
|
||||
// at throwingFirst (repl:2:9)
|
||||
// AssertionError [ERR_ASSERTION]
|
||||
```
|
||||
|
||||
Due to the confusing error-prone notation, avoid a string as the second
|
||||
|
||||
Reference in new issue
Block a user