Skip to content
← Back to rules

jest/valid-describe-callback 正确性

它的作用

此规则验证 describe() 函数的第二个参数是否为回调函数。该回调函数应满足以下条件:

  • 不能是 async 函数
  • 不能包含任何参数
  • 不能包含任何 return 语句

为什么这是错误的?

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

示例

此规则的错误代码示例:

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

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

// 在 describe 块中返回值是不允许的
describe("myFunction", () =>
  it("返回一个真值", () => {
    expect(myFunction()).toBeTruthy();
  }));

此规则与 eslint-plugin-vitest 兼容。要使用它,请将以下配置添加到你的 .oxlintrc.json 文件中:

json
{
  "rules": {
    "vitest/valid-describe-callback": "error"
  }
}

如何使用

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

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

参考资料