eslint-plugin-jest-dom 5.0.1 → 5.0.2

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/README.md CHANGED
@@ -96,21 +96,22 @@ module.exports = {
96
96
 
97
97
  💼 Configurations enabled in.\
98
98
  ✅ Set in the `recommended` configuration.\
99
- 🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).
100
-
101
- | Name                        | Description | 💼 | 🔧 |
102
- | :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- |
103
- | [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes | ✅ | 🔧 |
104
- | [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML | ✅ | 🔧 |
105
- | [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes | ✅ | 🔧 |
106
- | [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement | ✅ | 🔧 |
107
- | [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node | ✅ | 🔧 |
108
- | [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties | ✅ | 🔧 |
109
- | [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking getAttribute/hasAttribute | ✅ | 🔧 |
110
- | [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className | ✅ | 🔧 |
111
- | [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style | ✅ | 🔧 |
112
- | [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent | ✅ | 🔧 |
113
- | [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value | ✅ | 🔧 |
99
+ 🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
100
+ 💡 Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
101
+
102
+ | Name                        | Description | 💼 | 🔧 | 💡 |
103
+ | :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- | :- |
104
+ | [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes | ✅ | 🔧 | |
105
+ | [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML | ✅ | 🔧 | |
106
+ | [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes | ✅ | 🔧 | |
107
+ | [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement | ✅ | 🔧 | |
108
+ | [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node | ✅ | 🔧 | 💡 |
109
+ | [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties | ✅ | 🔧 | |
110
+ | [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking getAttribute/hasAttribute | ✅ | 🔧 | |
111
+ | [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className | ✅ | 🔧 | |
112
+ | [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style | ✅ | 🔧 | |
113
+ | [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent | ✅ | 🔧 | |
114
+ | [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value | ✅ | 🔧 | |
114
115
 
115
116
  <!-- end auto-generated rules list -->
116
117
 
@@ -23,8 +23,11 @@ const meta = {
23
23
  },
24
24
  fixable: "code",
25
25
  messages: {
26
- "use-document": `Prefer .toBeInTheDocument() for asserting DOM node existence`
27
- }
26
+ "use-document": `Prefer .toBeInTheDocument() for asserting DOM node existence`,
27
+ "invalid-combination-length-1": `Invalid combination of {{ query }} and .toHaveLength(1). Did you mean to use {{ allQuery }}?`,
28
+ "replace-query-with-all": `Replace {{ query }} with {{ allQuery }}`
29
+ },
30
+ hasSuggestions: true
28
31
  };
29
32
  exports.meta = meta;
30
33
  function isAntonymMatcher(matcherNode, matcherArguments) {
@@ -38,6 +41,22 @@ function usesToHaveLengthZero(matcherNode, matcherArguments) {
38
41
  // matcherArguments[0].value: toHaveLength(0, ...) means zero length
39
42
  return matcherNode.name === "toHaveLength" && (matcherArguments.length === 0 || matcherArguments[0].value === 0);
40
43
  }
44
+
45
+ /**
46
+ * Extract the DTL query identifier from a call expression
47
+ *
48
+ * <query>() -> <query>
49
+ * screen.<query>() -> <query>
50
+ */
51
+ function getDTLQueryIdentifierNode(callExpressionNode) {
52
+ if (!callExpressionNode || callExpressionNode.type !== "CallExpression") {
53
+ return null;
54
+ }
55
+ if (callExpressionNode.callee.type === "Identifier") {
56
+ return callExpressionNode.callee;
57
+ }
58
+ return callExpressionNode.callee.property;
59
+ }
41
60
  const create = context => {
42
61
  const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual|toBeTruthy|toBeFalsy)$/;
43
62
  function getLengthValue(matcherArguments) {
@@ -67,15 +86,53 @@ const create = context => {
67
86
  // only report on dom nodes which we can resolve to RTL queries.
68
87
  if (!queryNode || !queryNode.name && !queryNode.property) return;
69
88
 
70
- // toHaveLength() is only invalid with 0 or 1
71
- if (matcherNode.name === "toHaveLength" && matcherArguments.length) {
89
+ // *By* query with .toHaveLength(0/1) matcher are considered violations
90
+ //
91
+ // | Selector type | .toHaveLength(1) | .toHaveLength(0) |
92
+ // | ============= | =========================== | ===================================== |
93
+ // | *By* query | Did you mean to use *AllBy* | Replace with .not.toBeInTheDocument() |
94
+ // | *AllBy* query | Correct | Correct
95
+ //
96
+ // @see https://github.com/testing-library/eslint-plugin-jest-dom/issues/171
97
+ //
98
+ if (matcherNode.name === "toHaveLength" && matcherArguments.length === 1) {
72
99
  const lengthValue = getLengthValue(matcherArguments);
73
- // isNotToHaveLengthZero represents .not.toHaveLength(0) which is a valid use of toHaveLength
74
- const isNotToHaveLengthZero = usesToHaveLengthZero(matcherNode, matcherArguments) && negatedMatcher;
75
- const isValidUseOfToHaveLength = isNotToHaveLengthZero || !["Literal", "Identifier"].includes(matcherArguments[0].type) || lengthValue === undefined || lengthValue > 1;
76
- if (isValidUseOfToHaveLength) {
100
+ const queryName = queryNode.name || queryNode.property.name;
101
+ const isSingleQuery = _queries.queries.includes(queryName) && !/AllBy/.test(queryName);
102
+ const hasViolation = isSingleQuery && [1, 0].includes(lengthValue);
103
+ if (!hasViolation) {
77
104
  return;
78
105
  }
106
+ // If length === 1, report violation with suggestions
107
+ // Otherwise fallback to default report
108
+ if (lengthValue === 1) {
109
+ const allQuery = queryName.replace("By", "AllBy");
110
+ return context.report({
111
+ node: matcherNode,
112
+ messageId: "invalid-combination-length-1",
113
+ data: {
114
+ query: queryName,
115
+ allQuery
116
+ },
117
+ loc: matcherNode.loc,
118
+ suggest: [{
119
+ messageId: "replace-query-with-all",
120
+ data: {
121
+ query: queryName,
122
+ allQuery
123
+ },
124
+ fix(fixer) {
125
+ return fixer.replaceText(queryNode.property || queryNode, allQuery);
126
+ }
127
+ }, {
128
+ desc: "Replace .toHaveLength(1) with .toBeInTheDocument()",
129
+ fix(fixer) {
130
+ // Remove any arguments in the matcher
131
+ return [...Array.from(matcherArguments).map(argument => fixer.remove(argument)), fixer.replaceText(matcherNode, "toBeInTheDocument")];
132
+ }
133
+ }]
134
+ });
135
+ }
79
136
  }
80
137
 
81
138
  // toBe() or toEqual() are only invalid with null
@@ -145,12 +202,17 @@ const create = context => {
145
202
  // // const foo = <query> expect(foo).not.<matcher>
146
203
  [`MemberExpression[object.object.callee.name=expect][object.property.name=not][property.name=${alternativeMatchers}][object.object.arguments.0.type=Identifier]`](node) {
147
204
  const queryNode = (0, _assignmentAst.getAssignmentForIdentifier)(context, node.object.object.arguments[0].name);
205
+
206
+ // Not an RTL query
207
+ if (!queryNode || queryNode.type !== "CallExpression") {
208
+ return;
209
+ }
148
210
  const matcherNode = node.property;
149
211
  const matcherArguments = node.parent.arguments;
150
212
  const expect = node.object.object;
151
213
  check({
152
214
  negatedMatcher: true,
153
- queryNode: queryNode && queryNode.callee || queryNode,
215
+ queryNode: queryNode.callee,
154
216
  matcherNode,
155
217
  matcherArguments,
156
218
  expect
@@ -158,12 +220,19 @@ const create = context => {
158
220
  },
159
221
  // const foo = <query> expect(foo).<matcher>
160
222
  [`MemberExpression[object.callee.name=expect][property.name=${alternativeMatchers}][object.arguments.0.type=Identifier]`](node) {
161
- const queryNode = (0, _assignmentAst.getAssignmentForIdentifier)(context, node.object.arguments[0].name);
223
+ // Value expression being assigned to the left-hand value
224
+ const rightValueNode = (0, _assignmentAst.getAssignmentForIdentifier)(context, node.object.arguments[0].name);
225
+
226
+ // Not a DTL query
227
+ if (!rightValueNode || rightValueNode.type !== "CallExpression") {
228
+ return;
229
+ }
230
+ const queryIdentifierNode = getDTLQueryIdentifierNode(rightValueNode);
162
231
  const matcherNode = node.property;
163
232
  const matcherArguments = node.parent.arguments;
164
233
  check({
165
234
  negatedMatcher: false,
166
- queryNode: queryNode && queryNode.callee || queryNode,
235
+ queryNode: queryIdentifierNode,
167
236
  matcherNode,
168
237
  matcherArguments
169
238
  });
@@ -175,12 +244,12 @@ const create = context => {
175
244
  if (!arg) {
176
245
  return;
177
246
  }
178
- const queryNode = arg.type === "AwaitExpression" ? arg.argument.callee : arg.callee;
247
+ const queryIdentifierNode = arg.type === "AwaitExpression" ? getDTLQueryIdentifierNode(arg.argument) : getDTLQueryIdentifierNode(arg);
179
248
  const matcherNode = node.callee.property;
180
249
  const matcherArguments = node.arguments;
181
250
  check({
182
251
  negatedMatcher: false,
183
- queryNode,
252
+ queryNode: queryIdentifierNode,
184
253
  matcherNode,
185
254
  matcherArguments
186
255
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "5.0.1",
3
+ "version": "5.0.2",
4
4
  "description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom",
5
5
  "main": "dist/index.js",
6
6
  "keywords": [
@@ -52,11 +52,11 @@
52
52
  "eslint-remote-tester": "^3.0.0",
53
53
  "eslint-remote-tester-repositories": "^1.0.1",
54
54
  "kcd-scripts": "^12.0.0",
55
- "typescript": "^4.5.3"
55
+ "typescript": "^5.1.3"
56
56
  },
57
57
  "peerDependencies": {
58
- "eslint": "^6.8.0 || ^7.0.0 || ^8.0.0",
59
- "@testing-library/dom": "^8.0.0 || ^9.0.0"
58
+ "@testing-library/dom": "^8.0.0 || ^9.0.0",
59
+ "eslint": "^6.8.0 || ^7.0.0 || ^8.0.0"
60
60
  },
61
61
  "eslintConfig": {
62
62
  "extends": "./node_modules/kcd-scripts/eslint.js",