diff --git a/.eslintrc.yaml b/.eslintrc.yaml index e73e97dede44af..377f615e8a1d20 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -135,8 +135,11 @@ rules: no-mixed-spaces-and-tabs: error no-multiple-empty-lines: [error, {max: 2, maxEOF: 0, maxBOF: 0}] no-restricted-syntax: [error, { + selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']", + message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead." + }, { selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])", - message: "use a regular expression for second argument of assert.throws()" + message: "Use a regular expression for second argument of assert.throws()" }, { selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]", message: "assert.throws() must be invoked with at least two arguments." diff --git a/benchmark/assert/throws.js b/benchmark/assert/throws.js index bffde7cbc1fd94..2409d19206e353 100644 --- a/benchmark/assert/throws.js +++ b/benchmark/assert/throws.js @@ -26,6 +26,7 @@ function main({ n, method }) { case 'doesNotThrow': bench.start(); for (i = 0; i < n; ++i) { + // eslint-disable-next-line no-restricted-syntax assert.doesNotThrow(doesNotThrow); } bench.end(n); diff --git a/doc/api/assert.md b/doc/api/assert.md index ca414e1c7cba47..cedbc14d427b9a 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -341,6 +341,7 @@ to the caller. The following, for instance, will throw the [`TypeError`][] because there is no matching error type in the assertion: + ```js assert.doesNotThrow( () => { @@ -353,6 +354,7 @@ assert.doesNotThrow( However, the following will result in an `AssertionError` with the message 'Got unwanted exception (TypeError)..': + ```js assert.doesNotThrow( () => { @@ -366,6 +368,7 @@ If an `AssertionError` is thrown and a value is provided for the `message` parameter, the value of `message` will be appended to the `AssertionError` message: + ```js assert.doesNotThrow( () => { diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 331ac63c47a062..c5a2bfe8561fdb 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -121,7 +121,7 @@ assert.throws(() => thrower(TypeError)); } common.expectsError( - () => assert.doesNotThrow(() => thrower(Error), 'user message'), + () => a.doesNotThrow(() => thrower(Error), 'user message'), { type: a.AssertionError, code: 'ERR_ASSERTION', @@ -131,7 +131,7 @@ common.expectsError( ); common.expectsError( - () => assert.doesNotThrow(() => thrower(Error), 'user message'), + () => a.doesNotThrow(() => thrower(Error), 'user message'), { code: 'ERR_ASSERTION', message: /Got unwanted exception: user message\n\[object Object\]/ @@ -139,7 +139,7 @@ common.expectsError( ); common.expectsError( - () => assert.doesNotThrow(() => thrower(Error)), + () => a.doesNotThrow(() => thrower(Error)), { code: 'ERR_ASSERTION', message: /Got unwanted exception\.\n\[object Object\]/ @@ -292,7 +292,7 @@ try { // Verify AssertionError is the result from doesNotThrow with custom Error. try { - assert.doesNotThrow(() => { + a.doesNotThrow(() => { throw new TypeError('wrong type'); }, TypeError, rangeError); } catch (e) { @@ -760,7 +760,6 @@ common.expectsError( errObj.code = '404'; common.expectsError( - // eslint-disable-next-line no-restricted-syntax () => assert.throws(errFn, errObj), { code: 'ERR_ASSERTION', @@ -772,7 +771,6 @@ common.expectsError( errObj.code = 404; errObj.foo = 'bar'; common.expectsError( - // eslint-disable-next-line no-restricted-syntax () => assert.throws(errFn, errObj), { code: 'ERR_ASSERTION', @@ -791,7 +789,7 @@ common.expectsError( ); common.expectsError( - () => assert.doesNotThrow(() => { throw new Error(); }, { foo: 'bar' }), + () => a.doesNotThrow(() => { throw new Error(); }, { foo: 'bar' }), { type: TypeError, code: 'ERR_INVALID_ARG_TYPE', @@ -822,7 +820,7 @@ common.expectsError( assert.throws(() => { throw undefined; }, /undefined/); common.expectsError( // eslint-disable-next-line no-throw-literal - () => assert.doesNotThrow(() => { throw undefined; }), + () => a.doesNotThrow(() => { throw undefined; }), { type: assert.AssertionError, code: 'ERR_ASSERTION',