diff --git a/doc/api/assert.md b/doc/api/assert.md index eaf5c5447da6db..c96516855cee8b 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -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