eslint-plugin-jest-dom 5.5.0 → 5.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/README.md +13 -13
- package/dist/context.js +1 -1
- package/dist/createBannedAttributeRule.js +9 -1
- package/dist/rules/prefer-checked.js +2 -1
- package/dist/rules/prefer-to-have-attribute.js +2 -2
- package/dist/rules/prefer-to-have-class.js +3 -3
- package/dist/rules/prefer-to-have-style.js +5 -5
- package/dist/rules/prefer-to-have-text-content.js +2 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -123,19 +123,19 @@ module.exports = [
|
|
|
123
123
|
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
|
|
124
124
|
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
125
125
|
|
|
126
|
-
| Name                       | Description | 💼
|
|
127
|
-
| :----------------------------------------------------------------------- | :-------------------------------------------------------------------- |
|
|
128
|
-
| [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes | ✅ | 🔧
|
|
129
|
-
| [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML | ✅ | 🔧
|
|
130
|
-
| [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes | ✅ | 🔧
|
|
131
|
-
| [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement | ✅ | 🔧
|
|
132
|
-
| [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node | ✅ | 🔧
|
|
133
|
-
| [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties | ✅ | 🔧
|
|
134
|
-
| [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking
|
|
135
|
-
| [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className | ✅ | 🔧
|
|
136
|
-
| [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style | ✅ | 🔧
|
|
137
|
-
| [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent | ✅ | 🔧
|
|
138
|
-
| [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value | ✅ | 🔧
|
|
126
|
+
| Name                       | Description | 💼 | 🔧 | 💡 |
|
|
127
|
+
| :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :-- | :-- | :-- |
|
|
128
|
+
| [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes | ✅ | 🔧 | |
|
|
129
|
+
| [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML | ✅ | 🔧 | |
|
|
130
|
+
| [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes | ✅ | 🔧 | |
|
|
131
|
+
| [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement | ✅ | 🔧 | |
|
|
132
|
+
| [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node | ✅ | 🔧 | 💡 |
|
|
133
|
+
| [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties | ✅ | 🔧 | |
|
|
134
|
+
| [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking getAttribute/hasAttribute | ✅ | 🔧 | |
|
|
135
|
+
| [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className | ✅ | 🔧 | |
|
|
136
|
+
| [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style | ✅ | 🔧 | |
|
|
137
|
+
| [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent | ✅ | 🔧 | |
|
|
138
|
+
| [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value | ✅ | 🔧 | |
|
|
139
139
|
|
|
140
140
|
<!-- end auto-generated rules list -->
|
|
141
141
|
|
package/dist/context.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.getScope = getScope;
|
|
|
7
7
|
exports.getSourceCode = getSourceCode;
|
|
8
8
|
/* istanbul ignore next */
|
|
9
9
|
function getSourceCode(context) {
|
|
10
|
-
if (
|
|
10
|
+
if ("sourceCode" in context) {
|
|
11
11
|
return context.sourceCode;
|
|
12
12
|
}
|
|
13
13
|
return context.getSourceCode();
|
|
@@ -8,8 +8,10 @@ var _assignmentAst = require("./assignment-ast");
|
|
|
8
8
|
var _default = ({
|
|
9
9
|
preferred,
|
|
10
10
|
negatedPreferred,
|
|
11
|
-
attributes
|
|
11
|
+
attributes,
|
|
12
|
+
excludeValues = []
|
|
12
13
|
}) => context => {
|
|
14
|
+
const isExcludedValue = node => excludeValues.length > 0 && node.arguments.length >= 2 && node.arguments[1].type === "Literal" && typeof node.arguments[1].value === "string" && excludeValues.some(v => v.toLowerCase() === node.arguments[1].value.toLowerCase());
|
|
13
15
|
const getCorrectFunctionFor = (node, negated = false) => (node.arguments.length === 1 || node.arguments[1].value === true || node.arguments[1].type !== "Literal" || typeof node.arguments[1].value === "string" && node.arguments[1].value.toLowerCase() === "true" || node.arguments[1].value === "") && !negated ? preferred : negatedPreferred;
|
|
14
16
|
const isBannedArg = node => node.arguments.length && attributes.some(attr => attr === node.arguments[0].value);
|
|
15
17
|
|
|
@@ -62,6 +64,9 @@ var _default = ({
|
|
|
62
64
|
if (!isBannedArg(node)) {
|
|
63
65
|
return;
|
|
64
66
|
}
|
|
67
|
+
if (isExcludedValue(node)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
65
70
|
const arg = node.arguments[0].value;
|
|
66
71
|
const correctFunction = getCorrectFunctionFor(node, true);
|
|
67
72
|
const incorrectFunction = node.callee.property.name;
|
|
@@ -75,6 +80,9 @@ var _default = ({
|
|
|
75
80
|
if (!isBannedArg(node)) {
|
|
76
81
|
return;
|
|
77
82
|
}
|
|
83
|
+
if (isExcludedValue(node)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
78
86
|
const {
|
|
79
87
|
isDTLQuery
|
|
80
88
|
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
|
|
@@ -23,5 +23,6 @@ const meta = exports.meta = {
|
|
|
23
23
|
const create = exports.create = (0, _createBannedAttributeRule.default)({
|
|
24
24
|
preferred: "toBeChecked",
|
|
25
25
|
negatedPreferred: "not.toBeChecked",
|
|
26
|
-
attributes: ["checked", "aria-checked"]
|
|
26
|
+
attributes: ["checked", "aria-checked"],
|
|
27
|
+
excludeValues: ["mixed"]
|
|
27
28
|
});
|
|
@@ -28,7 +28,7 @@ const create = context => ({
|
|
|
28
28
|
context.report({
|
|
29
29
|
node: node.parent,
|
|
30
30
|
message: `Use toHaveAttribute instead of asserting on getAttribute`,
|
|
31
|
-
fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `not.toHaveAttribute(${
|
|
31
|
+
fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `not.toHaveAttribute(${(0, _context.getSourceCode)(context).getText(node.arguments[0])})`)]
|
|
32
32
|
});
|
|
33
33
|
},
|
|
34
34
|
[`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](node) {
|
|
@@ -82,7 +82,7 @@ const create = context => ({
|
|
|
82
82
|
context.report({
|
|
83
83
|
node: node.parent,
|
|
84
84
|
message: `Use toHaveAttribute instead of asserting on hasAttribute`,
|
|
85
|
-
fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `${node.parent.parent.property.name === "toBeFalsy" ? "not." : ""}toHaveAttribute(${
|
|
85
|
+
fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `${node.parent.parent.property.name === "toBeFalsy" ? "not." : ""}toHaveAttribute(${(0, _context.getSourceCode)(context).getText(node.arguments[0])})`)]
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
88
|
});
|
|
@@ -143,7 +143,7 @@ const create = context => ({
|
|
|
143
143
|
[[`CallExpression[callee.object.callee.name=expect][callee.property.name=toHaveAttribute][arguments.0.type=/Literal/][arguments.1.type=/Literal$/]`, `CallExpression[callee.object.callee.name=expect][callee.property.name=toHaveProperty][arguments.0.type=/Literal/][arguments.1.type=/Literal$/]`].join(",")](node) {
|
|
144
144
|
const matcher = node.callee.property;
|
|
145
145
|
const [classArg, classValueArg] = node.arguments;
|
|
146
|
-
const classNameValue =
|
|
146
|
+
const classNameValue = (0, _context.getSourceCode)(context).getText(classArg).slice(1, -1);
|
|
147
147
|
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
|
|
148
148
|
return;
|
|
149
149
|
}
|
|
@@ -165,7 +165,7 @@ const create = context => ({
|
|
|
165
165
|
//[callee.object.property.name=/toHaveAttribute|toHaveProperty/][arguments.0.value=class][arguments.1.type=/Literal$/]
|
|
166
166
|
const matcher = node.callee.property;
|
|
167
167
|
const [classArg, classValueArg] = node.arguments;
|
|
168
|
-
const classNameValue =
|
|
168
|
+
const classNameValue = (0, _context.getSourceCode)(context).getText(classArg).slice(1, -1);
|
|
169
169
|
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
|
|
170
170
|
return;
|
|
171
171
|
}
|
|
@@ -187,7 +187,7 @@ const create = context => ({
|
|
|
187
187
|
const matcher = node.callee.property;
|
|
188
188
|
const [classArg, classValue] = node.arguments;
|
|
189
189
|
const classValueArg = classValue.arguments[0];
|
|
190
|
-
const classNameValue =
|
|
190
|
+
const classNameValue = (0, _context.getSourceCode)(context).getText(classArg).slice(1, -1);
|
|
191
191
|
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
|
|
192
192
|
return;
|
|
193
193
|
}
|
|
@@ -31,7 +31,7 @@ const create = context => {
|
|
|
31
31
|
return `[${(0, _context.getSourceCode)(context).getText(styleName)}]`;
|
|
32
32
|
}
|
|
33
33
|
function getReplacementStyleParam(styleName, styleValue) {
|
|
34
|
-
return styleName.type === "Literal" ? `{${camelCase(styleName.value)}: ${
|
|
34
|
+
return styleName.type === "Literal" ? `{${camelCase(styleName.value)}: ${(0, _context.getSourceCode)(context).getText(styleValue)}}` : `${(0, _context.getSourceCode)(context).getText(styleName).slice(0, -1)}: ${styleValue.type === "TemplateLiteral" ? (0, _context.getSourceCode)(context).getText(styleValue).substring(1) : `${styleValue.value}\``}`;
|
|
35
35
|
}
|
|
36
36
|
return {
|
|
37
37
|
//expect(el.style.foo).toBe("bar");
|
|
@@ -43,7 +43,7 @@ const create = context => {
|
|
|
43
43
|
node: node.property,
|
|
44
44
|
message: "Use toHaveStyle instead of asserting on element style",
|
|
45
45
|
fix(fixer) {
|
|
46
|
-
return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${
|
|
46
|
+
return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${(0, _context.getSourceCode)(context).getText(styleValue)}}`)];
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
49
|
},
|
|
@@ -56,7 +56,7 @@ const create = context => {
|
|
|
56
56
|
node: node.property,
|
|
57
57
|
message: "Use toHaveStyle instead of asserting on element style",
|
|
58
58
|
fix(fixer) {
|
|
59
|
-
return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${
|
|
59
|
+
return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${(0, _context.getSourceCode)(context).getText(styleValue)}}`)];
|
|
60
60
|
}
|
|
61
61
|
});
|
|
62
62
|
},
|
|
@@ -142,7 +142,7 @@ const create = context => {
|
|
|
142
142
|
if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
|
|
143
143
|
return null;
|
|
144
144
|
}
|
|
145
|
-
return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${getReplacementObjectProperty(styleName)}: ${
|
|
145
|
+
return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${getReplacementObjectProperty(styleName)}: ${(0, _context.getSourceCode)(context).getText(styleValue)}}`)];
|
|
146
146
|
}
|
|
147
147
|
});
|
|
148
148
|
},
|
|
@@ -157,7 +157,7 @@ const create = context => {
|
|
|
157
157
|
if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
|
|
158
158
|
return null;
|
|
159
159
|
}
|
|
160
|
-
return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${getReplacementObjectProperty(styleName)}: ${
|
|
160
|
+
return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${getReplacementObjectProperty(styleName)}: ${(0, _context.getSourceCode)(context).getText(styleValue)}}`)];
|
|
161
161
|
}
|
|
162
162
|
});
|
|
163
163
|
}
|
|
@@ -27,7 +27,7 @@ const create = context => ({
|
|
|
27
27
|
node: node.parent,
|
|
28
28
|
message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
|
|
29
29
|
fix: fixer => {
|
|
30
|
-
return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, expectedArg.type === "Literal" ? expectedArg.regex ? expectedArgSource : new RegExp(expectedArg.value.toString().replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")) : `new RegExp(${expectedArgSource})`)];
|
|
30
|
+
return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, expectedArg.type === "Literal" ? expectedArg.regex ? expectedArgSource : new RegExp(expectedArg.value.toString().replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")).toString() : `new RegExp(${expectedArgSource})`)];
|
|
31
31
|
}
|
|
32
32
|
});
|
|
33
33
|
},
|
|
@@ -51,7 +51,7 @@ const create = context => ({
|
|
|
51
51
|
context.report({
|
|
52
52
|
node: node.parent,
|
|
53
53
|
message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
|
|
54
|
-
fix: fixer => [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, expectedArg.type === "Literal" ? expectedArg.regex ? expectedArgSource : new RegExp(expectedArg.value.toString().replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")) : `new RegExp(${expectedArgSource})`)]
|
|
54
|
+
fix: fixer => [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, expectedArg.type === "Literal" ? expectedArg.regex ? expectedArgSource : new RegExp(expectedArg.value.toString().replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")).toString() : `new RegExp(${expectedArgSource})`)]
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest-dom",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.6.0",
|
|
4
4
|
"description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -101,12 +101,13 @@
|
|
|
101
101
|
"eslint-remote-tester": "^3.0.0",
|
|
102
102
|
"eslint-remote-tester-repositories": "^1.0.1",
|
|
103
103
|
"kcd-scripts": "^12.0.0",
|
|
104
|
+
"prettier": "^2.0.0",
|
|
104
105
|
"semver": "^7.6.0",
|
|
105
106
|
"typescript": "^5.1.3"
|
|
106
107
|
},
|
|
107
108
|
"peerDependencies": {
|
|
108
109
|
"@testing-library/dom": "^8.0.0 || ^9.0.0 || ^10.0.0",
|
|
109
|
-
"eslint": "^6.8.0 || ^7.0.0 || ^8.0.0 || ^9.0.0"
|
|
110
|
+
"eslint": "^6.8.0 || ^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0"
|
|
110
111
|
},
|
|
111
112
|
"peerDependenciesMeta": {
|
|
112
113
|
"@testing-library/dom": {
|