# prefer-each

Rule `jest/prefer-each`. Prefer using `.each` rather than manual loops

- Source: jest
- Technologies: Testing
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #1754 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `jest/prefer-each` | no | no | package `eslint-plugin-jest` | [docs](https://github.com/jest-community/eslint-plugin-jest/blob/v29.17.2/docs/rules/prefer-each.md) |
| Oxlint | `jest/prefer-each` | no | no | plugin `jest`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/jest/prefer-each.html) |

## Code examples

### Incorrect: the rule reports this

```js
for (const number of getNumbers()) {
  it('is greater than five', function () {
    expect(number).toBeGreaterThan(5);
  });
}

for (const [input, expected] of data) {
  beforeEach(() => setupSomething(input));

  test(`results in ${expected}`, () => {
    expect(doSomething()).toBe(expected);
  });
}
```

### Correct: the rule accepts this

```js
it.each(getNumbers())(
  'only returns numbers that are greater than seven',
  number => {
    expect(number).toBeGreaterThan(7);
  },
);

describe.each(data)('when input is %s', ([input, expected]) => {
  beforeEach(() => setupSomething(input));

  test(`results in ${expected}`, () => {
    expect(doSomething()).toBe(expected);
  });
});

// we don't warn on loops _in_ test functions because those typically involve
// complex setup that is better done in the test function itself
it('returns numbers that are greater than five', () => {
  for (const number of getNumbers()) {
    expect(number).toBeGreaterThan(5);
  }
});
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import jest from "eslint-plugin-jest";

export default [
  {
    plugins: { jest },
    rules: {
      "jest/prefer-each": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "plugins": [
    "jest"
  ],
  "rules": {
    "jest/prefer-each": "error"
  }
}
```

## Related rules

- [vitest/prefer-each](https://lintradar.dev/rules/vitest/prefer-each.md): Enforce using `each` rather than manual loops
- [jest/prefer-comparison-matcher](https://lintradar.dev/rules/jest/prefer-comparison-matcher.md): Suggest using the built-in comparison matchers
- [jest/prefer-ending-with-an-expect](https://lintradar.dev/rules/jest/prefer-ending-with-an-expect.md): Prefer having the last statement in a test be an assertion
- [jest/padding-around-test-blocks](https://lintradar.dev/rules/jest/padding-around-test-blocks.md): Enforce padding around `test` and `it` blocks
- [jest/prefer-expect-assertions](https://lintradar.dev/rules/jest/prefer-expect-assertions.md): Suggest using `expect.assertions()` OR `expect.hasAssertions()`
- [jest/padding-around-expect-groups](https://lintradar.dev/rules/jest/padding-around-expect-groups.md): Enforce padding around `expect` groups

---

Web page: https://lintradar.dev/rules/jest/prefer-each. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
