Skip to content
← Back to rules

jest/valid-describe-callback Correctness

作用

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

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

为什么这不好?

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

示例

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

javascript
// 不允许使用异步回调函数
describe("myFunction()", async () => {
  // ...
});

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

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

How to use

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

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

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

版本

此规则于 v0.0.8 中添加。

参考资料