max-expects
Configuration
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 top-level 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). The counter resets when entering a new test case. 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 inside nested functions within a test (for example a helper arrow function defined in the callback) are counted toward that test's limit. expect calls in standalone helper functions defined outside the test callback are not attributed to the test body.
Examples of incorrect code for this rule (with the default { "max": 5 }):
Examples of correct code for this rule (with the default { "max": 5 }):
Options
- First argument (optional): object with
maxmax: maximum allowedexpect()calls per test callback. Default is5.
Examples of correct code with { "max": 10 }:
Examples of incorrect code with { "max": 1 }: