diff --git a/doc/api/assert.md b/doc/api/assert.md index db222580c30235..06eb57bad48af0 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -1222,6 +1222,9 @@ assert.throws( Custom error validation: +The function must return `true` to indicate all internal validations passed. +It will otherwise fail with an AssertionError. + ```js assert.throws( () => { diff --git a/lib/assert.js b/lib/assert.js index 1beb33265d6cd3..3d54dcd63fa040 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -653,7 +653,21 @@ function expectedException(actual, expected, message, fn) { // Check validation functions return value. const res = expected.call({}, actual); if (res !== true) { - throw actual; + if (!message) { + generatedMessage = true; + const name = expected.name ? `"${expected.name}" ` : ''; + message = `The ${name}validation function is expected to return "true".` + + ` Received ${inspect(res)}`; + } + const err = new AssertionError({ + actual, + expected, + message, + operator: fn.name, + stackStartFn: fn + }); + err.generatedMessage = generatedMessage; + throw err; } } diff --git a/test/parallel/test-assert-async.js b/test/parallel/test-assert-async.js index 8aad1d865c097e..45447d456379e1 100644 --- a/test/parallel/test-assert-async.js +++ b/test/parallel/test-assert-async.js @@ -66,6 +66,21 @@ const invalidThenableFunc = () => { code: 'ERR_INVALID_RETURN_VALUE' }) ); + + const err = new Error('foobar'); + const validate = () => { return 'baz'; }; + promises.push(assert.rejects( + () => assert.rejects(Promise.reject(err), validate), + { + message: 'The "validate" validation function is expected to ' + + "return \"true\". Received 'baz'", + code: 'ERR_ASSERTION', + actual: err, + expected: validate, + name: 'AssertionError', + operator: 'rejects', + } + )); } { diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 13f9d6427b01e6..9c59227a78492e 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -1326,3 +1326,20 @@ assert.throws( 'Received "[Array]"' } ); + +{ + const err = new TypeError('foo'); + const validate = (() => () => ({ a: true, b: [ 1, 2, 3 ] }))(); + assert.throws( + () => assert.throws(() => { throw err; }, validate), + { + message: 'The validation function is expected to ' + + `return "true". Received ${inspect(validate())}`, + code: 'ERR_ASSERTION', + actual: err, + expected: validate, + name: 'AssertionError', + operator: 'throws', + } + ); +}