All Projects → davidcalhoun → eslint-plugin-test-selectors

davidcalhoun / eslint-plugin-test-selectors

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to eslint-plugin-test-selectors

eslint-plugin-layout-shift
ESLint plugin to force responsive media elements to set the width/height attributes
Stars: ✭ 15 (-21.05%)
Mutual labels:  eslint, jsx, eslint-plugin
Typescript Eslint
✨ Monorepo for all the tooling which enables ESLint to support TypeScript
Stars: ✭ 10,831 (+56905.26%)
Mutual labels:  eslint, eslint-plugin, eslintplugin
eslint-plugin-lodash-template
ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.
Stars: ✭ 15 (-21.05%)
Mutual labels:  eslint, eslint-plugin, eslintplugin
Eslint Plugin Import
ESLint plugin with rules that help validate proper imports.
Stars: ✭ 3,722 (+19489.47%)
Mutual labels:  lint, eslint, eslint-plugin
Sowing Machine
🌱A React UI toolchain & JSX alternative
Stars: ✭ 64 (+236.84%)
Mutual labels:  eslint, jsx, eslint-plugin
html-eslint
ESLint plugin for linting HTML
Stars: ✭ 72 (+278.95%)
Mutual labels:  lint, eslint, eslint-plugin
eslint-plugin-react-hook-form
ESLint plugin for react-hook-form
Stars: ✭ 27 (+42.11%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-ban
Ban some methods and functions
Stars: ✭ 23 (+21.05%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-total-functions
An ESLint plugin to enforce the use of total functions (and prevent the use of partial functions) in TypeScript.
Stars: ✭ 72 (+278.95%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-decorator-position
ESLint plugin for enforcing decorator position
Stars: ✭ 32 (+68.42%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-editorconfig
An ESLint plugin to enforce EditorConfig rules
Stars: ✭ 22 (+15.79%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin
autofix some errors reported by eslint rules.
Stars: ✭ 74 (+289.47%)
Mutual labels:  eslint, eslint-plugin
elint
A easy way to lint your code
Stars: ✭ 38 (+100%)
Mutual labels:  lint, eslint
arcanist-linters
A collection of custom Arcanist linters
Stars: ✭ 64 (+236.84%)
Mutual labels:  lint, eslint
eslint-plugin-svelte
ESLint plugin for Svelte using AST
Stars: ✭ 22 (+15.79%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-disable
Disable ESLint plugins using file path patterns and inline comments
Stars: ✭ 51 (+168.42%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-sql
SQL linting rules for ESLint.
Stars: ✭ 56 (+194.74%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin
😎 基于 @lint-md,提供 eslint-plugin,让 lint-md 玩家在 IDE 中得到愉悦的文档编写体验。
Stars: ✭ 22 (+15.79%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin
Enforcing best practices for Effector
Stars: ✭ 69 (+263.16%)
Mutual labels:  eslint, eslint-plugin
eslint-config-get-off-my-lawn
A highly opinionated, sharable config of ESLint rules to produce beautiful, readable JavaScript.
Stars: ✭ 44 (+131.58%)
Mutual labels:  eslint, eslint-plugin

eslint-plugin-test-selectors

Build Status Downloads

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 eslint-plugin-test-selectors running in Visual Studio Code:

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

Changelog

  • 2.0.0 - new onSubmit rule (thank you @@jzatt), upgrade to ESLint 8 and Mocha 9, fix moderate security advisory for chalk/ansi-regex
  • 1.3.0 - Add auto-fix capability to onClick (thank you @bkonuwa and @pixelbandito). (#8)
  • 1.1.0
    • elements with disabled and readonly attributes are now ignored by default. See Custom Rules Options to customize this behavior. (fixes #3)
    • plugin:test-selectors/recommended now emits warnings by default instead of errors. For the old stricter behavior which emits errors, folks can use plugin:test-selectors/recommendedWithErrors (fixes #4)
    • Refactoring and cleanup. Readme improvements.
  • 1.0.1 - fix bug with inline functions (fixes #1)
  • 1.0.0 - initial release

Installation

You'll first need to install ESLint, which requires Node.js (note that eslint-plugin-test-selectors requires Node.js 10+):

$ npm i eslint --save-dev

Next, install eslint-plugin-test-selectors:

$ npm install eslint-plugin-test-selectors --save-dev

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

Usage

Add test-selectors to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": ["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: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: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 test-selectors/button rule, skip the extends addition above and simply add the following to the rules section of your .eslintrc configuration:

{
  "rules": {
    "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": {
    "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": {
    "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": {
    "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": {
    "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": {
    "test-selectors/button": ["warn", "always", { "htmlOnly": true }]
  }
}

Supported Rules

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

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:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].