From fc463639fa38434a3360a1e3695d7ded029242c3 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 28 Jun 2017 19:57:02 -0400 Subject: [PATCH] assert: fix assert.fail with zero arguments PR-URL: https://github.com/nodejs/node/pull/13974 Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- doc/api/assert.md | 8 ++++++-- lib/assert.js | 7 ++++++- test/parallel/test-assert-fail.js | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index 1a040a927bc7f9..502150fd8dc9c8 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -256,19 +256,20 @@ If the values are not equal, an `AssertionError` is thrown with a `message` property set equal to the value of the `message` parameter. If the `message` parameter is undefined, a default error message is assigned. -## assert.fail(message) +## assert.fail([message]) ## assert.fail(actual, expected, message, operator) * `actual` {any} * `expected` {any} -* `message` {any} +* `message` {any} (default: 'Failed') * `operator` {string} (default: '!=') Throws an `AssertionError`. If `message` is falsy, the error message is set as the values of `actual` and `expected` separated by the provided `operator`. Otherwise, the error message is the value of `message`. +If no arguments are provided at all, a default message will be used instead. ```js const assert = require('assert'); @@ -284,6 +285,9 @@ assert.fail('boom'); assert.fail('a', 'b'); // AssertionError: 'a' != 'b' + +assert.fail(); +// AssertionError: Failed ``` ## assert.ifError(value) diff --git a/lib/assert.js b/lib/assert.js index bd36c9a16d1b38..ca7c28154a93a9 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -52,8 +52,13 @@ const assert = module.exports = ok; // display purposes. function fail(actual, expected, message, operator, stackStartFunction) { - if (arguments.length === 1) + if (arguments.length === 0) { + message = 'Failed'; + } + if (arguments.length === 1) { message = actual; + actual = undefined; + } if (arguments.length === 2) operator = '!='; const errors = lazyErrors(); diff --git a/test/parallel/test-assert-fail.js b/test/parallel/test-assert-fail.js index 1f389933bb4a9c..1b72310a169933 100644 --- a/test/parallel/test-assert-fail.js +++ b/test/parallel/test-assert-fail.js @@ -8,7 +8,7 @@ assert.throws( common.expectsError({ code: 'ERR_ASSERTION', type: assert.AssertionError, - message: 'undefined undefined undefined' + message: 'Failed' }) );