-
Notifications
You must be signed in to change notification settings - Fork 237
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
feat(rules): add no-commented-out rule #262
Conversation
src/rules/no-commented-tests.js
Outdated
const message = 'Some tests seem to be commented'; | ||
|
||
function hasAssertions(node) { | ||
return /x?(test|it|describe)((\.skip|\[['"]skip['"]\]))?\(/.test(node.value); |
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.
I didn't bother with matching quotes here. I think this will be rare enough and the rule is fuzzy as it is.
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.
Awesome @kangax, I love that you added extensive documentation and many tests right away 👍 I left a few review comments :)
Also, I'm not quite sure about the rule name - a "commented test" could be misunderstood as
test('stuff', () => {
// check that x does y
expect(whatever).toBe(something)
})
no-commented-out-tests
is clearer, but sounds a bit weird. Or maybe you have a different idea?
I agree that |
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.
Two more comments on cases that came to my mind looking at the regex, other than that LGTM :)
@SimenB also want to review maybe?
src/rules/no-commented-out-tests.js
Outdated
const message = 'Some tests seem to be commented'; | ||
|
||
function hasTests(node) { | ||
return /x?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\(.*?\)/.test(node.value); |
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.
- Other than
xit
, there's alsofit
- The closing parenthesis might cause trouble with multi-line tests in comments, we might need the
//s
regex modifier? Should add some multiline test cases
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.
Addressed both comments and added tests. The thing about newlines is that ESLint seems to create a separate comment for each line with line-level comments. We can't detect stuff like:
// test
// (
// 'x
// )
..which, I think, is an OK limitation?
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.
For block comments as well though?
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.
Actually, best thing might be to just not look for the closing parenthesis? // test(
already looks suspicious enough
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.
Yep, I did just that in the last commit. Let me add a multiline comment test as well.
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.
LGTM, thanks! Would be nice to address @jeysal's comments about the regexp and my little nit
src/index.js
Outdated
@@ -38,6 +38,7 @@ module.exports = { | |||
rules: { | |||
'jest/no-alias-methods': 'warn', | |||
'jest/no-disabled-tests': 'warn', | |||
'jest/no-commented-out-tests': 'warn', |
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.
changing the recommended config is a breaking change.
Not sure this needs to be in the recommended config, at all, but we can revisit that before the next release 🙂
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.
Ah, I didn't realise this will modify recommended config. On the other hand, I think it's ok to warn since warnings usually don't fail with tools like lint-staged, etc.
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.
People can run eslint with a flag failing on warnings
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.
removed it
@SimenB anything else you want me to do here? |
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.
Thanks @kangax ❤️
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.
Thanks!
🎉 This PR is included in version 22.6.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Hi @kangax! Here I am again... 😊 I ran this rule on our codebase just like I did here and I found some cases where this rule reports false positives. You may want to check it so people don't have to disable this rule by default just like we did. All these cases are reporting issues incorrectly (+ none of them is a test file):
... And also this one - reports issue on line 8 incorrectly. Cheers 🍻 |
Closes #261