max-expects
Added in v0.6.1
Configuration
rslint.config.ts
import { defineConfig, jestPlugin } from '@rslint/core';
export default defineConfig([
jestPlugin.configs.recommended,
{
rules: {
'jest/max-expects': 'error',
},
},
]);
Rule Details
Enforce a maximum number of expect() calls in a test body. As more assertions are added, a test is more likely to mix multiple objectives. This rule reports when a single test callback exceeds the configured limit.
The rule counts expect() calls inside each test or it callback (including async callbacks and forms such as test.each and it.each that the Jest integration recognizes), and inside each lifecycle hook callback such as beforeEach. Every callback has its own count. Nested expect() calls used as matchers (for example expect.any(Boolean) inside toEqual) and static expect APIs such as expect.hasAssertions() are not counted.
expect calls in a function passed inline to another call inside the test, such as an Array.prototype.forEach callback, count toward that test's limit. A function that is not passed directly as an argument, such as a helper arrow function assigned to a variable, has its own count, and the test's count resumes after it. expect calls outside every test and hook callback, for example at the top level or directly in a describe callback, are not counted.
Examples of incorrect code for this rule (with the default { "max": 5 }):
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
it('should not pass', async () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
describe('test', () => {
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
});
Examples of correct code for this rule (with the default { "max": 5 }):
test('should pass');
test('should pass', () => {});
test.skip('should pass', () => {});
test('should pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
test('should pass', async () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toEqual(expect.any(Boolean));
});
function myHelper() {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
}
test('should pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
myHelper();
});
Options
- First argument (optional): object with
max
max: maximum allowed expect() calls per test callback. Default is 5.
Examples of correct code with { "max": 10 }:
test('should pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
expect(true).toBeDefined();
});
Examples of incorrect code with { "max": 1 }:
test('should not pass', () => {
expect(true).toBeDefined();
expect(true).toBeDefined();
});
Original Documentation