Skip to content

jest/prefer-each Style

What it does

This rule enforces using each rather than manual loops.

Why is this bad?

Manual loops for tests can be less readable and more error-prone. Using each provides a clearer and more concise way to run parameterized tests, improving readability and maintainability.

Examples

Examples of incorrect code for this rule:

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

Examples of correct code for this rule:

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

How to use

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

bash
oxlint --deny jest/prefer-each --jest-plugin
json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-each": "error"
  }
}

References

Released under the MIT License.