Skip to content

jest/prefer-comparison-matcher Style

🛠️ An auto-fix is available for this rule.

What it does

This rule checks for comparisons in tests that could be replaced with one of the following built-in comparison matchers:

  • toBeGreaterThan
  • toBeGreaterThanOrEqual
  • toBeLessThan
  • toBeLessThanOrEqual

Why is this bad?

Using generic matchers like toBe(true) with comparison expressions makes tests less readable and provides less helpful error messages when they fail. Jest's specific comparison matchers offer clearer intent and better error output that shows the actual values being compared.

Examples

Examples of incorrect code for this rule:

js
expect(x > 5).toBe(true);
expect(x < 7).not.toEqual(true);
expect(x <= y).toStrictEqual(true);

Examples of correct code for this rule:

js
expect(x).toBeGreaterThan(5);
expect(x).not.toBeLessThanOrEqual(7);
expect(x).toBeLessThanOrEqual(y);
// special case - see below
expect(x < "Carl").toBe(true);

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny jest/prefer-comparison-matcher --jest-plugin
json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-comparison-matcher": "error"
  }
}

References

Released under the MIT License.