Skip to content
← Back to rules

jest/prefer-each 样式

它的作用

此规则强制使用 each,而不是手动循环。

为什么这不好?

测试中的手动循环可读性可能较差,也更容易出错。使用 each 提供了一种更清晰、更简洁的方式来运行参数化测试, 从而提升可读性和可维护性。

示例

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

js
for (const item of items) {
  describe(item, () => {
    expect(item).toBe("foo");
  });
}

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

js
describe.each(items)("item", (item) => {
  expect(item).toBe("foo");
});

How to use

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

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-each": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["jest"],
  rules: {
    "jest/prefer-each": "error",
  },
});
bash
oxlint --deny jest/prefer-each --jest-plugin

版本

此规则在 v0.9.0 中添加。

参考资料