{"record":{"id":"883913fce63527c8","repo":"oxc-project/oxc","slug":"suggest-using-the-built-in-comparison-matchers","errorCode":null,"errorMessage":"Suggest using the built-in comparison matchers","messagePattern":"Suggest using the built-in comparison matchers","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/prefer_comparison_matcher.rs","lineNumber":16,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, BinaryExpression, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_span::Span;\nuse oxc_syntax::operator::BinaryOperator;\n\nuse crate::{\n    context::LintContext,\n    fixer::RuleFixer,\n    utils::{ParsedExpectFnCall, PossibleJestNode, is_equality_matcher, parse_expect_jest_fn_call},\n};\n\nfn use_to_be_comparison(preferred_method: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Suggest using the built-in comparison matchers\")\n        .with_help(format!(\"Prefer using `{preferred_method:?}` instead\"))\n        .with_label(span)\n}\n\npub const DOCUMENTATION: &str = r\"### What it does\n\nThis rule checks for comparisons in tests that could be replaced with one of the\nfollowing built-in comparison matchers:\n- `toBeGreaterThan`\n- `toBeGreaterThanOrEqual`\n- `toBeLessThan`\n- `toBeLessThanOrEqual`\n\n### Why is this bad?\n\nUsing generic matchers like `toBe(true)` with comparison expressions\nmakes tests less readable and provides less helpful error messages when\nthey fail. Jest's specific comparison matchers offer clearer intent and","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/prefer_comparison_matcher.rs#L1-L34","documentation":"Diagnostic from oxlint's shared jest/vitest prefer-comparison-matcher rule. It fires when `expect()` wraps a raw comparison (`>`, `>=`, `<`, `<=`) asserted with an equality matcher (`toBe`/`toEqual` with true/false), instead of using Jest's built-in comparison matchers. The matcher form produces far better failure messages (it prints both operands and the operator instead of just `false`).","triggerScenarios":"An `expect(...)` argument that is a BinaryExpression with a gt/gte/lt/lte operator combined with an equality matcher (`is_equality_matcher`), e.g. `expect(a > b).toBe(true)`, `expect(price <= budget).toBe(false)`, `expect(age >= 18).toEqual(true)`. Equality (===) and other shapes are untouched.","commonSituations":"Assertions written by developers new to Jest's matcher vocabulary; ported assert-style tests (`assert(a > b)`); reviewer-unnoticed `expect(x > y).toBe(true)` in older suites. Note the NaN edge: `expect(a > b).toBe(false)` passes when a or b is NaN, but `expect(a).toBeLessThanOrEqual(b)` fails — the rewrite is usually desired but is not strictly semantics-preserving there.","solutions":["Rewrite positive comparisons: `expect(a > b).toBe(true)` → `expect(a).toBeGreaterThan(b)` (and gte/lt/lte equivalents).","Rewrite negative comparisons by negating the matcher: `expect(a <= b).toBe(false)` → `expect(a).toBeGreaterThan(b)`.","Where NaN is possible, assert `Number.isNaN` explicitly alongside the matcher rewrite."],"exampleFix":"// before\nexpect(actual.length).toBeGreaterThan(0) ? null : null;\nexpect(total > limit).toBe(true);\nexpect(score < passing).toBe(false);\n\n// after\nexpect(total).toBeGreaterThan(limit);\nexpect(score).toBeGreaterThanOrEqual(passing);","handlingStrategy":"validation","validationCode":"oxlint --jest-plugin test/ # prefer-comparison-matcher","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reach for toBeGreaterThan/OrEqual/toBeLessThan/OrEqual before writing expect(a OP b).toBe(true).","Watch the NaN edge when flipping `toBe(false)` comparisons into matchers — add an explicit Number.isNaN assertion if NaN is possible.","Prefer matcher forms in review: they print both operands on failure."],"tags":["jest","vitest","matchers","style","oxlint","lint"],"backgroundTag":"prefer-jest-matchers","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}