-
Notifications
You must be signed in to change notification settings - Fork 18
Conversation
Should we nest this rule inside of a "jest" folder (like with "webpack")? |
So much potential 😍 Some suggestions:
The double-negatives thing is maybe its own rule, but it could reuse code from this one. Mocha has a similar rule: https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/valid-test-description.md#rule-details. |
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.
Like Gord's suggestion of expanding this to be more of a "no-vague-titles" rule. Also all for nesting under jest/
const ignoredFunctionNames = ignore.reduce((accumulator, value) => { | ||
accumulator[value] = true; | ||
return accumulator; | ||
}, Object.create(null)); |
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.
Probably better as a map, a little easier to construct too
); | ||
} | ||
|
||
function isItDescription(node) { |
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.
This function name is confusing to me
function descriptionContainsCorrect(node) { | ||
if (isItTestOrDescribeFunction(node) && isItDescription(node)) { | ||
const description = testDescription(node); | ||
if (!description[0]) { |
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.
Checking if empty string?
} | ||
|
||
if (description.match(/correct/gi)) { | ||
return node.callee.name; |
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 return the name?
CallExpression(node) { | ||
const erroneousMethod = descriptionContainsCorrect(node); | ||
|
||
if (erroneousMethod && !isIgnoredFunctionName(node)) { |
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.
Seems like a weird flow to me — I'd expect it to check if this is one of the functions we expect, removing ignored ones, then check if the description contains invalid words, then report.
}, | ||
}; | ||
|
||
function isItTestOrDescribeFunction(node) { |
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.
Probably good to catch the skipped versions (fit
/ it.only
and the same for the other kinds)
cf6a7d0
to
c64fd75
Compare
51ece6e
to
30facbd
Compare
@lemonmade @GoodForOneFare thanks for the feedback! I have addressed everything and think this is ready for another look. |
docs/rules/jest/no-vague-titles.md
Outdated
@@ -0,0 +1,53 @@ | |||
# Prevent the usage of vague words in test statements.(no-vague-titles) | |||
This rule prevents the use of the words "correct" and "appropriate" in test descriptions. |
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.
Make this a little more general
docs/rules/jest/no-vague-titles.md
Outdated
@@ -0,0 +1,53 @@ | |||
# Prevent the usage of vague words in test statements.(no-vague-titles) |
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.
Space after period
docs/rules/jest/no-vague-titles.md
Outdated
|
||
## Rule Details | ||
|
||
Using vague words to convey the valid test path within `it`, `describe` or `test` descriptions often fails to communicate the meaningful details of what the underlying code is meant to do. Enabling this rule will encourage developers to write more specific and readable test descriptions. |
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.
Not sure what to convey the valid test path within
means?
docs/rules/jest/no-vague-titles.md
Outdated
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
|
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 do you leave empty lines in these?
lib/rules/jest/no-vague-titles.js
Outdated
function validate(node) { | ||
if ( | ||
!isTestFunction(node) || | ||
isIgnoredFunctionName(node) || |
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 would probably flip this condition so they are all either positive or negative
lib/rules/jest/no-vague-titles.js
Outdated
return firstArgument.value; | ||
} | ||
|
||
return firstArgument.quasis[0].value.raw; |
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.
This is a little odd, might want to merge together all the quasis that you can to come up with a complete description
lib/rules/jest/no-vague-titles.js
Outdated
const type = firstArgument.type; | ||
|
||
if (type === 'Literal') { | ||
return firstArgument.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.
What if it's not a string literal?
lib/rules/jest/no-vague-titles.js
Outdated
} | ||
|
||
function containsVagueWord(description) { | ||
return description.match(/correct/giu) || description.match(/appropriate/giu); |
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.
No need for g
, we also don't usually bother with u
parser, | ||
}, | ||
{ | ||
code: `test('foo bar baz')`, |
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.
You should also test non-test functions called with an argument that contains the bad words
errors: errorWithMethod('describe'), | ||
}, | ||
{ | ||
code: "describe('correct')", |
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 would ignore for now, but you might be interested in tackling the new describe/it/test.each
Jest recently introduced: https://facebook.github.io/jest/docs/en/api.html#describeeachtable-name-fn
77c1be6
to
242798e
Compare
Thanks again @lemonmade! Last round should all be addressed, let me know if there is anything else or good to merge. |
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.
Still think you can skip the member expression check.
lib/rules/jest/no-vague-titles.js
Outdated
} | ||
|
||
return { | ||
MemberExpression(node) { |
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.
But validate
always works for call expressions, regardless of whether the callee is just an identifier or a member expression. You end up basically validating every member expression twice (once at the call expression, once at the member expression)
lib/rules/jest/no-vague-titles.js
Outdated
} | ||
|
||
function getDescription(node) { | ||
const args = node.arguments || node.parent.arguments || []; |
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.
This is only needed because you are unnecessarily validating the member expression
fa6bca2
to
66e676d
Compare
review feedback docs minor tweaks more review fixes Removing MemberExpression validation
66e676d
to
0827edc
Compare
Addresses one of the points here #77 by testing the
it
,describe
andtest
statements against a simple regex that looks for the word "correct". Will fill in the docs once the general approach is approved.