-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add no-error-ctor-with-notthrows
rule
#240
Changes from 7 commits
6604155
0c8b003
0a968d4
66a5e99
d754d0a
dcd4357
02a2c8f
eb5e7d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# No specifying error type in `t.notThrows()` | ||
|
||
AVA will fail if error constructor is specified in the second argument of `t.notThrows()`. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', t => { | ||
t.notThrows(() => { | ||
t.pass(); | ||
}, TypeError); | ||
}); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', t => { | ||
t.notThrows(() => { | ||
t.pass(); | ||
}); | ||
}); | ||
|
||
test('some test', t => { | ||
t.throws(() => { | ||
t.pass(); | ||
}, TypeError); | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,7 @@ | |
"ava": { | ||
"files": [ | ||
"!rules", | ||
"test/*js" | ||
"test/*js" | ||
] | ||
}, | ||
"xo": { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -44,6 +44,7 @@ Configure it in `package.json`. | |||||
"ava/no-async-fn-without-await": "error", | ||||||
"ava/no-cb-test": "off", | ||||||
"ava/no-duplicate-modifiers": "error", | ||||||
"ava/no-error-ctor-with-notthrows": "error", | ||||||
"ava/no-identical-title": "error", | ||||||
"ava/no-ignored-test-files": "error", | ||||||
"ava/no-import-test-files": "error", | ||||||
|
@@ -84,6 +85,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) - No specifying error type in `t.notThrows()`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
- [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. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
const {visitIf} = require('enhance-visitors'); | ||
const util = require('../util'); | ||
const createAvaRule = require('../create-ava-rule'); | ||
|
||
const create = context => { | ||
const ava = createAvaRule(); | ||
|
||
return ava.merge({ | ||
CallExpression: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode | ||
])(node => { | ||
const functionArgIndex = node.arguments.length - 1; | ||
rahgurung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (typeof node.callee.property === 'undefined' || functionArgIndex !== 1 || node.callee.type !== 'MemberExpression' || node.arguments[1].type !== 'Identifier' || util.getNameOfRootNodeObject(node.callee) !== 't') { | ||
return; | ||
} | ||
|
||
const calleeProperty = node.callee.property.name; | ||
rahgurung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const functionArgName = node.arguments[1].name; | ||
rahgurung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (calleeProperty === 'notThrows' || calleeProperty === 'notThrowsAsync') { | ||
if (functionArgName.endsWith('Error')) { | ||
context.report({ | ||
node, | ||
message: `Do not specify an error constructor in the second argument of \`t.${calleeProperty}()\`` | ||
}); | ||
} | ||
} | ||
}) | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
url: util.getDocsUrl(__filename) | ||
}, | ||
type: 'problem' | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,210 @@ | ||||||
import test from 'ava'; | ||||||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||||||
import rule from '../rules/no-error-ctor-with-notthrows'; | ||||||
|
||||||
const ruleTester = avaRuleTester(test, { | ||||||
env: { | ||||||
es6: true | ||||||
}, | ||||||
parserOptions: { | ||||||
ecmaVersion: 2019 | ||||||
} | ||||||
}); | ||||||
|
||||||
const errors = [{ruleId: 'no-error-ctor-with-notthrows'}]; | ||||||
|
||||||
const header = `const test = require('ava');\n`; // eslint-disable-line quotes | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
ruleTester.run('no-error-ctor-with-notthrows', rule, { | ||||||
valid: [ | ||||||
`${header} | ||||||
test('some test',t => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Could you fix this for all the other times ? |
||||||
t.notThrows(() => { | ||||||
t.pass(); | ||||||
}); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test(t => { | ||||||
t.notThrows(() => { | ||||||
t.pass(); | ||||||
}); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test(t => { | ||||||
t.throws(() => { | ||||||
t.pass(); | ||||||
}, TypeError); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test(t => { | ||||||
t.end(); })`, | ||||||
|
||||||
`${header} | ||||||
test('some test',t => { | ||||||
t.notThrows(() => { | ||||||
t.pass(); | ||||||
}, true); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test('some test',t => { | ||||||
t.notThrows(() => { | ||||||
t.pass(); | ||||||
}, 'some string'); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test('some test',t => { | ||||||
t.notThrows(() => { | ||||||
t.pass(); | ||||||
}, {firstName:'some', lastName: 'object'}); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test('some test',t => { | ||||||
t.notThrowsAsync(() => { | ||||||
t.pass(); | ||||||
}); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test(t => { | ||||||
t.notThrowsAsync(() => { | ||||||
t.pass(); | ||||||
}); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test('some test',t => { | ||||||
t.notThrowsAsync(() => { | ||||||
t.pass(); | ||||||
}, {firstName:'some', lastName: 'object'}); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test('some test',t => { | ||||||
notThrows(foo); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
test('some test',t => { | ||||||
myCustomNotThrows.notThrows(foo); | ||||||
});`, | ||||||
|
||||||
`${header} | ||||||
t.notThrows(() => { | ||||||
t.pass(); | ||||||
}, void 0);`, | ||||||
|
||||||
// Shouldn't be triggered since it's not a test file | ||||||
`test('some test',t => { | ||||||
t.notThrowsAsync(() => { | ||||||
t.pass(); | ||||||
}, TypeError); | ||||||
});` | ||||||
rahgurung marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
], | ||||||
invalid: [ | ||||||
{ | ||||||
code: `${header} | ||||||
test(t => { | ||||||
t.notThrows(() => { | ||||||
t.pass(); | ||||||
}, TypeError); | ||||||
});`, | ||||||
errors | ||||||
}, | ||||||
{ | ||||||
code: `${header} | ||||||
test('some test',t => { | ||||||
t.notThrows(() => { | ||||||
t.pass(); | ||||||
}, TypeError); | ||||||
});`, | ||||||
errors | ||||||
}, | ||||||
{ | ||||||
code: `${header} | ||||||
test(t => { | ||||||
t.notThrowsAsync(() => { | ||||||
t.pass(); | ||||||
}, TypeError); | ||||||
});`, | ||||||
errors | ||||||
}, | ||||||
{ | ||||||
code: `${header} | ||||||
test('some test',t => { | ||||||
t.notThrowsAsync(() => { | ||||||
t.pass(); | ||||||
}, TypeError); | ||||||
rahgurung marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
});`, | ||||||
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 | ||||||
}, | ||||||
{ | ||||||
code: `${header} | ||||||
test('some test',t => { | ||||||
t.notThrowsAsync(() => { | ||||||
t.pass(); | ||||||
}, $DOMError); | ||||||
});`, | ||||||
errors | ||||||
} | ||||||
] | ||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this ?