Skip to content
← Back to rules

vitest/prefer-describe-function-title 风格

An auto-fix is available for this rule.

它的作用

在测试特定函数时,该规则旨在强制要求将命名函数传递给 describe(),而不是使用等效的硬编码字符串。

为什么这是不好的?

如果测试与某个特定函数相关联,而被测试的函数名称被更改,则描述标题将不再匹配,将来可能会造成混淆。使用函数作为参数可以确保即使函数名称被重命名,也能保持一致性。

示例

此规则的错误代码示例:

js
// myFunction.test.js
import { myFunction } from "./myFunction";

describe("myFunction", () => {
  // ...
});

此规则的正确代码示例:

js
// myFunction.test.js
import { myFunction } from "./myFunction";

describe(myFunction, () => {
  // ...
});

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/prefer-describe-function-title": "error"
  }
}
bash
oxlint --deny vitest/prefer-describe-function-title --vitest-plugin

参考资料