From 8b82c856f926ac90e473ae1e0c445fec772b0df6 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 17 Jun 2019 16:35:10 +0200 Subject: [PATCH] doc: update assert.throws() examples This updates two outdated examples to the current implementation. PR-URL: https://github.com/nodejs/node/pull/28263 Reviewed-By: Rich Trott Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- doc/api/assert.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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