prefer-comparison-matcher
Added in v0.7.0Configuration
Rule Details
Prefer Jest's built-in comparison matchers over wrapping a relational operator in
expect(...).toBe(true) (or toEqual / toStrictEqual).
Assertions such as expect(x > 5).toBe(true) are harder to read and produce less
helpful failure output than expect(x).toBeGreaterThan(5).
This rule reports expect(left OP right).<equalityMatcher>(true|false) patterns
that can use one of these matchers instead:
toBeGreaterThantoBeGreaterThanOrEqualtoBeLessThantoBeLessThanOrEqual
Violations are automatically fixed where possible.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
String comparisons. These matchers only accept numbers and bigints. The rule
assumes operands are numeric and does not report comparisons that involve string
literals (for example, expect(x < 'Carl').toBe(true)). If you intentionally
compare strings with > or <, disable the rule for that line—otherwise the
fix rewrites the assertion to a numeric matcher and fails at runtime:
Negative expectations use the opposite comparison operator, matching eslint-plugin-jest. This assumes neither operand is NaN: for example, expect(NaN > 1).toBe(false) passes, but its fix expect(NaN).toBeLessThanOrEqual(1) fails. Disable the rule for comparisons that may involve NaN and need to retain that behavior.
Differences from ESLint
- Parentheses around comma-expression operands are preserved, so moving
(read(), value)does not turn it into multiple arguments. - Type assertions on boolean expectations are removed with the boolean, rather than being applied to the replacement operand. For example,
toBe(true as const)does not becometoBeGreaterThan(limit as const). - String literals inside TypeScript type assertions are excluded just like unwrapped string literals.
- Only one diagnostic is emitted for the equality matcher when further calls follow it, such as
expect(a > b).toBe(true).toString(). - Assertions with parentheses around the receiver, such as
(expect(a > b)).toBe(true), are reported without an autofix to avoid removing only the closing parentheses.