vitest/prefer-describe-function-title Style
作用
在测试某个特定函数时,此规则旨在强制向 describe() 传入一个命名函数, 而不是使用等价的硬编码字符串。
为什么这不好?
对于与某个特定函数相关的测试,如果被测试的函数被重命名, describe 标题将不再匹配,并且将来可能会造成混淆。直接使用函数 即使函数被重命名,也能确保一致性。
示例
以下是此规则的错误代码示例:
js
// myFunction.test.js
import { myFunction } from "./myFunction";
describe("myFunction", () => {
// ...
});以下是此规则的正确代码示例:
js
// myFunction.test.js
import { myFunction } from "./myFunction";
describe(myFunction, () => {
// ...
});如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vitest"],
"rules": {
"vitest/prefer-describe-function-title": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/prefer-describe-function-title": "error",
},
});bash
oxlint --deny vitest/prefer-describe-function-title --vitest-plugin版本
此规则于 v1.39.0 中添加。
