Coder Social home page Coder Social logo

davidcalhoun / eslint-plugin-test-selectors Goto Github PK

View Code? Open in Web Editor NEW
28.0 3.0 15.0 466 KB

Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.

JavaScript 100.00%
eslint lint plugin eslintplugin eslint-plugin testing ui jsx

eslint-plugin-test-selectors's People

Contributors

bkonuwa avatar davidcalhoun avatar jzatt avatar khaledmohamedp avatar milosrasic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

eslint-plugin-test-selectors's Issues

Remove auto-fix

Hi! As I told you before, thank you for a great plugin.

There is one thing that I haven't noticed earlier (it's only mentioned in the changelog) but become a big deal now. AFAIK there is no way to disable the auto-fix option per plugin on the .eslintrc level by design. Many projects have lint-staged with eslint --fix option, or auto fix enabled on save in IDE, and the last thing you want to see in the codebase is a random unconfigurable testid :(

It's a big no-go for adoption :/

cc @bkonuwa @pixelbandito

Bug with testAttribute error messages

I've added this plugin to our projects with the following settings:

rules: [
"test-selectors/onChange": [
      "warn",
      "always",
      {
        "testAttribute": "data-testid"
      }
    ],
]

Is it possible to set the testAttribute for all rules? Because I don't think there's a scenario where you use a different testAttribute for onChange and for button.

testAttribute case sensitivity

Hi! Thank you for such a useful plugin. There is one thing that confuses me a little bit which is related to testAttribute case insensitivity. For example:

<Button onClick={handleClose} data-testid="cancelButton">Close</Button>

and

<Button onClick={handleClose} data-testId="cancelButton">Close</Button>

Both variants (testid and testId) are treated the same with the following setup: 'test-selectors/onClick': [severity, 'always', { testAttribute: ['data-testid'] }],
Is it expected behavior?

Test selector is not working in angular projects.

Hi,
I am trying to add this plugin to the angular project (v12). Not successfull as of now.
I extended both angular HTML rule and test selector rule angular rules are working but not the test-selector rules.

In my .eslintrc.json file I added the plugin as recommended by the documentation.

image

Here is my html
image

when I run ng lint I am not getting the lint error.

image

Is there anything else I am missing here?

Allow multiple selectors

How open would you be to the testAttribute config property being an array (string or array I guess)? Normally you want to enforce consistency, but here's my reasoning. You can't destructure 'data-test-id' in props because it's not a valid variable name, so I'd love to be able to do this:

// in consumer
<SomeComponentWithAButton dataTest="something" />

// in component
const SomeComponentWithAButton = ({ dataTest }) => {
  <button data-test={`btn-${ dataTest }`}>blah</button>
};

Currently that doesn't work. I'd have to use data-test everywhere, and then in the actual component do something like

({ other, props, ...rest }) => {
  const dataTest = rest['data-test']
}

Either that or an option to turn the rule off for all components and only enforce it on html elements. But I prefer the former cause I do want to enforce dataTest being there.

This package doesn't work?

Hey there,

I was looking to add this to our eslint setup since it supports configuring the name of the attribute, but when I install it and follow the setup in the readme, I get this for every file being linted:

 1:1  error  Definition for rule 'test-selectors/onChange' was not found   test-selectors/onChange
  1:1  error  Definition for rule 'test-selectors/anchor' was not found     test-selectors/anchor
  1:1  error  Definition for rule 'test-selectors/button' was not found     test-selectors/button
  1:1  error  Definition for rule 'test-selectors/input' was not found      test-selectors/input
  1:1  error  Definition for rule 'test-selectors/onClick' was not found    test-selectors/onClick
  1:1  error  Definition for rule 'test-selectors/onKeyDown' was not found  test-selectors/onKeyDown
  1:1  error  Definition for rule 'test-selectors/onKeyUp' was not found    test-selectors/onKeyUp

and it looks like that's because lib/index uses

requireIndex(`${ __dirname }/rules`)

but there isn't an index file in rules (and looking at the requireindex package, it seems like that's how it's intended to be used).

Errors vs warnings

Need to create an optional recommendation set that spits out warnings instead of errors.

test-data-ids for static content, useful when asserting.

Hey all! Thanks for building this up. Very handy extension - we're exploring adopting this, with conjunction of semi-auto-generating test-data-ids with react-id-generator.

One suggestion I had for the current rules is that they are focused on elements which you'd be interacting with (buttons, anchors). The flip side of that is conducting assertions - making sure a certain element contains text (post action or page load). I'm not sure how difficult it would be to add rules for <p>, <span>, <h1> through <h6> and so on.

Once we fully adopt the plugin, I'll give it a spin - but as part of our evaluation of this as a potential solution, this jumped out as a big missing part.

I'm also not sure if there's an easy way to incorporate auto-generation of ids - for most of my elements, I don't care if they're pretty or not, I just want a unique one. If it's commonly used, I'll name it myself, but it'd be great to have a --fix option that just generates random ones / configurable ones. Some documentation on how to wire this up or native functionality would really make this a one-stop-shop for this use case! :)

Update readme

The way this is currently implemented, this needs to be added, but it's missing from the docs:

    "extends": [
        "plugin:test-selectors/recommended"
    ],

Allow data-testid

Issue

  • Large codebase using data-testid rather than data-test-id
  • Would like to use this plugin to enforce setting data-testid

Request

  • Be able to set data-testid as the target syntax in the settings of this plugin

customise auto-generated attributes

i find auto-generated ids useful, but it's not great to have a lot of random ids in the codebase. would be nice to be able to configure it, for example so that it takes some kind of combination of component name / file name to generate the id.

Don't error for `readonly` or `disabled` by default

I think it would be good to exclude elements with readonly or disabled by default with the ability to turn it on. I am using input's for displaying some non-editable information and this lint rule set errors on them. One other option would be to not error on input without an onChange, but I am not sure which would be better and more robust.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.