@corva/eslint-plugin-test-selectors

中文文档
1.0.0发布于 2023/12/15ISCCommonJS近 30 天下载 7158

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

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Custom rule options
  4. testAttribute
  5. ignoreDisabled
  6. ignoreReadonly
  7. htmlOnly
  8. Supported Rules

eslint-plugin-test-selectors

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

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

Installation

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

$ yarn add eslint --dev

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

$ yarn add @corva/eslint-plugin-test-selectors --dev

Usage

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

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

{
  "rules": {
    "@corva/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": {
    "@corva/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": {
    "@corva/test-selectors/onChange": [
      "warn",
      "always",
      { "testAttribute": "data-some-custom-attribute" }
    ]
  }
}

Note: You can also pass multiple attributes

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

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

Supported Rules

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

版本与发布记录

@corva/eslint-plugin-test-selectors 共发布 2 个版本,首次发布于 2023/12/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2023/12/152 年前
0.0.0-c3397592023/12/142 年前

关于 @corva/eslint-plugin-test-selectors 的常见问题

@corva/eslint-plugin-test-selectors 是什么?

Makes sure test DOM attributes (e.g. data-test-id) are added to interactive DOM elements.注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 @corva/eslint-plugin-test-selectors?

在项目目录运行 npm install @corva/eslint-plugin-test-selectors,或使用 pnpm add / yarn add / bun add。

@corva/eslint-plugin-test-selectors 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/corva__eslint-plugin-test-selectors 是否存在。

@corva/eslint-plugin-test-selectors 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@corva/eslint-plugin-test-selectors 可以商用吗?

@corva/eslint-plugin-test-selectors 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@corva/eslint-plugin-test-selectors 的下载量有多少?

根据 npm 官方下载统计,@corva/eslint-plugin-test-selectors 在 2026/10/11 – 2026/11/09 这 30 天内被下载 7,158 次,日均约 239 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@corva/eslint-plugin-test-selectors 还在维护吗?

最新版本 1.0.0 发布于 2023/12/15(2 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/12/14。作者已将其标记为弃用,新项目建议寻找替代方案。