eslint-plugin-jest 25.5.0 → 25.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [25.6.0](https://github.com/jest-community/eslint-plugin-jest/compare/v25.5.0...v25.6.0) (2022-01-15)
2
+
3
+
4
+ ### Features
5
+
6
+ * create `prefer-comparison-matcher` rule ([#1015](https://github.com/jest-community/eslint-plugin-jest/issues/1015)) ([eb11876](https://github.com/jest-community/eslint-plugin-jest/commit/eb118761a422b3589311113cd827a6be437f5bb5))
7
+
1
8
  # [25.5.0](https://github.com/jest-community/eslint-plugin-jest/compare/v25.4.0...v25.5.0) (2022-01-15)
2
9
 
3
10
 
package/README.md CHANGED
@@ -177,6 +177,7 @@ installations requiring long-term consistency.
177
177
  | [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
178
178
  | [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
179
179
  | [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
180
+ | [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | Suggest using the built-in comparison matchers | | ![fixable][] |
180
181
  | [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
181
182
  | [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
182
183
  | [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
@@ -0,0 +1,55 @@
1
+ # Suggest using the built-in comparison matchers (`prefer-comparison-matcher`)
2
+
3
+ Jest has a number of built-in matchers for comparing numbers which allow for
4
+ more readable tests and error messages if an expectation fails.
5
+
6
+ ## Rule details
7
+
8
+ This rule checks for comparisons in tests that could be replaced with one of the
9
+ following built-in comparison matchers:
10
+
11
+ - `toBeGreaterThan`
12
+ - `toBeGreaterThanOrEqual`
13
+ - `toBeLessThan`
14
+ - `toBeLessThanOrEqual`
15
+
16
+ Examples of **incorrect** code for this rule:
17
+
18
+ ```js
19
+ expect(x > 5).toBe(true);
20
+ expect(x < 7).not.toEqual(true);
21
+ expect(x <= y).toStrictEqual(true);
22
+ ```
23
+
24
+ Examples of **correct** code for this rule:
25
+
26
+ ```js
27
+ expect(x).toBeGreaterThan(5);
28
+ expect(x).not.toBeLessThanOrEqual(7);
29
+ expect(x).toBeLessThanOrEqual(y);
30
+
31
+ // special case - see below
32
+ expect(x < 'Carl').toBe(true);
33
+ ```
34
+
35
+ Note that these matchers only work with numbers and bigints, and that the rule
36
+ assumes that any variables on either side of the comparison operator are of one
37
+ of those types - this means if you're using the comparison operator with
38
+ strings, the fix applied by this rule will result in an error.
39
+
40
+ ```js
41
+ expect(myName).toBeGreaterThanOrEqual(theirName); // Matcher error: received value must be a number or bigint
42
+ ```
43
+
44
+ The reason for this is that comparing strings with these operators is expected
45
+ to be very rare and would mean not being able to have an automatic fixer for
46
+ this rule.
47
+
48
+ If for some reason you are using these operators to compare strings, you can
49
+ disable 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
54
+ expect(myName > theirName).toBe(true);
55
+ ```
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _experimentalUtils = require("@typescript-eslint/experimental-utils");
9
+
10
+ var _utils = require("./utils");
11
+
12
+ const isBooleanLiteral = node => node.type === _experimentalUtils.AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
13
+
14
+ /**
15
+ * Checks if the given `ParsedExpectMatcher` is a call to one of the equality matchers,
16
+ * with a boolean literal as the sole argument.
17
+ *
18
+ * @example javascript
19
+ * toBe(true);
20
+ * toEqual(false);
21
+ *
22
+ * @param {ParsedExpectMatcher} matcher
23
+ *
24
+ * @return {matcher is ParsedBooleanEqualityMatcher}
25
+ */
26
+ const isBooleanEqualityMatcher = matcher => (0, _utils.isParsedEqualityMatcherCall)(matcher) && isBooleanLiteral((0, _utils.followTypeAssertionChain)(matcher.arguments[0]));
27
+
28
+ const isString = node => {
29
+ return (0, _utils.isStringNode)(node) || node.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral;
30
+ };
31
+
32
+ const isComparingToString = expression => {
33
+ return isString(expression.left) || isString(expression.right);
34
+ };
35
+
36
+ const invertOperator = operator => {
37
+ switch (operator) {
38
+ case '>':
39
+ return '<=';
40
+
41
+ case '<':
42
+ return '>=';
43
+
44
+ case '>=':
45
+ return '<';
46
+
47
+ case '<=':
48
+ return '>';
49
+ }
50
+
51
+ return null;
52
+ };
53
+
54
+ const determineMatcher = (operator, negated) => {
55
+ const op = negated ? invertOperator(operator) : operator;
56
+
57
+ switch (op) {
58
+ case '>':
59
+ return 'toBeGreaterThan';
60
+
61
+ case '<':
62
+ return 'toBeLessThan';
63
+
64
+ case '>=':
65
+ return 'toBeGreaterThanOrEqual';
66
+
67
+ case '<=':
68
+ return 'toBeLessThanOrEqual';
69
+ }
70
+
71
+ return null;
72
+ };
73
+
74
+ var _default = (0, _utils.createRule)({
75
+ name: __filename,
76
+ meta: {
77
+ docs: {
78
+ category: 'Best Practices',
79
+ description: 'Suggest using the built-in comparison matchers',
80
+ recommended: false
81
+ },
82
+ messages: {
83
+ useToBeComparison: 'Prefer using `{{ preferredMatcher }}` instead'
84
+ },
85
+ fixable: 'code',
86
+ type: 'suggestion',
87
+ schema: []
88
+ },
89
+ defaultOptions: [],
90
+
91
+ create(context) {
92
+ return {
93
+ CallExpression(node) {
94
+ if (!(0, _utils.isExpectCall)(node)) {
95
+ return;
96
+ }
97
+
98
+ const {
99
+ expect: {
100
+ arguments: [comparison],
101
+ range: [, expectCallEnd]
102
+ },
103
+ matcher,
104
+ modifier
105
+ } = (0, _utils.parseExpectCall)(node);
106
+
107
+ if (!matcher || (comparison === null || comparison === void 0 ? void 0 : comparison.type) !== _experimentalUtils.AST_NODE_TYPES.BinaryExpression || isComparingToString(comparison) || !isBooleanEqualityMatcher(matcher)) {
108
+ return;
109
+ }
110
+
111
+ const preferredMatcher = determineMatcher(comparison.operator, (0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value === !!modifier);
112
+
113
+ if (!preferredMatcher) {
114
+ return;
115
+ }
116
+
117
+ context.report({
118
+ fix(fixer) {
119
+ const sourceCode = context.getSourceCode();
120
+ return [// replace the comparison argument with the left-hand side of the comparison
121
+ fixer.replaceText(comparison, sourceCode.getText(comparison.left)), // replace the current matcher & modifier with the preferred matcher
122
+ fixer.replaceTextRange([expectCallEnd, matcher.node.range[1]], `.${preferredMatcher}`), // replace the matcher argument with the right-hand side of the comparison
123
+ fixer.replaceText(matcher.arguments[0], sourceCode.getText(comparison.right))];
124
+ },
125
+
126
+ messageId: 'useToBeComparison',
127
+ data: {
128
+ preferredMatcher
129
+ },
130
+ node: (modifier || matcher).node.property
131
+ });
132
+ }
133
+
134
+ };
135
+ }
136
+
137
+ });
138
+
139
+ exports.default = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "25.5.0",
3
+ "version": "25.6.0",
4
4
  "description": "Eslint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",