Skip to content
← Back to rules

jest/valid-title 正确性

🛠️ An auto-fix is available for this rule for some violations.

它做了什么

检查 Jest 和 Vitest 块的标题是否有效。

标题必须满足以下条件:

  • 不能为空,
  • 必须是字符串,
  • 不能以块名称作为前缀,
  • 不能有首尾空格。

为什么这是不好的?

无效的标题可能会产生误导,使测试目的更难理解。

示例

此规则的 错误 代码示例:

javascript
describe("", () => {});
describe("foo", () => {
  it("", () => {});
});
it("", () => {});
test("", () => {});
xdescribe("", () => {});
xit("", () => {});
xtest("", () => {});

此规则的 正确 代码示例:

javascript
describe("foo", () => {});
it("bar", () => {});
test("baz", () => {});

选项

typescript
interface Options {
  ignoreSpaces?: boolean; // 是否忽略空格检查
  ignoreTypeOfTestName?: boolean; // 是否忽略 test 块名称类型检查
  ignoreTypeOfDescribeName?: boolean; // 是否忽略 describe 块名称类型检查
  allowArguments?: boolean; // 是否允许在标题中包含参数
  disallowedWords?: string[]; // 禁止出现的词汇列表
  mustNotMatch?: Partial<Record<"describe" | "test" | "it", string>> | string; // 不应匹配的模式(可选)
  mustMatch?: Partial<Record<"describe" | "test" | "it", string>> | string; // 必须匹配的模式(可选)
}

如何使用

通过配置文件或 CLI 启用此规则的方法如下:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/valid-title": "error"
  }
}
bash
oxlint --deny jest/valid-title --jest-plugin

参考资料