Skip to content
← Back to rules

vitest/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": ["vitest"],
  "rules": {
    "vitest/no-unneeded-async-expect-function": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则在 v1.39.0 中添加。

参考资料