This ESLint plugin enforces the presence of a data-cy
attribute (or other specified test attributes) on specified React components to improve end-to-end (E2E) testing practices.
To install the plugin, run:
npm install --save-dev eslint-plugin-react-data-cy
or
yarn add -D eslint-plugin-react-data-cy
Add react-data-cy
to your ESLint configuration:
{
"extends": ["plugin:react-data-cy/recommended"]
}
or manually configure the rule:
{
"plugins": ["react-data-cy"],
"rules": {
"react-data-cy/react-data-cy-rule": "error"
}
}
The rule accepts an optional configuration object:
{
"rules": {
"react-data-cy/react-data-cy-rule": [
"error",
{
"testAttributes": ["data-cy", "data-testid"],
"components": ["Button", "Input", "*"]
}
]
}
}
-
testAttributes
(array, optional): List of valid test attributes (default:['data-cy']
). -
components
(array, optional): List of components to enforce the rule on. Supports"*"
to apply to all components.
<Button data-cy="submit-button" />
<Input data-testid="username-input" />
<Button /> // ❌ Missing required test attribute
<Input /> // ❌ Missing required test attribute
MIT