vitest/prefer-each Style
它的作用
此规则强制使用 each,而不是手动循环。
这为什么不好?
用于测试的手动循环可读性较差,也更容易出错。使用 each 提供了一种更清晰、更简洁的方式来运行参数化测试, 从而提高可读性和可维护性。
示例
此规则的错误代码示例:
js
for (const item of items) {
describe(item, () => {
expect(item).toBe("foo");
});
}此规则的正确代码示例:
js
describe.each(items)("item", (item) => {
expect(item).toBe("foo");
});如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vitest"],
"rules": {
"vitest/prefer-each": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/prefer-each": "error",
},
});bash
oxlint --deny vitest/prefer-each --vitest-plugin版本
此规则在 v0.9.0 中添加。
