Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Add no-vague-titles rule #93

Merged
merged 1 commit into from
Jun 24, 2018
Merged

Add no-vague-titles rule #93

merged 1 commit into from
Jun 24, 2018

Conversation

cartogram
Copy link
Contributor

@cartogram cartogram commented May 20, 2018

Addresses one of the points here #77 by testing the it, describe and test statements against a simple regex that looks for the word "correct". Will fill in the docs once the general approach is approved.

@cartogram cartogram requested a review from lemonmade May 20, 2018 05:50
@cartogram
Copy link
Contributor Author

Should we nest this rule inside of a "jest" folder (like with "webpack")?

@GoodForOneFare
Copy link
Member

GoodForOneFare commented May 20, 2018

So much potential 😍 Some suggestions:

  • Rename to be more general (e.g., no-weasel-words, no-ambiguous-titles, no-vague-titles)
  • Forbid appropriate
  • Forbid double-negatives
  • Submit this to eslint-plugin-jest

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.

Copy link
Member

@lemonmade lemonmade left a 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));
Copy link
Member

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) {
Copy link
Member

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]) {
Copy link
Member

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;
Copy link
Member

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)) {
Copy link
Member

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) {
Copy link
Member

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)

@cartogram cartogram force-pushed the no-tests-contain-correct branch 2 times, most recently from cf6a7d0 to c64fd75 Compare May 23, 2018 03:45
@cartogram cartogram changed the title Add no-tests-contain-correct rule Add no-vague-titles rule May 27, 2018
@cartogram cartogram force-pushed the no-tests-contain-correct branch 3 times, most recently from 51ece6e to 30facbd Compare May 27, 2018 20:26
@cartogram
Copy link
Contributor Author

cartogram commented May 27, 2018

@lemonmade @GoodForOneFare thanks for the feedback! I have addressed everything and think this is ready for another look.

@@ -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.
Copy link
Member

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

@@ -0,0 +1,53 @@
# Prevent the usage of vague words in test statements.(no-vague-titles)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space after period


## 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.
Copy link
Member

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?

Examples of **incorrect** code for this rule:

```js

Copy link
Member

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?

function validate(node) {
if (
!isTestFunction(node) ||
isIgnoredFunctionName(node) ||
Copy link
Member

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

return firstArgument.value;
}

return firstArgument.quasis[0].value.raw;
Copy link
Member

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

const type = firstArgument.type;

if (type === 'Literal') {
return firstArgument.value;
Copy link
Member

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?

}

function containsVagueWord(description) {
return description.match(/correct/giu) || description.match(/appropriate/giu);
Copy link
Member

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')`,
Copy link
Member

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')",
Copy link
Member

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

@cartogram cartogram force-pushed the no-tests-contain-correct branch 7 times, most recently from 77c1be6 to 242798e Compare June 4, 2018 03:58
@cartogram
Copy link
Contributor Author

Thanks again @lemonmade! Last round should all be addressed, let me know if there is anything else or good to merge.

Copy link
Member

@lemonmade lemonmade left a 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.

}

return {
MemberExpression(node) {
Copy link
Member

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)

}

function getDescription(node) {
const args = node.arguments || node.parent.arguments || [];
Copy link
Member

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

@cartogram cartogram force-pushed the no-tests-contain-correct branch 3 times, most recently from fa6bca2 to 66e676d Compare June 16, 2018 17:36
review feedback

docs

minor tweaks

more review fixes

Removing MemberExpression validation
@cartogram cartogram merged commit ef53ab1 into master Jun 24, 2018
@cartogram cartogram deleted the no-tests-contain-correct branch June 24, 2018 20:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants