diff --git a/doc/api/assert.md b/doc/api/assert.md index 0a7d35f105e48e..ffebe4d50cd57f 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -633,13 +633,12 @@ changes: > functions instead. If `message` is falsy, the error message is set as the values of `actual` and -`expected` separated by the provided `operator`. If just the two `actual` and -`expected` arguments are provided, `operator` will default to `'!='`. If -`message` is provided as third argument it will be used as the error message and -the other arguments will be stored as properties on the thrown object. If -`stackStartFn` is provided, all stack frames above that function will be -removed from stacktrace (see [`Error.captureStackTrace`]). If no arguments are -given, the default message `Failed` will be used. +`expected` separated by the provided `operator`. If `operator` is falsy and no +message is set, it will default to `'!='`. If `message` is truthy, the `operator` +will default to `'fail'` and `message` will be used for the error message. Other +arguments will be stored as properties on the thrown object. If `stackStartFn` +is provided, all stack frames above that function will be removed from +stacktrace (see [`Error.captureStackTrace`][]). ```js const assert = require('assert').strict; diff --git a/lib/assert.js b/lib/assert.js index 6e0b850b40c6a7..2e1e8b056554e8 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -68,6 +68,17 @@ const meta = [ const escapeFn = (str) => meta[str.charCodeAt(0)]; let warned = false; +function showMulParamDeprecation() { + if (warned === false) { + warned = true; + process.emitWarning( + 'assert.fail() with more than one argument is deprecated. ' + + 'Please use assert.strictEqual() instead or only pass a message.', + 'DeprecationWarning', + 'DEP0094' + ); + } +} // The assert module provides functions that throw // AssertionError's when particular conditions are not met. The @@ -100,25 +111,23 @@ function fail(actual, expected, message, operator, stackStartFn) { message = actual; actual = undefined; } else { - if (warned === false) { - warned = true; - process.emitWarning( - 'assert.fail() with more than one argument is deprecated. ' + - 'Please use assert.strictEqual() instead or only pass a message.', - 'DeprecationWarning', - 'DEP0094' - ); - } - if (argsLen === 2) - operator = '!='; + showMulParamDeprecation(); } if (message instanceof Error) throw message; + if (operator === undefined) { + if (message) { + operator = 'fail'; + } else { + operator = '!='; + } + } + const errArgs = { actual, expected, - operator: operator === undefined ? 'fail' : operator, + operator, stackStartFn: stackStartFn || fail, message }; diff --git a/test/parallel/test-assert-fail-deprecation.js b/test/parallel/test-assert-fail-deprecation.js index 5f51d27e3cfc0f..8e68b5f0d94176 100644 --- a/test/parallel/test-assert-fail-deprecation.js +++ b/test/parallel/test-assert-fail-deprecation.js @@ -61,3 +61,16 @@ assert.throws( function foo() { assert.fail('first', 'second', 'message', '!==', foo); }, (err) => !/^\s*at\sfoo\b/m.test(err.stack) ); + +// Actual undefined = Error +assert.throws(() => { + assert.fail(undefined); +}, { + code: 'ERR_ASSERTION', + name: 'AssertionError', + message: 'Failed', + operator: 'fail', + actual: undefined, + expected: undefined, + generatedMessage: true +});