eslint-plugin-jest-dom 5.0.0 → 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 +16 -15
- package/dist/assignment-ast.js +10 -7
- package/dist/rules/prefer-in-document.js +82 -13
- package/dist/rules/prefer-to-have-style.js +2 -2
- package/package.json +5 -4
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
|
-
|
|
102
|
-
|
|
|
103
|
-
|
|
|
104
|
-
| [prefer-
|
|
105
|
-
| [prefer-
|
|
106
|
-
| [prefer-
|
|
107
|
-
| [prefer-
|
|
108
|
-
| [prefer-
|
|
109
|
-
| [prefer-
|
|
110
|
-
| [prefer-to-have-
|
|
111
|
-
| [prefer-to-have-
|
|
112
|
-
| [prefer-to-have-
|
|
113
|
-
| [prefer-to-have-
|
|
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
|
|
package/dist/assignment-ast.js
CHANGED
|
@@ -12,17 +12,20 @@ var _queries = require("./queries");
|
|
|
12
12
|
* await someAsyncFunc() => someAsyncFunc()
|
|
13
13
|
* someElement as HTMLDivElement => someElement
|
|
14
14
|
*
|
|
15
|
+
* @param {Object} context - Context for a rule
|
|
15
16
|
* @param {Object} expression - An expression node
|
|
16
17
|
* @returns {Object} - A node
|
|
17
18
|
*/
|
|
18
|
-
function getInnerNodeFrom(expression) {
|
|
19
|
+
function getInnerNodeFrom(context, expression) {
|
|
19
20
|
switch (expression.type) {
|
|
21
|
+
case "Identifier":
|
|
22
|
+
return getAssignmentForIdentifier(context, expression.name);
|
|
20
23
|
case "TSAsExpression":
|
|
21
|
-
return getInnerNodeFrom(expression.expression);
|
|
24
|
+
return getInnerNodeFrom(context, expression.expression);
|
|
22
25
|
case "AwaitExpression":
|
|
23
|
-
return getInnerNodeFrom(expression.argument);
|
|
26
|
+
return getInnerNodeFrom(context, expression.argument);
|
|
24
27
|
case "MemberExpression":
|
|
25
|
-
return getInnerNodeFrom(expression.object);
|
|
28
|
+
return getInnerNodeFrom(context, expression.object);
|
|
26
29
|
default:
|
|
27
30
|
return expression;
|
|
28
31
|
}
|
|
@@ -42,7 +45,7 @@ function getAssignmentForIdentifier(context, identifierName) {
|
|
|
42
45
|
let assignmentNode;
|
|
43
46
|
if (init) {
|
|
44
47
|
// let foo = bar;
|
|
45
|
-
assignmentNode = getInnerNodeFrom(init);
|
|
48
|
+
assignmentNode = getInnerNodeFrom(context, init);
|
|
46
49
|
} else {
|
|
47
50
|
// let foo;
|
|
48
51
|
// foo = bar;
|
|
@@ -50,7 +53,7 @@ function getAssignmentForIdentifier(context, identifierName) {
|
|
|
50
53
|
if (!assignmentRef) {
|
|
51
54
|
return;
|
|
52
55
|
}
|
|
53
|
-
assignmentNode = getInnerNodeFrom(assignmentRef.writeExpr);
|
|
56
|
+
assignmentNode = getInnerNodeFrom(context, assignmentRef.writeExpr);
|
|
54
57
|
}
|
|
55
58
|
return assignmentNode;
|
|
56
59
|
}
|
|
@@ -64,7 +67,7 @@ function getAssignmentForIdentifier(context, identifierName) {
|
|
|
64
67
|
* @returns {Object} - Object with query, queryArg & isDTLQuery
|
|
65
68
|
*/
|
|
66
69
|
function getQueryNodeFrom(context, nodeWithValueProp) {
|
|
67
|
-
const queryNode =
|
|
70
|
+
const queryNode = getInnerNodeFrom(context, nodeWithValueProp);
|
|
68
71
|
if (!queryNode || !queryNode.callee) {
|
|
69
72
|
return {
|
|
70
73
|
isDTLQuery: false,
|
|
@@ -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()
|
|
71
|
-
|
|
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
|
-
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
if (
|
|
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
|
|
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
|
-
|
|
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:
|
|
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
|
|
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
|
});
|
|
@@ -102,7 +102,7 @@ const create = context => {
|
|
|
102
102
|
const startOfStyleMemberExpression = node.object.range[1];
|
|
103
103
|
const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1];
|
|
104
104
|
let fix = null;
|
|
105
|
-
if (typeof styleValue.value !== "number" && !(styleValue.value instanceof RegExp)) {
|
|
105
|
+
if (typeof styleValue.value !== "number" && !(styleValue.value instanceof RegExp) && styleName.type !== "Identifier") {
|
|
106
106
|
fix = fixer => {
|
|
107
107
|
return [fixer.removeRange([startOfStyleMemberExpression, endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, typeof styleName.value === "number" ? `{${getReplacementObjectProperty(styleValue)}: expect.anything()}` : getReplacementStyleParam(styleName, styleValue))];
|
|
108
108
|
};
|
|
@@ -120,7 +120,7 @@ const create = context => {
|
|
|
120
120
|
const matcher = node.parent.parent.parent.parent.property;
|
|
121
121
|
const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1];
|
|
122
122
|
let fix = null;
|
|
123
|
-
if (typeof styleName.value !== "number") {
|
|
123
|
+
if (typeof styleName.value !== "number" && styleName.type !== "Identifier") {
|
|
124
124
|
fix = fixer => {
|
|
125
125
|
return [fixer.removeRange([node.object.range[1], endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))];
|
|
126
126
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest-dom",
|
|
3
|
-
"version": "5.0.
|
|
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": [
|
|
@@ -45,17 +45,18 @@
|
|
|
45
45
|
"requireindex": "^1.2.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
+
"@testing-library/dom": "^8.20.0",
|
|
48
49
|
"@typescript-eslint/parser": "^5.9.1",
|
|
49
50
|
"eslint": "^8.7.0",
|
|
50
51
|
"eslint-doc-generator": "^1.0.0",
|
|
51
52
|
"eslint-remote-tester": "^3.0.0",
|
|
52
53
|
"eslint-remote-tester-repositories": "^1.0.1",
|
|
53
54
|
"kcd-scripts": "^12.0.0",
|
|
54
|
-
"typescript": "^
|
|
55
|
+
"typescript": "^5.1.3"
|
|
55
56
|
},
|
|
56
57
|
"peerDependencies": {
|
|
57
|
-
"
|
|
58
|
-
"
|
|
58
|
+
"@testing-library/dom": "^8.0.0 || ^9.0.0",
|
|
59
|
+
"eslint": "^6.8.0 || ^7.0.0 || ^8.0.0"
|
|
59
60
|
},
|
|
60
61
|
"eslintConfig": {
|
|
61
62
|
"extends": "./node_modules/kcd-scripts/eslint.js",
|