jest/prefer-each
eslint-plugin-jest
No autofix
eslint.config.js
import jest from "eslint-plugin-jest";
export default [
{
plugins: { jest },
rules: {
"jest/prefer-each": "error",
},
},
];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);
});
}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.
No tracked preset enables this rule, so it has no reach yet.
None of the tracked presets enable this rule. Turn it on with the config below.
jest/prefer-each
eslint-plugin-jest
import jest from "eslint-plugin-jest";
export default [
{
plugins: { jest },
rules: {
"jest/prefer-each": "error",
},
},
];jest/prefer-each
jest plugin · style
{
"plugins": [
"jest"
],
"rules": {
"jest/prefer-each": "error"
}
}Biome does not have this rule.
Suggest using the built-in comparison matchers
Prefer having the last statement in a test be an assertion
Enforce padding around test and it blocks
Suggest using expect.assertions() OR expect.hasAssertions()