Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
defined variables just before
they are used and added more
tests
  • Loading branch information
rahgurung committed Jul 9, 2019
1 parent 92dfbab commit 4fcc57f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions rules/no-error-ctor-with-notthrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}()\``
});
}
}
Expand Down
61 changes: 61 additions & 0 deletions test/no-error-ctor-with-notthrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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
}
]
});

0 comments on commit 4fcc57f

Please sign in to comment.