Skip to content

Rule to enforce consistent data-testid format #55

@tknickman

Description

@tknickman

Hey, great plugin!

I recently wrote a custom rule that we are using internally at my company to enforce consistent values for data-testid. (I found this plugin because I wanted to use the same name 😂). We have a giant monorepo and teams were using everything from camelCase to PascalCase to kebab-case to snake_case to who-knows-what-case when writing test Id's.

This rule allows specifying a regex (that can include an inferred component name) that is used to validate all data-testid values. It has proved to be useful in larger codebases because it

  1. Avoids the dreaded naming problem
  2. Allows automation and developers to move faster by inferring test Id's without having to look them up every time.

Here is what the rule config looks like

'@custom/my-secret-plugin-name/data-testid': [
  2,
  {
    testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$',
    excludePaths: ['__tests__']
  }
],

If you think this is generally useful I would be happy to submit a PR to add this rule.
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    new ruleNew rule to be included in the pluginreleased

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions