vitest/require-awaited-expect-poll Correctness
作用
此规则确保对 expect.poll 和 expect.element 调用返回的 promise 进行正确处理。
为什么这不好?
expect.poll 和 expect.element 会返回 promise。如果没有 await 或 return, 测试会在断言解析之前完成,这意味着无论断言成功还是失败,测试都会通过。
示例
此规则的错误代码示例如下:
js
test("element exists", () => {
asyncInjectElement();
expect.poll(() => document.querySelector(".element")).toBeInTheDocument();
});此规则的正确代码示例如下:
js
test("element exists", () => {
asyncInjectElement();
return expect.poll(() => document.querySelector(".element")).toBeInTheDocument();
});
test("element exists", async () => {
asyncInjectElement();
await expect.poll(() => document.querySelector(".element")).toBeInTheDocument();
});如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vitest"],
"rules": {
"vitest/require-awaited-expect-poll": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/require-awaited-expect-poll": "error",
},
});bash
oxlint --deny vitest/require-awaited-expect-poll --vitest-plugin版本
此规则于 v1.58.0 中添加。
