eslint-plugin-jest icon indicating copy to clipboard operation
eslint-plugin-jest copied to clipboard

[New option] [valid-title] - Allow types that yield string.

Open ColtHands opened this issue 3 years ago • 2 comments

Right now there is ability to { ignoreTypeOfDescribeName: true } that ignores any type whether its a number or a function.

How about we have a rule that restricts everything that does not yield type typeof {title} === 'string'. Or allows only the things that yield a string title.

Why?

Because a lot of codebases use action_types, enums, consts, etc.

Right now:

// "jest/valid-title": ["error", { ignoreTypeOfDescribeName: true }]

it.todo(123) // is good
// "jest/valid-title": ["error", { ignoreTypeOfDescribeName: false }]

const SOME_ACTION_TYPE = "SOME_ACTION_TYPE"
it.todo(SOME_ACTION_TYPE) // error

enum Direction = {
  Up = "Up"
}
describe(Direction.Up, () => {...}) // error

ColtHands avatar Sep 05 '22 11:09 ColtHands

Also allow ignoreTypeOfDescribeName to be applied to the it or test keywords. Right now if jest/valid-title: ["error", { ignoreTypeOfDescribeName: true }], rule is on


const title = "should yield true"

it(title, () => {...}) // error

will error out

ColtHands avatar Sep 05 '22 12:09 ColtHands

I'm pretty lukewarm on this - tbh I'm not really sure having ignoreTypeOfDescribeName was a good idea.

Titles have two purposes:

  1. explain what is being tested
  2. make it easy to track down where a specific test lives (i.e by being able to search for the title)

By not using a string literal, you halve the effectiveness of having the title since it's a lot harder to find tests by their name alone. Meanwhile, you can already control where this rule is applied both via eslint config and inline disables which imo is better as its more explicit that you intended to use this pattern.

As such, I don't think its worth the extra complexity to use typechecking in this rule, but because we already have ignoreTypeOfDescribeName its probably fine to have a similar option for tests themselves.

G-Rath avatar Sep 05 '22 19:09 G-Rath

:tada: This issue has been resolved in version 27.5.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Oct 26 '23 18:10 github-actions[bot]