eslint-plugin-jest-dom 1.4.1 → 2.1.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/.all-contributorsrc +12 -1
- package/.eslintrc.json +3 -4
- package/.travis.yml +1 -1
- package/README.md +10 -4
- package/build/generate-readme-table.js +14 -14
- package/docs/rules/prefer-empty.md +60 -0
- package/docs/rules/prefer-to-have-text-content.md +40 -0
- package/jest.config.js +5 -5
- package/lib/createBannedAttributeRule.js +18 -18
- package/lib/index.js +7 -7
- package/lib/rules/prefer-checked.js +10 -10
- package/lib/rules/prefer-empty.js +191 -0
- package/lib/rules/prefer-enabled-disabled.js +10 -10
- package/lib/rules/prefer-focus.js +43 -24
- package/lib/rules/prefer-required.js +10 -10
- package/lib/rules/prefer-to-have-attribute.js +35 -35
- package/lib/rules/prefer-to-have-text-content.js +110 -0
- package/package.json +4 -3
- package/tests/__snapshots__/index.test.js.snap +22 -0
- package/tests/fixtures/createBannedAttributeTestCases.js +51 -51
- package/tests/index.test.js +5 -5
- package/tests/lib/rules/no-attribute-checking.js +18 -18
- package/tests/lib/rules/prefer-empty.js +222 -0
- package/tests/lib/rules/prefer-focus.js +69 -51
- package/tests/lib/rules/prefer-to-have-attribute.js +63 -63
- package/tests/lib/rules/prefer-to-have-text-content.js +118 -0
- package/.prettierrc.json +0 -4
package/.all-contributorsrc
CHANGED
|
@@ -36,11 +36,22 @@
|
|
|
36
36
|
"contributions": [
|
|
37
37
|
"doc"
|
|
38
38
|
]
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"login": "zorfling",
|
|
42
|
+
"name": "Chris Colborne",
|
|
43
|
+
"avatar_url": "https://avatars2.githubusercontent.com/u/101371?v=4",
|
|
44
|
+
"profile": "https://chriscolborne.com",
|
|
45
|
+
"contributions": [
|
|
46
|
+
"code",
|
|
47
|
+
"test"
|
|
48
|
+
]
|
|
39
49
|
}
|
|
40
50
|
],
|
|
41
51
|
"contributorsPerLine": 7,
|
|
42
52
|
"projectName": "eslint-plugin-jest-dom",
|
|
43
53
|
"projectOwner": "testing-library",
|
|
44
54
|
"repoType": "github",
|
|
45
|
-
"repoHost": "https://github.com"
|
|
55
|
+
"repoHost": "https://github.com",
|
|
56
|
+
"skipCi": true
|
|
46
57
|
}
|
package/.eslintrc.json
CHANGED
|
@@ -6,17 +6,16 @@
|
|
|
6
6
|
"commonjs": true,
|
|
7
7
|
"es6": true,
|
|
8
8
|
"node": true
|
|
9
|
-
// "jest/globals": true
|
|
10
9
|
},
|
|
11
10
|
"plugins": [
|
|
12
11
|
"eslint-plugin",
|
|
13
12
|
"prettier"
|
|
14
13
|
],
|
|
15
14
|
"extends": [
|
|
16
|
-
"plugin:eslint-plugin/recommended"
|
|
15
|
+
"plugin:eslint-plugin/recommended",
|
|
16
|
+
"prettier"
|
|
17
17
|
],
|
|
18
18
|
"rules": {
|
|
19
|
-
"prettier/prettier": "error",
|
|
20
19
|
"no-var": "error"
|
|
21
20
|
}
|
|
22
|
-
}
|
|
21
|
+
}
|
package/.travis.yml
CHANGED
package/README.md
CHANGED
|
@@ -69,10 +69,12 @@ To enable this configuration use the `extends` property in your
|
|
|
69
69
|
Name | ✔️ | 🛠 | Description
|
|
70
70
|
----- | ----- | ----- | -----
|
|
71
71
|
[prefer-checked](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-checked.md) | ✔️ | 🛠 | prefer toBeChecked over checking attributes
|
|
72
|
+
[prefer-empty](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-empty.md) | ✔️ | 🛠 | Prefer toBeEmpty over checking innerHTML
|
|
72
73
|
[prefer-enabled-disabled](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-enabled-disabled.md) | ✔️ | 🛠 | prefer toBeDisabled or toBeEnabled over checking attributes
|
|
73
74
|
[prefer-focus](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-focus.md) | ✔️ | 🛠 | prefer toHaveFocus over checking document.activeElement
|
|
74
75
|
[prefer-required](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-required.md) | ✔️ | 🛠 | prefer toBeRequired over checking properties
|
|
75
76
|
[prefer-to-have-attribute](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-attribute.md) | ✔️ | 🛠 | prefer toHaveAttribute over checking getAttribute/hasAttribute
|
|
77
|
+
[prefer-to-have-text-content](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-text-content.md) | ✔️ | 🛠 | Prefer toHaveTextContent over checking element.textContent
|
|
76
78
|
<!-- __END AUTOGENERATED TABLE__ -->
|
|
77
79
|
|
|
78
80
|
## Contributors ✨
|
|
@@ -80,15 +82,19 @@ Name | ✔️ | 🛠 | Description
|
|
|
80
82
|
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
81
83
|
|
|
82
84
|
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
83
|
-
<!-- prettier-ignore -->
|
|
85
|
+
<!-- prettier-ignore-start -->
|
|
86
|
+
<!-- markdownlint-disable -->
|
|
84
87
|
<table>
|
|
85
88
|
<tr>
|
|
86
|
-
<td align="center"><a href="https://github.com/benmonro"><img src="https://avatars3.githubusercontent.com/u/399236?v=4" width="100px;" alt="
|
|
87
|
-
<td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4" width="100px;" alt="
|
|
88
|
-
<td align="center"><a href="https://twitter.com/gnapse"><img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;" alt="
|
|
89
|
+
<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>
|
|
90
|
+
<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>
|
|
91
|
+
<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>
|
|
92
|
+
<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>
|
|
89
93
|
</tr>
|
|
90
94
|
</table>
|
|
91
95
|
|
|
96
|
+
<!-- markdownlint-enable -->
|
|
97
|
+
<!-- prettier-ignore-end -->
|
|
92
98
|
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
93
99
|
|
|
94
100
|
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
const fs = require(
|
|
4
|
-
const path = require(
|
|
5
|
-
const rules = require(
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const rules = require("..").rules;
|
|
6
6
|
|
|
7
|
-
const README_LOCATION = path.resolve(__dirname,
|
|
8
|
-
const BEGIN_TABLE_MARKER =
|
|
9
|
-
const END_TABLE_MARKER =
|
|
7
|
+
const README_LOCATION = path.resolve(__dirname, "..", "README.md");
|
|
8
|
+
const BEGIN_TABLE_MARKER = "<!-- __BEGIN AUTOGENERATED TABLE__ -->\n";
|
|
9
|
+
const END_TABLE_MARKER = "\n<!-- __END AUTOGENERATED TABLE__ -->";
|
|
10
10
|
|
|
11
11
|
const expectedTableLines = Object.keys(rules)
|
|
12
12
|
.sort()
|
|
@@ -17,19 +17,19 @@ const expectedTableLines = Object.keys(rules)
|
|
|
17
17
|
lines.push(
|
|
18
18
|
[
|
|
19
19
|
`[${ruleId}](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/${ruleId}.md)`,
|
|
20
|
-
rule.meta.docs.recommended ?
|
|
21
|
-
rule.meta.fixable ?
|
|
22
|
-
rule.meta.docs.description
|
|
23
|
-
].join(
|
|
20
|
+
rule.meta.docs.recommended ? "✔️" : "",
|
|
21
|
+
rule.meta.fixable ? "🛠" : "",
|
|
22
|
+
rule.meta.docs.description
|
|
23
|
+
].join(" | ")
|
|
24
24
|
);
|
|
25
25
|
|
|
26
26
|
return lines;
|
|
27
27
|
},
|
|
28
|
-
[
|
|
28
|
+
["Name | ✔️ | 🛠 | Description", "----- | ----- | ----- | -----"]
|
|
29
29
|
)
|
|
30
|
-
.join(
|
|
30
|
+
.join("\n");
|
|
31
31
|
|
|
32
|
-
const readmeContents = fs.readFileSync(README_LOCATION,
|
|
32
|
+
const readmeContents = fs.readFileSync(README_LOCATION, "utf8");
|
|
33
33
|
|
|
34
34
|
if (!readmeContents.includes(BEGIN_TABLE_MARKER)) {
|
|
35
35
|
throw new Error(
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Prefer toBeEmpty over checking innerHTML / firstChild (prefer-empty)
|
|
2
|
+
|
|
3
|
+
This rule ensures people will use toBeEmpty() rather than checking dom nodes/properties. It is primarily aimed at consistently using jest-dom for readability.
|
|
4
|
+
|
|
5
|
+
## Rule Details
|
|
6
|
+
|
|
7
|
+
This autofixable rule aims to ensure usage of `.toBeEmpty()`
|
|
8
|
+
|
|
9
|
+
Examples of **incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
|
|
13
|
+
expect(element.innerHTML).toBe('foo');
|
|
14
|
+
expect(element.innerHTML).not.toBe('foo');
|
|
15
|
+
expect(element.firstChild).toBe('foo');
|
|
16
|
+
expect(element.firstChild).not.toBe('foo');
|
|
17
|
+
expect(getByText("foo").innerHTML).toBe('foo');
|
|
18
|
+
expect(getByText("foo").innerHTML).not.toBe('foo');
|
|
19
|
+
expect(getByText("foo").firstChild).toBe('foo');
|
|
20
|
+
expect(getByText("foo").firstChild).not.toBe('foo');
|
|
21
|
+
expect(element.innerHTML === '').toBe(true);
|
|
22
|
+
expect(element.innerHTML !== '').toBe(true);
|
|
23
|
+
expect(element.innerHTML === '').toBe(false);
|
|
24
|
+
expect(element.innerHTML !== '').toBe(false);
|
|
25
|
+
expect(element.firstChild === null).toBe(true);
|
|
26
|
+
expect(element.firstChild !== null).toBe(false);
|
|
27
|
+
expect(element.firstChild === null).toBe(false);
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Examples of **correct** code for this rule:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
expect(element.innerHTML).toBe('');
|
|
35
|
+
expect(element.innerHTML).toBe(null);
|
|
36
|
+
expect(element.innerHTML).not.toBe(null);
|
|
37
|
+
expect(element.innerHTML).not.toBe('');
|
|
38
|
+
expect(element.firstChild).toBeNull();
|
|
39
|
+
expect(element.firstChild).toBe(null);
|
|
40
|
+
expect(element.firstChild).not.toBe(null);
|
|
41
|
+
expect(element.firstChild).not.toBeNull();
|
|
42
|
+
expect(getByText('foo').innerHTML).toBe('');
|
|
43
|
+
expect(getByText('foo').innerHTML).toStrictEqual('');
|
|
44
|
+
expect(getByText('foo').innerHTML).toStrictEqual(null);
|
|
45
|
+
expect(getByText('foo').firstChild).toBe(null);
|
|
46
|
+
expect(getByText('foo').firstChild).not.toBe(null);
|
|
47
|
+
expect(element.innerHTML === 'foo').toBe(true);
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## When Not To Use It
|
|
53
|
+
|
|
54
|
+
Don't use this rule if you don't care if people use `.toBeEmpty()`.
|
|
55
|
+
|
|
56
|
+
## Further Reading
|
|
57
|
+
|
|
58
|
+
<https://github.com/testing-library/jest-dom#tobeempty>
|
|
59
|
+
|
|
60
|
+
<https://github.com/testing-library/jest-dom/blob/master/src/to-be-empty.js>
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Prefer toHaveTextContent over checking element.textContent (prefer-to-have-text-content)
|
|
2
|
+
|
|
3
|
+
Please describe the origin of the rule here.
|
|
4
|
+
|
|
5
|
+
## Rule Details
|
|
6
|
+
|
|
7
|
+
This rule aims to prevent checking of the `textContent` DOM node attribute in tests and prefer `toHaveTextContent` instead.
|
|
8
|
+
|
|
9
|
+
Examples of **incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
expect(element.textContent).toBe("foo")
|
|
13
|
+
expect(container.firstChild.textContent).toBe("foo")
|
|
14
|
+
expect(getByText('foo').textContent).toBe("foo")
|
|
15
|
+
expect(screen.getByText('foo').textContent).toBe("foo")
|
|
16
|
+
expect(element.textContent).toEqual("foo")
|
|
17
|
+
expect(element.textContent).toContain("foo")
|
|
18
|
+
expect(element.textContent).not.toBe("foo")
|
|
19
|
+
expect(element.textContent).not.toContain("foo")
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Examples of **correct** code for this rule:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
expect(string).toBe("foo"),
|
|
29
|
+
expect(element).toHaveTextContent("foo"),
|
|
30
|
+
expect(container.lastNode).toBe("foo")
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## When Not To Use It
|
|
35
|
+
|
|
36
|
+
Don't use this rule if you don't care about the added readability and improvements that `toHaveTextContent` offers to your expects.
|
|
37
|
+
|
|
38
|
+
## Further Reading
|
|
39
|
+
|
|
40
|
+
* [Docs on toHaveContent](https://github.com/testing-library/jest-dom#tohavetextcontent)
|
package/jest.config.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
testMatch: [
|
|
2
|
+
testMatch: ["**/tests/**/*.js"],
|
|
3
3
|
collectCoverage: true,
|
|
4
4
|
coverageThreshold: {
|
|
5
5
|
global: {
|
|
6
6
|
branches: 96.55,
|
|
7
7
|
functions: 100,
|
|
8
8
|
lines: 98.97,
|
|
9
|
-
statements: 0
|
|
10
|
-
}
|
|
9
|
+
statements: 0
|
|
10
|
+
}
|
|
11
11
|
},
|
|
12
|
-
testPathIgnorePatterns: [
|
|
13
|
-
collectCoverageFrom: [
|
|
12
|
+
testPathIgnorePatterns: ["<rootDir>/tests/fixtures/"],
|
|
13
|
+
collectCoverageFrom: ["lib/**/*.js", "!**/node_modules/**"]
|
|
14
14
|
};
|
|
@@ -2,7 +2,7 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
|
|
|
2
2
|
function getCorrectFunctionFor(node, negated = false) {
|
|
3
3
|
return (node.arguments.length === 1 ||
|
|
4
4
|
node.arguments[1].value === true ||
|
|
5
|
-
node.arguments[1].value ===
|
|
5
|
+
node.arguments[1].value === "") &&
|
|
6
6
|
!negated
|
|
7
7
|
? preferred
|
|
8
8
|
: negatedPreferred;
|
|
@@ -15,7 +15,7 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
|
|
|
15
15
|
[`CallExpression[callee.property.name=/${preferred}|${negatedPreferred}/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](
|
|
16
16
|
node
|
|
17
17
|
) {
|
|
18
|
-
if (negatedPreferred.startsWith(
|
|
18
|
+
if (negatedPreferred.startsWith("toBe")) {
|
|
19
19
|
const incorrectFunction = node.callee.property.name;
|
|
20
20
|
|
|
21
21
|
const correctFunction =
|
|
@@ -25,10 +25,10 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
|
|
|
25
25
|
node,
|
|
26
26
|
fix(fixer) {
|
|
27
27
|
return fixer.replaceTextRange(
|
|
28
|
-
[node.callee.object.property.
|
|
28
|
+
[node.callee.object.property.range[0], node.range[1]],
|
|
29
29
|
`${correctFunction}()`
|
|
30
30
|
);
|
|
31
|
-
}
|
|
31
|
+
}
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
34
|
},
|
|
@@ -37,14 +37,14 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
|
|
|
37
37
|
node
|
|
38
38
|
) {
|
|
39
39
|
const {
|
|
40
|
-
arguments: [{ property, property: { name } = {} }]
|
|
40
|
+
arguments: [{ property, property: { name } = {} }]
|
|
41
41
|
} = node.callee.object;
|
|
42
42
|
const matcher = node.callee.property.name;
|
|
43
43
|
const matcherArg = node.arguments.length && node.arguments[0].value;
|
|
44
44
|
if (attributes.some(attr => attr === name)) {
|
|
45
45
|
const isNegated =
|
|
46
|
-
matcher.endsWith(
|
|
47
|
-
((matcher ===
|
|
46
|
+
matcher.endsWith("Falsy") ||
|
|
47
|
+
((matcher === "toBe" || matcher === "toEqual") &&
|
|
48
48
|
matcherArg !== true);
|
|
49
49
|
const correctFunction = getCorrectFunctionFor(
|
|
50
50
|
node.callee.object,
|
|
@@ -55,13 +55,13 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
|
|
|
55
55
|
message: `Use ${correctFunction}() instead of checking .${name} directly`,
|
|
56
56
|
fix(fixer) {
|
|
57
57
|
return [
|
|
58
|
-
fixer.removeRange([property.
|
|
58
|
+
fixer.removeRange([property.range[0] - 1, property.range[1]]),
|
|
59
59
|
fixer.replaceTextRange(
|
|
60
|
-
[node.callee.property.
|
|
60
|
+
[node.callee.property.range[0], node.range[1]],
|
|
61
61
|
`${correctFunction}()`
|
|
62
|
-
)
|
|
62
|
+
)
|
|
63
63
|
];
|
|
64
|
-
}
|
|
64
|
+
}
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
},
|
|
@@ -78,10 +78,10 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
|
|
|
78
78
|
node,
|
|
79
79
|
fix(fixer) {
|
|
80
80
|
return fixer.replaceTextRange(
|
|
81
|
-
[node.callee.object.property.
|
|
81
|
+
[node.callee.object.property.range[0], node.range[1]],
|
|
82
82
|
`${correctFunction}()`
|
|
83
83
|
);
|
|
84
|
-
}
|
|
84
|
+
}
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
87
|
},
|
|
@@ -95,20 +95,20 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
|
|
|
95
95
|
|
|
96
96
|
const message = `Use ${correctFunction}() instead of ${incorrectFunction}(${node.arguments
|
|
97
97
|
.map(({ raw }) => raw)
|
|
98
|
-
.join(
|
|
98
|
+
.join(", ")})`;
|
|
99
99
|
context.report({
|
|
100
100
|
node: node.callee.property,
|
|
101
101
|
message,
|
|
102
102
|
fix(fixer) {
|
|
103
103
|
return [
|
|
104
104
|
fixer.replaceTextRange(
|
|
105
|
-
[node.callee.property.
|
|
105
|
+
[node.callee.property.range[0], node.range[1]],
|
|
106
106
|
`${correctFunction}()`
|
|
107
|
-
)
|
|
107
|
+
)
|
|
108
108
|
];
|
|
109
|
-
}
|
|
109
|
+
}
|
|
110
110
|
});
|
|
111
111
|
}
|
|
112
|
-
}
|
|
112
|
+
}
|
|
113
113
|
};
|
|
114
114
|
};
|
package/lib/index.js
CHANGED
|
@@ -2,26 +2,26 @@
|
|
|
2
2
|
* @fileoverview lint rules for use with jest-dom
|
|
3
3
|
* @author Ben Monro
|
|
4
4
|
*/
|
|
5
|
-
|
|
5
|
+
"use strict";
|
|
6
6
|
|
|
7
7
|
//------------------------------------------------------------------------------
|
|
8
8
|
// Requirements
|
|
9
9
|
//------------------------------------------------------------------------------
|
|
10
10
|
|
|
11
|
-
let requireIndex = require(
|
|
11
|
+
let requireIndex = require("requireindex");
|
|
12
12
|
|
|
13
13
|
//------------------------------------------------------------------------------
|
|
14
14
|
// Plugin Definition
|
|
15
15
|
//------------------------------------------------------------------------------
|
|
16
16
|
|
|
17
17
|
// import all rules in lib/rules
|
|
18
|
-
module.exports.rules = requireIndex(__dirname +
|
|
18
|
+
module.exports.rules = requireIndex(__dirname + "/rules");
|
|
19
19
|
|
|
20
20
|
module.exports.generateRecommendedConfig = rules => {
|
|
21
21
|
return Object.entries(rules).reduce(
|
|
22
22
|
(memo, [name, rule]) =>
|
|
23
23
|
rule.meta.docs.recommended
|
|
24
|
-
? { ...memo, [`jest-dom/${name}`]:
|
|
24
|
+
? { ...memo, [`jest-dom/${name}`]: "error" }
|
|
25
25
|
: memo,
|
|
26
26
|
{}
|
|
27
27
|
);
|
|
@@ -29,7 +29,7 @@ module.exports.generateRecommendedConfig = rules => {
|
|
|
29
29
|
|
|
30
30
|
module.exports.configs = {
|
|
31
31
|
recommended: {
|
|
32
|
-
plugins: [
|
|
33
|
-
rules: module.exports.generateRecommendedConfig(module.exports.rules)
|
|
34
|
-
}
|
|
32
|
+
plugins: ["jest-dom"],
|
|
33
|
+
rules: module.exports.generateRecommendedConfig(module.exports.rules)
|
|
34
|
+
}
|
|
35
35
|
};
|
|
@@ -2,24 +2,24 @@
|
|
|
2
2
|
* @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks
|
|
3
3
|
* @author Ben Monro
|
|
4
4
|
*/
|
|
5
|
-
|
|
5
|
+
"use strict";
|
|
6
6
|
|
|
7
|
-
const createBannedAttributeRule = require(
|
|
7
|
+
const createBannedAttributeRule = require("../createBannedAttributeRule");
|
|
8
8
|
|
|
9
9
|
module.exports = {
|
|
10
10
|
meta: {
|
|
11
11
|
docs: {
|
|
12
|
-
description:
|
|
13
|
-
category:
|
|
12
|
+
description: "prefer toBeChecked over checking attributes",
|
|
13
|
+
category: "jest-dom",
|
|
14
14
|
recommended: true,
|
|
15
|
-
url:
|
|
15
|
+
url: "prefer-checked"
|
|
16
16
|
},
|
|
17
|
-
fixable:
|
|
17
|
+
fixable: "code"
|
|
18
18
|
},
|
|
19
19
|
|
|
20
20
|
create: createBannedAttributeRule({
|
|
21
|
-
preferred:
|
|
22
|
-
negatedPreferred:
|
|
23
|
-
attributes: [
|
|
24
|
-
})
|
|
21
|
+
preferred: "toBeChecked",
|
|
22
|
+
negatedPreferred: "not.toBeChecked",
|
|
23
|
+
attributes: ["checked", "aria-checked"]
|
|
24
|
+
})
|
|
25
25
|
};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Prefer toBeEmpty over checking innerHTML
|
|
3
|
+
* @author Ben Monro
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
meta: {
|
|
8
|
+
docs: {
|
|
9
|
+
description: "Prefer toBeEmpty over checking innerHTML",
|
|
10
|
+
category: "jest-dom",
|
|
11
|
+
recommended: true,
|
|
12
|
+
url: "prefer-empty"
|
|
13
|
+
},
|
|
14
|
+
fixable: "code" // or "code" or "whitespace"
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
create: function(context) {
|
|
18
|
+
return {
|
|
19
|
+
[`BinaryExpression[left.property.name='innerHTML'][right.value=''][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](
|
|
20
|
+
node
|
|
21
|
+
) {
|
|
22
|
+
context.report({
|
|
23
|
+
node,
|
|
24
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
25
|
+
fix(fixer) {
|
|
26
|
+
return [
|
|
27
|
+
fixer.removeRange([
|
|
28
|
+
node.left.property.range[0] - 1,
|
|
29
|
+
node.range[1]
|
|
30
|
+
]),
|
|
31
|
+
fixer.replaceText(
|
|
32
|
+
node.parent.parent.property,
|
|
33
|
+
!!node.parent.parent.parent.arguments[0].value ===
|
|
34
|
+
node.operator.startsWith("=") // binary expression XNOR matcher boolean
|
|
35
|
+
? "toBeEmpty"
|
|
36
|
+
: "not.toBeEmpty"
|
|
37
|
+
),
|
|
38
|
+
fixer.remove(node.parent.parent.parent.arguments[0])
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
[`BinaryExpression[left.property.name='firstChild'][right.value=null][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](
|
|
44
|
+
node
|
|
45
|
+
) {
|
|
46
|
+
context.report({
|
|
47
|
+
node,
|
|
48
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
49
|
+
fix(fixer) {
|
|
50
|
+
return [
|
|
51
|
+
fixer.removeRange([
|
|
52
|
+
node.left.property.range[0] - 1,
|
|
53
|
+
node.range[1]
|
|
54
|
+
]),
|
|
55
|
+
fixer.replaceText(
|
|
56
|
+
node.parent.parent.property,
|
|
57
|
+
!!node.parent.parent.parent.arguments[0].value ===
|
|
58
|
+
node.operator.startsWith("=") // binary expression XNOR matcher boolean
|
|
59
|
+
? "toBeEmpty"
|
|
60
|
+
: "not.toBeEmpty"
|
|
61
|
+
),
|
|
62
|
+
fixer.remove(node.parent.parent.parent.arguments[0])
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
[`MemberExpression[property.name = 'innerHTML'][parent.callee.name = 'expect'][parent.parent.property.name = /toBe$|to(Strict)?Equal/]`](
|
|
68
|
+
node
|
|
69
|
+
) {
|
|
70
|
+
if (!node.parent.parent.parent.arguments[0].value) {
|
|
71
|
+
context.report({
|
|
72
|
+
node,
|
|
73
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
74
|
+
fix(fixer) {
|
|
75
|
+
return [
|
|
76
|
+
fixer.removeRange([
|
|
77
|
+
node.property.range[0] - 1,
|
|
78
|
+
node.property.range[1]
|
|
79
|
+
]),
|
|
80
|
+
fixer.replaceText(node.parent.parent.property, "toBeEmpty"),
|
|
81
|
+
fixer.remove(node.parent.parent.parent.arguments[0])
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
[`MemberExpression[property.name='innerHTML'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal$/][parent.parent.object.callee.name='expect']`](
|
|
88
|
+
node
|
|
89
|
+
) {
|
|
90
|
+
if (!node.parent.parent.parent.parent.arguments[0].value) {
|
|
91
|
+
context.report({
|
|
92
|
+
node,
|
|
93
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
94
|
+
fix(fixer) {
|
|
95
|
+
return [
|
|
96
|
+
fixer.removeRange([
|
|
97
|
+
node.property.range[0] - 1,
|
|
98
|
+
node.property.range[1]
|
|
99
|
+
]),
|
|
100
|
+
fixer.replaceText(
|
|
101
|
+
node.parent.parent.parent.property,
|
|
102
|
+
"toBeEmpty"
|
|
103
|
+
),
|
|
104
|
+
fixer.remove(node.parent.parent.parent.parent.arguments[0])
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
[`MemberExpression[property.name = 'firstChild'][parent.callee.name = 'expect'][parent.parent.property.name = /toBeNull$/]`](
|
|
111
|
+
node
|
|
112
|
+
) {
|
|
113
|
+
context.report({
|
|
114
|
+
node,
|
|
115
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
116
|
+
fix(fixer) {
|
|
117
|
+
return [
|
|
118
|
+
fixer.removeRange([
|
|
119
|
+
node.property.range[0] - 1,
|
|
120
|
+
node.property.range[1]
|
|
121
|
+
]),
|
|
122
|
+
fixer.replaceText(node.parent.parent.property, "toBeEmpty")
|
|
123
|
+
];
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
[`MemberExpression[property.name='firstChild'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal$/][parent.parent.object.callee.name='expect']`](
|
|
129
|
+
node
|
|
130
|
+
) {
|
|
131
|
+
if (node.parent.parent.parent.parent.arguments[0].value === null) {
|
|
132
|
+
context.report({
|
|
133
|
+
node,
|
|
134
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
135
|
+
fix(fixer) {
|
|
136
|
+
return [
|
|
137
|
+
fixer.removeRange([
|
|
138
|
+
node.property.range[0] - 1,
|
|
139
|
+
node.property.range[1]
|
|
140
|
+
]),
|
|
141
|
+
fixer.replaceText(
|
|
142
|
+
node.parent.parent.parent.property,
|
|
143
|
+
"toBeEmpty"
|
|
144
|
+
),
|
|
145
|
+
fixer.remove(node.parent.parent.parent.parent.arguments[0])
|
|
146
|
+
];
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
[`MemberExpression[property.name='firstChild'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBeNull$/][parent.parent.object.callee.name='expect']`](
|
|
153
|
+
node
|
|
154
|
+
) {
|
|
155
|
+
context.report({
|
|
156
|
+
node,
|
|
157
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
158
|
+
fix(fixer) {
|
|
159
|
+
return [
|
|
160
|
+
fixer.removeRange([
|
|
161
|
+
node.property.range[0] - 1,
|
|
162
|
+
node.property.range[1]
|
|
163
|
+
]),
|
|
164
|
+
fixer.replaceText(node.parent.parent.parent.property, "toBeEmpty")
|
|
165
|
+
];
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
[`MemberExpression[property.name = 'firstChild'][parent.callee.name = 'expect'][parent.parent.property.name = /toBe$|to(Strict)?Equal/]`](
|
|
170
|
+
node
|
|
171
|
+
) {
|
|
172
|
+
if (node.parent.parent.parent.arguments[0].value === null) {
|
|
173
|
+
context.report({
|
|
174
|
+
node,
|
|
175
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
176
|
+
fix(fixer) {
|
|
177
|
+
return [
|
|
178
|
+
fixer.removeRange([
|
|
179
|
+
node.property.range[0] - 1,
|
|
180
|
+
node.property.range[1]
|
|
181
|
+
]),
|
|
182
|
+
fixer.replaceText(node.parent.parent.property, "toBeEmpty"),
|
|
183
|
+
fixer.remove(node.parent.parent.parent.arguments[0])
|
|
184
|
+
];
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
};
|