Skip to content
← Back to rules

jest/no-unneeded-async-expect-function Style

🛠️ An auto-fix is available for this rule.

它的作用

禁止对预期的 promises 使用不必要的 async 函数包装器。

为什么这不好?

当 async 包装器中的唯一语句是 await someCall() 时, 应当直接将该调用传递给 expect。这使测试代码更简洁, 也更容易阅读。

示例

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

js
await expect(async () => {
  await doSomethingAsync();
}).rejects.toThrow();

await expect(async () => await doSomethingAsync()).rejects.toThrow();

以下是此规则的正确代码示例:

js
await expect(doSomethingAsync()).rejects.toThrow();

如何使用

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

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-unneeded-async-expect-function": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["jest"],
  rules: {
    "jest/no-unneeded-async-expect-function": "error",
  },
});
bash
oxlint --deny jest/no-unneeded-async-expect-function --jest-plugin

版本

此规则在 v1.39.0 中添加。

参考资料