jest/prefer-expect-assertions
eslint-plugin-jest
Suggestion
eslint.config.js
import jest from "eslint-plugin-jest";
export default [
{
plugins: { jest },
rules: {
"jest/prefer-expect-assertions": "error",
},
},
];Suggest using expect.assertions() OR expect.hasAssertions()
describe('getNumbers', () => {
it('only returns numbers that are greater than zero', () => {
const numbers = getNumbers();
for (const number in numbers) {
expect(number).toBeGreaterThan(0);
}
});
});describe('getNumbers', () => {
it('only returns numbers that are greater than zero', () => {
const numbers = getNumbers();
getNumbers().forEach(number => {
expect(number).toBeGreaterThan(0);
});
});
});
describe('/users', () => {
it.each([1, 2, 3])('returns ok', id => {
client.get(`/users/${id}`, response => {
expect(response.status).toBe(200);
});
});
});test('my test', () => {
expect.assertions('1');
expect(someThing()).toEqual('foo');
});test('my test', () => {
expect.assertions('1');
expect(someThing()).toEqual('foo');
});
test('my test', () => {
expect(someThing()).toEqual('foo');
});test('my test', () => {
expect.assertions(1);
expect(someThing()).toEqual('foo');
});
test('my test', () => {
expect.hasAssertions();
expect(someThing()).toEqual('foo');
});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-expect-assertions
eslint-plugin-jest
import jest from "eslint-plugin-jest";
export default [
{
plugins: { jest },
rules: {
"jest/prefer-expect-assertions": "error",
},
},
];jest/prefer-expect-assertions
jest plugin · style
{
"plugins": [
"jest"
],
"rules": {
"jest/prefer-expect-assertions": "error"
}
}Biome does not have this rule.
Enforce using expect assertions instead of callbacks
Prefer having the last statement in a test be an assertion
Prefer await expect(...).resolves over expect(await ...) syntax
Suggest using the built-in comparison matchers