jest/no-unneeded-async-expect-function 风格
它的作用
禁止为预期的 Promise 附加不必要的 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();此规则与 eslint-plugin-vitest 兼容。要使用它,请在您的 .oxlintrc.json 中添加以下配置:
json
{
"rules": {
"vitest/no-unneeded-async-expect-function": "error"
}
}如何使用
要通过配置文件或 CLI 启用 此规则,可以使用:
json
{
"plugins": ["jest"],
"rules": {
"jest/no-unneeded-async-expect-function": "error"
}
}bash
oxlint --deny jest/no-unneeded-async-expect-function --jest-plugin