eslint-plugin-jest-dom 3.6.3 → 3.8.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
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
[![MIT License][license-badge]][license]
|
|
15
15
|
|
|
16
16
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
17
|
-
[](#contributors-)
|
|
18
18
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
19
19
|
[![PRs Welcome][prs-badge]][prs]
|
|
20
20
|
[![Code of Conduct][coc-badge]][coc]
|
|
@@ -144,26 +144,28 @@ Thanks goes to these people ([emoji key][emojis]):
|
|
|
144
144
|
<!-- markdownlint-disable -->
|
|
145
145
|
<table>
|
|
146
146
|
<tr>
|
|
147
|
-
<td align="center"><a href="https://github.com/benmonro"><img src="https://avatars3.githubusercontent.com/u/399236?v=4" width="100px;" alt=""/><br /><sub><b>Ben Monro</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=benmonro" title="Documentation">📖</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=benmonro" title="Code">💻</a> <a href="#example-benmonro" title="Examples">💡</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=benmonro" title="Tests">⚠️</a></td>
|
|
148
|
-
<td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4" width="100px;" alt=""/><br /><sub><b>Nick McCurdy</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=nickmccurdy" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=nickmccurdy" title="Documentation">📖</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=nickmccurdy" title="Tests">⚠️</a></td>
|
|
149
|
-
<td align="center"><a href="https://twitter.com/gnapse"><img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;" alt=""/><br /><sub><b>Ernesto García</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=gnapse" title="Documentation">📖</a></td>
|
|
150
|
-
<td align="center"><a href="https://chriscolborne.com"><img src="https://avatars2.githubusercontent.com/u/101371?v=4" width="100px;" alt=""/><br /><sub><b>Chris Colborne</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=zorfling" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=zorfling" title="Tests">⚠️</a></td>
|
|
151
|
-
<td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4" width="100px;" alt=""/><br /><sub><b>Michaël De Boey</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=MichaelDeBoey" title="Code">💻</a></td>
|
|
152
|
-
<td align="center"><a href="http://gerritalex.de"><img src="https://avatars1.githubusercontent.com/u/29307652?v=4" width="100px;" alt=""/><br /><sub><b>Gerrit Alex</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=ljosberinn" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=ljosberinn" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=ljosberinn" title="Documentation">📖</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3Aljosberinn" title="Bug reports">🐛</a></td>
|
|
153
|
-
<td align="center"><a href="http://ololos.space/"><img src="https://avatars1.githubusercontent.com/u/3940079?v=4" width="100px;" alt=""/><br /><sub><b>Andrey Los</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3ARIP21" title="Bug reports">🐛</a></td>
|
|
147
|
+
<td align="center"><a href="https://github.com/benmonro"><img src="https://avatars3.githubusercontent.com/u/399236?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ben Monro</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=benmonro" title="Documentation">📖</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=benmonro" title="Code">💻</a> <a href="#example-benmonro" title="Examples">💡</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=benmonro" title="Tests">⚠️</a></td>
|
|
148
|
+
<td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nick McCurdy</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=nickmccurdy" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=nickmccurdy" title="Documentation">📖</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=nickmccurdy" title="Tests">⚠️</a></td>
|
|
149
|
+
<td align="center"><a href="https://twitter.com/gnapse"><img src="https://avatars0.githubusercontent.com/u/15199?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ernesto García</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=gnapse" title="Documentation">📖</a></td>
|
|
150
|
+
<td align="center"><a href="https://chriscolborne.com"><img src="https://avatars2.githubusercontent.com/u/101371?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Chris Colborne</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=zorfling" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=zorfling" title="Tests">⚠️</a></td>
|
|
151
|
+
<td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michaël De Boey</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=MichaelDeBoey" title="Code">💻</a></td>
|
|
152
|
+
<td align="center"><a href="http://gerritalex.de"><img src="https://avatars1.githubusercontent.com/u/29307652?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gerrit Alex</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=ljosberinn" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=ljosberinn" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=ljosberinn" title="Documentation">📖</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3Aljosberinn" title="Bug reports">🐛</a></td>
|
|
153
|
+
<td align="center"><a href="http://ololos.space/"><img src="https://avatars1.githubusercontent.com/u/3940079?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Andrey Los</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3ARIP21" title="Bug reports">🐛</a></td>
|
|
154
154
|
</tr>
|
|
155
155
|
<tr>
|
|
156
|
-
<td align="center"><a href="https://skovy.dev"><img src="https://avatars1.githubusercontent.com/u/5247455?v=4" width="100px;" alt=""/><br /><sub><b>Spencer Miskoviak</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=skovy" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=skovy" title="Tests">⚠️</a></td>
|
|
157
|
-
<td align="center"><a href="https://github.com/atsikov"><img src="https://avatars3.githubusercontent.com/u/1422928?v=4" width="100px;" alt=""/><br /><sub><b>Aleksei Tsikov</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3Aatsikov" title="Bug reports">🐛</a></td>
|
|
158
|
-
<td align="center"><a href="https://mario.dev"><img src="https://avatars1.githubusercontent.com/u/2677072?v=4" width="100px;" alt=""/><br /><sub><b>Mario Beltrán Alarcón</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=Belco90" title="Documentation">📖</a></td>
|
|
159
|
-
<td align="center"><a href="https://codepen.io/ariperkkio/"><img src="https://avatars2.githubusercontent.com/u/14806298?v=4" width="100px;" alt=""/><br /><sub><b>Ari Perkkiö</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3AAriPerkkio" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AriPerkkio" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AriPerkkio" title="Tests">⚠️</a></td>
|
|
160
|
-
<td align="center"><a href="http://www.antn.se"><img src="https://avatars0.githubusercontent.com/u/785676?v=4" width="100px;" alt=""/><br /><sub><b>Anton Niklasson</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AntonNiklasson" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AntonNiklasson" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AntonNiklasson" title="Documentation">📖</a></td>
|
|
161
|
-
<td align="center"><a href="http://juzerzarif.com"><img src="https://avatars3.githubusercontent.com/u/22772637?v=4" width="100px;" alt=""/><br /><sub><b>Juzer Zarif</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=juzerzarif" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=juzerzarif" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3Ajuzerzarif" title="Bug reports">🐛</a></td>
|
|
156
|
+
<td align="center"><a href="https://skovy.dev"><img src="https://avatars1.githubusercontent.com/u/5247455?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Spencer Miskoviak</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=skovy" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=skovy" title="Tests">⚠️</a></td>
|
|
157
|
+
<td align="center"><a href="https://github.com/atsikov"><img src="https://avatars3.githubusercontent.com/u/1422928?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aleksei Tsikov</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3Aatsikov" title="Bug reports">🐛</a></td>
|
|
158
|
+
<td align="center"><a href="https://mario.dev"><img src="https://avatars1.githubusercontent.com/u/2677072?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mario Beltrán Alarcón</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=Belco90" title="Documentation">📖</a></td>
|
|
159
|
+
<td align="center"><a href="https://codepen.io/ariperkkio/"><img src="https://avatars2.githubusercontent.com/u/14806298?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ari Perkkiö</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3AAriPerkkio" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AriPerkkio" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AriPerkkio" title="Tests">⚠️</a></td>
|
|
160
|
+
<td align="center"><a href="http://www.antn.se"><img src="https://avatars0.githubusercontent.com/u/785676?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Anton Niklasson</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AntonNiklasson" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AntonNiklasson" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AntonNiklasson" title="Documentation">📖</a></td>
|
|
161
|
+
<td align="center"><a href="http://juzerzarif.com"><img src="https://avatars3.githubusercontent.com/u/22772637?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Juzer Zarif</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=juzerzarif" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=juzerzarif" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3Ajuzerzarif" title="Bug reports">🐛</a></td>
|
|
162
|
+
<td align="center"><a href="http://everlong.org/"><img src="https://avatars.githubusercontent.com/u/454175?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Julien Wajsberg</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=julienw" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=julienw" title="Tests">⚠️</a></td>
|
|
162
163
|
</tr>
|
|
163
164
|
</table>
|
|
164
165
|
|
|
165
|
-
<!-- markdownlint-
|
|
166
|
+
<!-- markdownlint-restore -->
|
|
166
167
|
<!-- prettier-ignore-end -->
|
|
168
|
+
|
|
167
169
|
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
168
170
|
|
|
169
171
|
This project follows the [all-contributors][all-contributors] specification.
|
package/dist/assignment-ast.js
CHANGED
|
@@ -5,6 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.getInnerNodeFrom = getInnerNodeFrom;
|
|
7
7
|
exports.getAssignmentForIdentifier = getAssignmentForIdentifier;
|
|
8
|
+
exports.getQueryNodeFrom = getQueryNodeFrom;
|
|
9
|
+
|
|
10
|
+
var _queries = require("./queries");
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* Gets the inner relevant node (CallExpression, Identity, et al.) given a generic expression node
|
|
@@ -15,7 +18,19 @@ exports.getAssignmentForIdentifier = getAssignmentForIdentifier;
|
|
|
15
18
|
* @returns {Object} - A node
|
|
16
19
|
*/
|
|
17
20
|
function getInnerNodeFrom(expression) {
|
|
18
|
-
|
|
21
|
+
switch (expression.type) {
|
|
22
|
+
case "TSAsExpression":
|
|
23
|
+
return getInnerNodeFrom(expression.expression);
|
|
24
|
+
|
|
25
|
+
case "AwaitExpression":
|
|
26
|
+
return getInnerNodeFrom(expression.argument);
|
|
27
|
+
|
|
28
|
+
case "MemberExpression":
|
|
29
|
+
return getInnerNodeFrom(expression.object);
|
|
30
|
+
|
|
31
|
+
default:
|
|
32
|
+
return expression;
|
|
33
|
+
}
|
|
19
34
|
}
|
|
20
35
|
/**
|
|
21
36
|
* Get the node corresponding to the latest assignment to a variable named `identifierName`
|
|
@@ -48,4 +63,36 @@ function getAssignmentForIdentifier(context, identifierName) {
|
|
|
48
63
|
}
|
|
49
64
|
|
|
50
65
|
return assignmentNode;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* get query node, arg and isDTLQuery flag for a given node. useful for rules that you only
|
|
69
|
+
* want to apply to dom elements.
|
|
70
|
+
*
|
|
71
|
+
* @param {Object} context - Context for a rule
|
|
72
|
+
* @param {Object} nodeWithValueProp - AST Node to get the query from
|
|
73
|
+
* @returns {Object} - Object with query, queryArg & isDTLQuery
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
function getQueryNodeFrom(context, nodeWithValueProp) {
|
|
78
|
+
const queryNode = nodeWithValueProp.type === "Identifier" ? getAssignmentForIdentifier(context, nodeWithValueProp.name) : getInnerNodeFrom(nodeWithValueProp);
|
|
79
|
+
|
|
80
|
+
if (!queryNode || !queryNode.callee) {
|
|
81
|
+
return {
|
|
82
|
+
isDTLQuery: false,
|
|
83
|
+
query: null,
|
|
84
|
+
queryArg: null
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const query = queryNode.callee.name || queryNode.callee.property.name;
|
|
89
|
+
const queryArg = queryNode.arguments[0] && queryNode.arguments[0].value;
|
|
90
|
+
|
|
91
|
+
const isDTLQuery = _queries.queries.includes(query);
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
queryArg,
|
|
95
|
+
query,
|
|
96
|
+
isDTLQuery
|
|
97
|
+
};
|
|
51
98
|
}
|
|
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
+
var _assignmentAst = require("./assignment-ast");
|
|
9
|
+
|
|
8
10
|
var _default = ({
|
|
9
11
|
preferred,
|
|
10
12
|
negatedPreferred,
|
|
@@ -12,7 +14,8 @@ var _default = ({
|
|
|
12
14
|
}) => context => {
|
|
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
|
|
|
15
|
-
const isBannedArg = node => attributes.some(attr => attr === node.arguments[0].value);
|
|
17
|
+
const isBannedArg = node => attributes.some(attr => attr === node.arguments[0].value); //expect(el).not.toBeEnabled() => expect(el).toBeDisabled()
|
|
18
|
+
|
|
16
19
|
|
|
17
20
|
return {
|
|
18
21
|
[`CallExpression[callee.property.name=/${preferred}|${negatedPreferred}/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](node) {
|
|
@@ -29,6 +32,7 @@ var _default = ({
|
|
|
29
32
|
});
|
|
30
33
|
},
|
|
31
34
|
|
|
35
|
+
//expect(getByText('foo').<attribute>).toBeTruthy()
|
|
32
36
|
"CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']"(node) {
|
|
33
37
|
const {
|
|
34
38
|
arguments: [{
|
|
@@ -46,6 +50,10 @@ var _default = ({
|
|
|
46
50
|
return;
|
|
47
51
|
}
|
|
48
52
|
|
|
53
|
+
const {
|
|
54
|
+
isDTLQuery
|
|
55
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
|
|
56
|
+
if (!isDTLQuery) return;
|
|
49
57
|
const isNegated = matcher.endsWith("Falsy") || (matcher === "toBe" || matcher === "toEqual") && matcherArg !== true;
|
|
50
58
|
const correctFunction = getCorrectFunctionFor(node.callee.object, isNegated);
|
|
51
59
|
context.report({
|
|
@@ -76,6 +84,10 @@ var _default = ({
|
|
|
76
84
|
return;
|
|
77
85
|
}
|
|
78
86
|
|
|
87
|
+
const {
|
|
88
|
+
isDTLQuery
|
|
89
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
|
|
90
|
+
if (!isDTLQuery) return;
|
|
79
91
|
const correctFunction = getCorrectFunctionFor(node);
|
|
80
92
|
const incorrectFunction = node.callee.property.name;
|
|
81
93
|
const message = `Use ${correctFunction}() instead of ${incorrectFunction}(${node.arguments.map(({
|
|
@@ -13,6 +13,8 @@ var _assignmentAst = require("../assignment-ast");
|
|
|
13
13
|
* @fileoverview prefer toBeInTheDocument over checking getAttribute/hasAttribute
|
|
14
14
|
* @author Anton Niklasson
|
|
15
15
|
*/
|
|
16
|
+
|
|
17
|
+
/*eslint complexity: ["error", {"max": 20}]*/
|
|
16
18
|
const meta = {
|
|
17
19
|
type: "suggestion",
|
|
18
20
|
docs: {
|
|
@@ -29,7 +31,11 @@ const meta = {
|
|
|
29
31
|
exports.meta = meta;
|
|
30
32
|
|
|
31
33
|
function isAntonymMatcher(matcherNode, matcherArguments) {
|
|
32
|
-
return matcherNode.name === "toBeNull" || usesToHaveLengthZero(matcherNode, matcherArguments);
|
|
34
|
+
return matcherNode.name === "toBeNull" || usesToBeOrToEqualWithNull(matcherNode, matcherArguments) || usesToHaveLengthZero(matcherNode, matcherArguments);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function usesToBeOrToEqualWithNull(matcherNode, matcherArguments) {
|
|
38
|
+
return (matcherNode.name === "toBe" || matcherNode.name === "toEqual") && matcherArguments[0].value === null;
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
function usesToHaveLengthZero(matcherNode, matcherArguments) {
|
|
@@ -37,7 +43,7 @@ function usesToHaveLengthZero(matcherNode, matcherArguments) {
|
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
const create = context => {
|
|
40
|
-
const alternativeMatchers =
|
|
46
|
+
const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual)$/;
|
|
41
47
|
|
|
42
48
|
function getLengthValue(matcherArguments) {
|
|
43
49
|
let lengthValue;
|
|
@@ -76,6 +82,13 @@ const create = context => {
|
|
|
76
82
|
if (isValidUseOfToHaveLength) {
|
|
77
83
|
return;
|
|
78
84
|
}
|
|
85
|
+
} // toBe() or toEqual() are only invalid with null
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
if (matcherNode.name === "toBe" || matcherNode.name === "toEqual") {
|
|
89
|
+
if (!usesToBeOrToEqualWithNull(matcherNode, matcherArguments)) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
79
92
|
}
|
|
80
93
|
|
|
81
94
|
const query = queryNode.name || queryNode.property.name;
|
|
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.create = exports.meta = void 0;
|
|
7
7
|
|
|
8
|
+
var _assignmentAst = require("../assignment-ast");
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* @fileoverview prefer toHaveClass over checking element className
|
|
10
12
|
* @author Ben Monro
|
|
@@ -82,7 +84,11 @@ const create = context => ({
|
|
|
82
84
|
const checkedProp = node.callee.object.arguments[0].property;
|
|
83
85
|
const [classValue] = node.arguments;
|
|
84
86
|
const matcher = node.callee.property;
|
|
85
|
-
const classNameProp = node.callee.object.arguments[0].object;
|
|
87
|
+
const classNameProp = node.callee.object.arguments[0].object;
|
|
88
|
+
const {
|
|
89
|
+
isDTLQuery
|
|
90
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp);
|
|
91
|
+
if (!isDTLQuery) return; // don't report here if using `expect.foo()`
|
|
86
92
|
|
|
87
93
|
if (classValue.type === "CallExpression" && classValue.callee.type === "MemberExpression" && classValue.callee.object.name === "expect") return;
|
|
88
94
|
context.report({
|
|
@@ -104,6 +110,10 @@ const create = context => ({
|
|
|
104
110
|
const matcher = node.callee.property;
|
|
105
111
|
const classNameProp = node.callee.object.arguments[0].object;
|
|
106
112
|
const matcherArg = node.arguments[0].callee.property;
|
|
113
|
+
const {
|
|
114
|
+
isDTLQuery
|
|
115
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp);
|
|
116
|
+
if (!isDTLQuery) return;
|
|
107
117
|
context.report({
|
|
108
118
|
node: matcher,
|
|
109
119
|
messageId,
|
|
@@ -122,6 +132,10 @@ const create = context => ({
|
|
|
122
132
|
const [classValue] = node.arguments;
|
|
123
133
|
const matcher = node.callee.property;
|
|
124
134
|
const classNameProp = node.callee.object.object.arguments[0].object;
|
|
135
|
+
const {
|
|
136
|
+
isDTLQuery
|
|
137
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp);
|
|
138
|
+
if (!isDTLQuery) return;
|
|
125
139
|
context.report({
|
|
126
140
|
node: matcher,
|
|
127
141
|
messageId,
|
|
@@ -141,6 +155,10 @@ const create = context => ({
|
|
|
141
155
|
const [classArg, classValueArg] = node.arguments;
|
|
142
156
|
const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
|
|
143
157
|
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
|
|
158
|
+
const {
|
|
159
|
+
isDTLQuery
|
|
160
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
|
|
161
|
+
if (!isDTLQuery) return;
|
|
144
162
|
context.report({
|
|
145
163
|
node: matcher,
|
|
146
164
|
messageId,
|
|
@@ -160,6 +178,10 @@ const create = context => ({
|
|
|
160
178
|
const [classArg, classValueArg] = node.arguments;
|
|
161
179
|
const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
|
|
162
180
|
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
|
|
181
|
+
const {
|
|
182
|
+
isDTLQuery
|
|
183
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.object.arguments[0]);
|
|
184
|
+
if (!isDTLQuery) return;
|
|
163
185
|
context.report({
|
|
164
186
|
node: matcher,
|
|
165
187
|
messageId,
|
|
@@ -179,6 +201,10 @@ const create = context => ({
|
|
|
179
201
|
const classValueArg = classValue.arguments[0];
|
|
180
202
|
const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
|
|
181
203
|
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
|
|
204
|
+
const {
|
|
205
|
+
isDTLQuery
|
|
206
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
|
|
207
|
+
if (!isDTLQuery) return;
|
|
182
208
|
context.report({
|
|
183
209
|
node: matcher,
|
|
184
210
|
messageId,
|
|
@@ -5,8 +5,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.create = exports.meta = void 0;
|
|
7
7
|
|
|
8
|
-
var _queries = require("../queries");
|
|
9
|
-
|
|
10
8
|
var _assignmentAst = require("../assignment-ast");
|
|
11
9
|
|
|
12
10
|
/**
|
|
@@ -32,26 +30,13 @@ exports.meta = meta;
|
|
|
32
30
|
const messageId = "use-to-have-value";
|
|
33
31
|
|
|
34
32
|
const create = context => {
|
|
35
|
-
function
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const query = queryNode.callee.name || queryNode.callee.property.name;
|
|
46
|
-
const queryArg = queryNode.arguments[0] && queryNode.arguments[0].value;
|
|
47
|
-
|
|
48
|
-
const isValidQuery = _queries.queries.includes(query);
|
|
49
|
-
|
|
50
|
-
const isValidElement = query.match(/^(get|find|query)ByRole$/) && ["textbox", "dropdown"].includes(queryArg);
|
|
51
|
-
return {
|
|
52
|
-
isValidQuery,
|
|
53
|
-
isValidElement
|
|
54
|
-
};
|
|
33
|
+
function isValidQueryNode(nodeWithValueProp) {
|
|
34
|
+
const {
|
|
35
|
+
query,
|
|
36
|
+
queryArg,
|
|
37
|
+
isDTLQuery
|
|
38
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, nodeWithValueProp);
|
|
39
|
+
return !!query && isDTLQuery && !!query.match(/^(get|find|query)(All)?ByRole$/) && ["textbox", "dropdown"].includes(queryArg);
|
|
55
40
|
}
|
|
56
41
|
|
|
57
42
|
return {
|
|
@@ -62,24 +47,16 @@ const create = context => {
|
|
|
62
47
|
const valueProp = node.callee.object.arguments[0].property;
|
|
63
48
|
const matcher = node.callee.property;
|
|
64
49
|
const queryNode = node.callee.object.arguments[0].object;
|
|
65
|
-
const {
|
|
66
|
-
isValidQuery,
|
|
67
|
-
isValidElement
|
|
68
|
-
} = validateQueryNode(queryNode);
|
|
69
|
-
|
|
70
|
-
function fix(fixer) {
|
|
71
|
-
return [fixer.remove(context.getSourceCode().getTokenBefore(valueProp)), fixer.remove(valueProp), fixer.replaceText(matcher, "toHaveValue")];
|
|
72
|
-
}
|
|
73
50
|
|
|
74
|
-
if (
|
|
51
|
+
if (isValidQueryNode(queryNode)) {
|
|
75
52
|
context.report({
|
|
76
53
|
messageId,
|
|
77
54
|
node,
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
55
|
+
|
|
56
|
+
fix(fixer) {
|
|
57
|
+
return [fixer.remove(context.getSourceCode().getTokenBefore(valueProp)), fixer.remove(valueProp), fixer.replaceText(matcher, "toHaveValue")];
|
|
58
|
+
}
|
|
59
|
+
|
|
83
60
|
});
|
|
84
61
|
}
|
|
85
62
|
},
|
|
@@ -91,24 +68,16 @@ const create = context => {
|
|
|
91
68
|
const queryNode = node.callee.object.object.arguments[0].object;
|
|
92
69
|
const valueProp = node.callee.object.object.arguments[0].property;
|
|
93
70
|
const matcher = node.callee.property;
|
|
94
|
-
const {
|
|
95
|
-
isValidQuery,
|
|
96
|
-
isValidElement
|
|
97
|
-
} = validateQueryNode(queryNode);
|
|
98
|
-
|
|
99
|
-
function fix(fixer) {
|
|
100
|
-
return [fixer.removeRange([context.getSourceCode().getTokenBefore(valueProp).range[0], valueProp.range[1]]), fixer.replaceText(matcher, "toHaveValue")];
|
|
101
|
-
}
|
|
102
71
|
|
|
103
|
-
if (
|
|
72
|
+
if (isValidQueryNode(queryNode)) {
|
|
104
73
|
context.report({
|
|
105
74
|
messageId,
|
|
106
75
|
node,
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
76
|
+
|
|
77
|
+
fix(fixer) {
|
|
78
|
+
return [fixer.removeRange([context.getSourceCode().getTokenBefore(valueProp).range[0], valueProp.range[1]]), fixer.replaceText(matcher, "toHaveValue")];
|
|
79
|
+
}
|
|
80
|
+
|
|
112
81
|
});
|
|
113
82
|
}
|
|
114
83
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest-dom",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.0",
|
|
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": [
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@typescript-eslint/parser": "^4.8.2",
|
|
49
|
-
"eslint": "7.
|
|
49
|
+
"eslint": "7.23",
|
|
50
50
|
"eslint-remote-tester": "^0.3.3",
|
|
51
51
|
"jest-extended": "^0.11.5",
|
|
52
52
|
"kcd-scripts": "6.5.1",
|