npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@funeralguide/eslint-plugin-test-selectors

v1.0.3

Published

Makes sure test DOM attributes (e.g. data-test-id) are added to interactive DOM elements.

Downloads

6

Readme

@funeralguide/eslint-plugin-test-selectors

Build Status [![Downloads][downloads-image]][npm-url]

Enforces that a data-test-id attribute is present on interactive DOM elements to help with UI testing.

  • <button>Download</button>
  • <button data-test-id="download-button">Download</button>

Example of @funeralguide/eslint-plugin-test-selectors running in Visual Studio Code:

Example of @funeralguide/eslint-plugin-test-selectors running in Visual Studio Code

Changelog

  • 1.0.0 - initial release

Installation

You'll first need to install ESLint:

$ npm i eslint --save-dev

Next, install @funeralguide/eslint-plugin-test-selectors:

$ npm install @funeralguide/eslint-plugin-test-selectors --save-dev

Note: If you installed ESLint globally (using the -g flag) then you must also install @funeralguide/eslint-plugin-test-selectors globally.

Usage

Add @funeralguide/test-selectors to the plugins section of your .eslintrc configuration fil:

{
    "plugins": [
        "@funeralguide/test-selectors"
    ]
}

If you want to use all the recommended default rules, you can simply add this line to the extends section of your .eslintrc configuration:

{
    "extends": [
        "plugin:@funeralguide/test-selectors/recommended"
    ]
}

By default, this will run all Supported Rules and emit eslint warnings. If you want to be more strict, you can emit eslint errors by instead using plugin:@funeralguide/test-selectors/recommendedWithErrors.

Another option: you can also selectively enable and disable individual rules in the rules section of your .eslintrc configuration. For instance, if you only want to enable the @funeralguide/test-selectors/button rule, skip the extends addition above and simply add the following to the rules section of your .eslintrc configuration:

{
    "rules": {
        "@funeralguide/test-selectors/button": ["warn", "always"]
    }
}

If you like most of the recommended rules by adding the extends option above, but find one in particular to be bothersome, you can simply disable it:

{
    "rules": {
        "@funeralguide/test-selectors/anchor": "off"
    }
}

Note: see Supported Rules below for a full list.

Custom rule options

All tests can be customized individually by passing an object with one or more of the following properties.

testAttribute

The default test attribute expected is data-test-id, but you can override it with whatever you like. Here is how you would use data-some-custom-attribute instead:

{
    "rules": {
        "@funeralguide/test-selectors/onChange": ["warn", "always", { "testAttribute": "data-some-custom-attribute" }]
    }
}

ignoreDisabled

By default all elements with the disabled attribute are ignored, e.g. <input disabled />. If you don't want to ignore this attribute, set ignoreDisabled to false:

{
    "rules": {
        "@funeralguide/test-selectors/onChange": ["warn", "always", { "ignoreDisabled": false }]
    }
}

ignoreReadonly

By default all elements with the readonly attribute are ignored, e.g. <input readonly />. If you don't want to ignore this attribute, set ignoreReadonly to false:

{
    "rules": {
        "@funeralguide/test-selectors/onChange": ["warn", "always", { "ignoreReadonly": false }]
    }
}

htmlOnly

Only supported on button rule, this option will exempt React components called Button from the rule.

{
    "rules": {
        "@funeralguide/test-selectors/button": ["warn", "always", {"htmlOnly": true}]
    }
}

Custom settings

There are a few settings that you can set

ignoreFileName

By default all files will be checked for the rules. However, you can specify a filename as a string to tell ESLint to not check the rules against.

{
    "settings": {
        "@funeralguide/test-selectors": {
            "ignoreFileName": "index.jsx"
        }
    }
}

Supported Rules

  • @funeralguide/test-selectors/anchor
  • @funeralguide/test-selectors/button
  • @funeralguide/test-selectors/input
  • @funeralguide/test-selectors/onChange
  • @funeralguide/test-selectors/onClick
  • @funeralguide/test-selectors/onKeyDown
  • @funeralguide/test-selectors/onKeyUp

Further Reading

If you don't want these test attributes added in production, you can use something like babel-plugin-jsx-remove-data-test-id

Why data attributes and not id or class? Check out some of the following: