Skip to content
← Back to rules

vitest/valid-describe-callback Correctness

作用

此规则验证 describe() 函数的第二个参数是否为一个 回调函数。该回调函数:

  • 不应包含任何参数
  • 不应包含任何 return 语句

Vitest 支持异步 describe() 回调,因此此规则允许它们。

为什么这很糟糕?

使用不正确的 describe() 回调函数可能会导致意外的测试 错误。

示例

此规则的 错误 代码示例如下:

javascript
// 不允许回调函数参数
describe("myFunction()", (done) => {
  // ...
});

// 不允许在 describe 块中返回值
describe("myFunction", () =>
  it("returns a truthy value", () => {
    expect(myFunction()).toBeTruthy();
  }));

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/valid-describe-callback": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vitest"],
  rules: {
    "vitest/valid-describe-callback": "error",
  },
});
bash
oxlint --deny vitest/valid-describe-callback --vitest-plugin

版本

此规则在 v0.0.8 中添加。

参考资料