From 4fcc57f945b8f1928fccd7d5969b7094f499aa50 Mon Sep 17 00:00:00 2001 From: Rahul Gurung Date: Tue, 9 Jul 2019 18:24:40 +0530 Subject: [PATCH] feedback defined variables just before they are used and added more tests --- readme.md | 2 +- rules/no-error-ctor-with-notthrows.js | 8 ++-- test/no-error-ctor-with-notthrows.js | 61 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 11e47e4d..ae5ee9d5 100644 --- a/readme.md +++ b/readme.md @@ -80,7 +80,7 @@ The rules will only activate in test files. - [no-async-fn-without-await](docs/rules/no-async-fn-without-await.md) - Ensure that async tests use `await`. - [no-cb-test](docs/rules/no-cb-test.md) - Ensure no `test.cb()` is used. - [no-duplicate-modifiers](docs/rules/no-duplicate-modifiers.md) - Ensure tests do not have duplicate modifiers. -- [no-error-ctor-with-notthrows](docs/rules/no-error-ctor-with-notthrows.md) - Prevent use of typerror with notthrows. +- [no-error-ctor-with-notthrows](docs/rules/no-error-ctor-with-notthrows.md) - No specifying error type in `t.notThrows()`. - [no-identical-title](docs/rules/no-identical-title.md) - Ensure no tests have the same title. - [no-ignored-test-files](docs/rules/no-ignored-test-files.md) - Ensure no tests are written in ignored files. - [no-import-test-files](docs/rules/no-import-test-files.md) - Ensure no test files are imported anywhere. diff --git a/rules/no-error-ctor-with-notthrows.js b/rules/no-error-ctor-with-notthrows.js index 976d1660..2bbf7d85 100644 --- a/rules/no-error-ctor-with-notthrows.js +++ b/rules/no-error-ctor-with-notthrows.js @@ -3,7 +3,7 @@ const {visitIf} = require('enhance-visitors'); const util = require('../util'); const createAvaRule = require('../create-ava-rule'); -const errorNameRegex = /^(?:[A-Z][a-z\d]*)*Error$/; +const errorNameRegex = /^([A-Z][a-z\d]*)*Error$/; const create = context => { const ava = createAvaRule(); @@ -13,25 +13,25 @@ const create = context => { ava.isInTestFile, ava.isInTestNode ])(node => { - const functionArgIndex = node.arguments.length - 1; if (typeof node.callee.property === 'undefined') { return; } - const calleeProperty = node.callee.property.name; + const functionArgIndex = node.arguments.length - 1; if (functionArgIndex !== 1) { return; } + const calleeProperty = node.callee.property.name; const functionArgName = node.arguments[1].name; if (calleeProperty === 'notThrows' || calleeProperty === 'notThrowsAsync') { if (errorNameRegex.test(functionArgName)) { context.report({ node, - message: 'Do not specify an error constructor in the second argument of t.notThrows()' + message: `Do not specify an error constructor in the second argument of \`t.${calleeProperty}()\`` }); } } diff --git a/test/no-error-ctor-with-notthrows.js b/test/no-error-ctor-with-notthrows.js index 3cb2421b..4469458b 100644 --- a/test/no-error-ctor-with-notthrows.js +++ b/test/no-error-ctor-with-notthrows.js @@ -79,6 +79,13 @@ ruleTester.run('no-error-ctor-with-notthrows', rule, { t.notThrowsAsync(() => { t.pass(); }, {firstName:'some', lastName: 'object'}); + });`, + + // Shouldn't be triggered since it's not a test file + `test('some test',t => { + t.notThrowsAsync(() => { + t.pass(); + }, TypeError); });` ], invalid: [ @@ -117,6 +124,60 @@ ruleTester.run('no-error-ctor-with-notthrows', rule, { }, TypeError); });`, errors + }, + { + code: `${header} + test('some test',t => { + t.notThrowsAsync(() => { + t.pass(); + }, Error); + });`, + errors + }, + { + code: `${header} + test('some test',t => { + t.notThrowsAsync(() => { + t.pass(); + }, SyntaxError); + });`, + errors + }, + { + code: `${header} + test('some test',t => { + t.notThrowsAsync(() => { + t.pass(); + }, AssertionError); + });`, + errors + }, + { + code: `${header} + test('some test',t => { + t.notThrowsAsync(() => { + t.pass(); + }, ReferenceError); + });`, + errors + }, + { + code: `${header} + test('some test',t => { + t.notThrowsAsync(() => { + t.pass(); + }, RangeError); + });`, + errors + }, + { + code: `${header} + test('some test',t => { + t.notThrowsAsync(() => { + t.pass(); + }, SystemError); + });`, + errors } ] });