eslint-plugin-test-selectors is an ESLint plugin designed to enforce the presence of specific DOM attributes, such as `data-test-id`, on interactive elements within your application's UI. This is crucial for robust UI testing, allowing test automation tools to reliably select and interact with elements. The plugin is currently on stable version `2.2.0`, with recent updates indicating active maintenance and minor feature development. It supports highly configurable test attributes, allowing users to specify single or multiple attribute names (e.g., `data-testid`, `testId`) for improved flexibility. Key differentiators include its ability to ignore disabled or readonly elements by default, and auto-fix capabilities for certain rules, like `onClick`. It integrates seamlessly with ESLint configurations, offering recommended rule sets (`recommended` and `recommendedWithErrors`) and granular control over individual rules, making it a flexible tool for improving the testability of frontend applications.
npm install eslint-plugin-test-selectorsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure `eslint-plugin-test-selectors` in a `.eslintrc.json` file, extending recommended rules to emit errors, and explicitly enabling the `button` rule with custom `testAttribute` options. The accompanying `App.jsx` shows examples of interactive elements that will trigger linting errors (a button and a submit input without one of the specified `data-test-id` or `data-automation-id` attributes) and correctly configured elements.
Upgrade ESLint to `8.5.0` or later (e.g., `npm install eslint@^8 --save-dev`). Review your `package.json` and ensure all related testing dependencies like Mocha are also updated if applicable.
Ensure your project's Node.js runtime environment is version 10.x or higher. It is recommended to use a Node Version Manager (NVM) to easily manage and switch between Node.js versions.
Upgrade to `eslint-plugin-test-selectors@2.0.0` or later: `npm install eslint-plugin-test-selectors@latest --save-dev`.
Upgrade to `eslint-plugin-test-selectors@2.1.0` or later: `npm install eslint-plugin-test-selectors@latest --save-dev`.
To utilize multiple test attributes, update your rule configuration from `"testAttribute": "data-test-id"` to `"testAttribute": ["data-test-id", "data-automation-id"]`.
Ensure `eslint-plugin-test-selectors` is installed as a dev dependency (`npm install eslint-plugin-test-selectors --save-dev`) in your project. If ESLint is installed globally, the plugin must also be installed globally (`npm install -g eslint-plugin-test-selectors`).
Add `"test-selectors"` to the `plugins` array in your `.eslintrc` file and verify the rule name (e.g., `"test-selectors/button"`) is exactly correct.
Change `"eslint-plugin-test-selectors"` to `"test-selectors"` in the `plugins` array of your `.eslintrc` file.