From f6247a945ca8c41ba8738883d948282f36b9671c Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 4 May 2017 22:09:09 -0700 Subject: [PATCH] assert: restore TypeError if no arguments In Node 7.x, calling `throw new assert.AssertionError()` resulted in a TypeError. In current master, the same call does not result in an error but, due to the default option, it results in uninformative output ("undefined undefined undefined"). This change removes the default argument, restoring a TypeError if there is no argument. This also will restore our test coverage to 100%. (The default argument is not tested in our current test suite.) PR-URL: https://github.com/nodejs/node/pull/12843 Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca Reviewed-By: Joyee Cheung Reviewed-By: Refael Ackermann Reviewed-By: James M Snell --- lib/assert.js | 2 +- test/parallel/test-assert.js | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index a1631ff2e6f428..95a3b1d565f81b 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -47,7 +47,7 @@ const assert = module.exports = ok; // TODO(jasnell): Consider moving AssertionError into internal/errors.js class AssertionError extends Error { - constructor(options = {}) { + constructor(options) { if (typeof options !== 'object' || options === null) { // Lazy because the errors module itself uses assertions, leading to // a circular dependency. This can be eliminated by moving this class diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index ecbe9ce79a20ff..4489773a40ea1b 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -707,12 +707,16 @@ assert.throws(() => { code: 'ERR_ASSERTION', message: new RegExp(`^'${'A'.repeat(127)} === ''$`)})); -[1, true, false, '', null, Infinity, Symbol('test')].forEach((input) => { - assert.throws( - () => new assert.AssertionError(input), - common.expectsError({ - code: 'ERR_INVALID_ARG_TYPE', - type: TypeError, - message: /^The "options" argument must be of type object$/ - })); -}); +{ + // bad args to AssertionError constructor should throw TypeError + const args = [1, true, false, '', null, Infinity, Symbol('test'), undefined]; + args.forEach((input) => { + assert.throws( + () => new assert.AssertionError(input), + common.expectsError({ + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: /^The "options" argument must be of type object$/ + })); + }); +}