source: frontend/node_modules/eslint-plugin-jest/docs/rules/prefer-comparison-matcher.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.7 KB
Line 
1# Suggest using the built-in comparison matchers (`prefer-comparison-matcher`)
2
3Jest has a number of built-in matchers for comparing numbers which allow for
4more readable tests and error messages if an expectation fails.
5
6## Rule details
7
8This rule checks for comparisons in tests that could be replaced with one of the
9following built-in comparison matchers:
10
11- `toBeGreaterThan`
12- `toBeGreaterThanOrEqual`
13- `toBeLessThan`
14- `toBeLessThanOrEqual`
15
16Examples of **incorrect** code for this rule:
17
18```js
19expect(x > 5).toBe(true);
20expect(x < 7).not.toEqual(true);
21expect(x <= y).toStrictEqual(true);
22```
23
24Examples of **correct** code for this rule:
25
26```js
27expect(x).toBeGreaterThan(5);
28expect(x).not.toBeLessThanOrEqual(7);
29expect(x).toBeLessThanOrEqual(y);
30
31// special case - see below
32expect(x < 'Carl').toBe(true);
33```
34
35Note that these matchers only work with numbers and bigints, and that the rule
36assumes that any variables on either side of the comparison operator are of one
37of those types - this means if you're using the comparison operator with
38strings, the fix applied by this rule will result in an error.
39
40```js
41expect(myName).toBeGreaterThanOrEqual(theirName); // Matcher error: received value must be a number or bigint
42```
43
44The reason for this is that comparing strings with these operators is expected
45to be very rare and would mean not being able to have an automatic fixer for
46this rule.
47
48If for some reason you are using these operators to compare strings, you can
49disable this rule using an inline
50[configuration comment](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules):
51
52```js
53// eslint-disable-next-line jest/prefer-comparison-matcher
54expect(myName > theirName).toBe(true);
55```
Note: See TracBrowser for help on using the repository browser.