eslint-plugin-jest-dom 2.0.0 → 3.0.1
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/LICENSE +20 -0
- package/README.md +137 -50
- package/dist/createBannedAttributeRule.js +93 -0
- package/dist/index.js +39 -0
- package/dist/rules/prefer-checked.js +31 -0
- package/dist/rules/prefer-empty.js +109 -0
- package/dist/rules/prefer-enabled-disabled.js +31 -0
- package/dist/rules/prefer-focus.js +79 -0
- package/dist/rules/prefer-required.js +31 -0
- package/dist/rules/prefer-to-have-attribute.js +93 -0
- package/dist/rules/prefer-to-have-text-content.js +58 -0
- package/package.json +42 -29
- package/.all-contributorsrc +0 -46
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -22
- package/.prettierrc.json +0 -4
- package/.travis.yml +0 -19
- package/build/generate-readme-table.js +0 -57
- package/docs/rules/prefer-checked.md +0 -73
- package/docs/rules/prefer-empty.md +0 -60
- package/docs/rules/prefer-enabled-disabled.md +0 -76
- package/docs/rules/prefer-focus.md +0 -49
- package/docs/rules/prefer-required.md +0 -74
- package/docs/rules/prefer-to-have-attribute.md +0 -42
- package/jest.config.js +0 -14
- package/lib/createBannedAttributeRule.js +0 -114
- package/lib/index.js +0 -35
- package/lib/rules/prefer-checked.js +0 -25
- package/lib/rules/prefer-empty.js +0 -170
- package/lib/rules/prefer-enabled-disabled.js +0 -26
- package/lib/rules/prefer-focus.js +0 -127
- package/lib/rules/prefer-required.js +0 -25
- package/lib/rules/prefer-to-have-attribute.js +0 -171
- package/tests/__snapshots__/index.test.js.snap +0 -75
- package/tests/fixtures/createBannedAttributeTestCases.js +0 -159
- package/tests/index.test.js +0 -14
- package/tests/lib/rules/no-attribute-checking.js +0 -52
- package/tests/lib/rules/prefer-empty.js +0 -222
- package/tests/lib/rules/prefer-focus.js +0 -138
- package/tests/lib/rules/prefer-to-have-attribute.js +0 -191
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2019 Ben Monro
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,62 +1,96 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>eslint-plugin-jest-dom</h1>
|
|
3
|
+
|
|
4
|
+
<p>ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom.</p>
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
<!-- prettier-ignore-start -->
|
|
10
|
+
[![Build Status][build-badge]][build]
|
|
11
|
+
[![Code Coverage][coverage-badge]][coverage]
|
|
3
12
|
[![version][version-badge]][package]
|
|
4
|
-
[![
|
|
13
|
+
[![downloads][downloads-badge]][npmtrends]
|
|
14
|
+
[![MIT License][license-badge]][license]
|
|
5
15
|
|
|
6
|
-
|
|
16
|
+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
17
|
+
[](#contributors-)
|
|
18
|
+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
19
|
+
[![PRs Welcome][prs-badge]][prs]
|
|
20
|
+
[![Code of Conduct][coc-badge]][coc]
|
|
21
|
+
<!-- prettier-ignore-end -->
|
|
7
22
|
|
|
8
|
-
##
|
|
23
|
+
## Table of Contents
|
|
9
24
|
|
|
10
|
-
|
|
25
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
26
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
11
27
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
28
|
+
- [Installation](#installation)
|
|
29
|
+
- [Usage](#usage)
|
|
30
|
+
- [Recommended Configuration](#recommended-configuration)
|
|
31
|
+
- [Supported Rules](#supported-rules)
|
|
32
|
+
- [Issues](#issues)
|
|
33
|
+
- [🐛 Bugs](#-bugs)
|
|
34
|
+
- [💡 Feature Requests](#-feature-requests)
|
|
35
|
+
- [Contributors ✨](#contributors-)
|
|
36
|
+
- [LICENSE](#license)
|
|
15
37
|
|
|
16
|
-
|
|
38
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
This module is distributed via [npm][npm] which is bundled with [node][node] and
|
|
43
|
+
should be installed as one of your project's `devDependencies`:
|
|
17
44
|
|
|
18
45
|
```
|
|
19
|
-
|
|
46
|
+
npm install --save-dev eslint-plugin-jest-dom
|
|
20
47
|
```
|
|
21
48
|
|
|
22
|
-
|
|
49
|
+
This library has a required `peerDependencies` listing for [`eslint`][eslint]
|
|
23
50
|
|
|
24
51
|
## Usage
|
|
25
52
|
|
|
26
|
-
Add `jest-dom` to the plugins section of your `.eslintrc` configuration file.
|
|
53
|
+
Add `jest-dom` to the plugins section of your `.eslintrc.js` configuration file.
|
|
54
|
+
You can omit the `eslint-plugin-` prefix:
|
|
27
55
|
|
|
28
|
-
```
|
|
29
|
-
{
|
|
30
|
-
|
|
31
|
-
|
|
56
|
+
```javascript
|
|
57
|
+
module.exports = {
|
|
58
|
+
plugins: ["jest-dom"],
|
|
59
|
+
rules: {
|
|
60
|
+
// your configuration
|
|
61
|
+
},
|
|
62
|
+
};
|
|
32
63
|
```
|
|
33
64
|
|
|
34
65
|
Then configure the rules you want to use under the rules section.
|
|
35
66
|
|
|
36
|
-
```
|
|
37
|
-
{
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
}
|
|
67
|
+
```javascript
|
|
68
|
+
module.exports = {
|
|
69
|
+
rules: {
|
|
70
|
+
"jest-dom/prefer-checked": "error",
|
|
71
|
+
"jest-dom/prefer-enabled-disabled": "error",
|
|
72
|
+
"jest-dom/prefer-required": "error",
|
|
73
|
+
"jest-dom/prefer-to-have-attribute": "error",
|
|
74
|
+
},
|
|
75
|
+
};
|
|
45
76
|
```
|
|
46
77
|
|
|
47
78
|
## Recommended Configuration
|
|
48
79
|
|
|
49
|
-
This plugin exports a recommended configuration that enforces good
|
|
50
|
-
|
|
51
|
-
|
|
80
|
+
This plugin exports a recommended configuration that enforces good `jest-dom`
|
|
81
|
+
practices _(you can find more info about enabled rules in
|
|
82
|
+
[Supported Rules section](#supported-rules))_.
|
|
52
83
|
|
|
53
|
-
To enable this configuration use the `extends` property in your
|
|
54
|
-
|
|
84
|
+
To enable this configuration use the `extends` property in your `.eslintrc.js`
|
|
85
|
+
config file:
|
|
55
86
|
|
|
56
|
-
```
|
|
57
|
-
{
|
|
58
|
-
|
|
59
|
-
|
|
87
|
+
```javascript
|
|
88
|
+
module.exports = {
|
|
89
|
+
extends: "plugin:jest-dom/recommended",
|
|
90
|
+
rules: {
|
|
91
|
+
// your configuration
|
|
92
|
+
},
|
|
93
|
+
};
|
|
60
94
|
```
|
|
61
95
|
|
|
62
96
|
## Supported Rules
|
|
@@ -66,33 +100,86 @@ To enable this configuration use the `extends` property in your
|
|
|
66
100
|
🛠 indicates that a rule is fixable.
|
|
67
101
|
|
|
68
102
|
<!-- __BEGIN AUTOGENERATED TABLE__ -->
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
[prefer-
|
|
73
|
-
[prefer-
|
|
74
|
-
[prefer-
|
|
75
|
-
[prefer-
|
|
76
|
-
[prefer-
|
|
103
|
+
|
|
104
|
+
| Name | ✔️ | 🛠 | Description |
|
|
105
|
+
| ---------------------------------------------------------------------------------------------------------------------------------------------- | --- | --- | -------------------------------------------------------------- |
|
|
106
|
+
| [prefer-checked](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-checked.md) | ✔️ | 🛠 | prefer toBeChecked over checking attributes |
|
|
107
|
+
| [prefer-empty](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-empty.md) | ✔️ | 🛠 | Prefer toBeEmpty over checking innerHTML |
|
|
108
|
+
| [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 |
|
|
109
|
+
| [prefer-focus](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-focus.md) | ✔️ | 🛠 | prefer toHaveFocus over checking document.activeElement |
|
|
110
|
+
| [prefer-required](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-required.md) | ✔️ | 🛠 | prefer toBeRequired over checking properties |
|
|
111
|
+
| [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 |
|
|
112
|
+
| [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 |
|
|
113
|
+
|
|
77
114
|
<!-- __END AUTOGENERATED TABLE__ -->
|
|
78
115
|
|
|
116
|
+
## Issues
|
|
117
|
+
|
|
118
|
+
_Looking to contribute? Look for the [Good First Issue][good-first-issue]
|
|
119
|
+
label._
|
|
120
|
+
|
|
121
|
+
### 🐛 Bugs
|
|
122
|
+
|
|
123
|
+
Please file an issue for bugs, missing documentation, or unexpected behavior.
|
|
124
|
+
|
|
125
|
+
[**See Bugs**][bugs]
|
|
126
|
+
|
|
127
|
+
### 💡 Feature Requests
|
|
128
|
+
|
|
129
|
+
Please file an issue to suggest new features. Vote on feature requests by adding
|
|
130
|
+
a 👍. This helps maintainers prioritize what to work on.
|
|
131
|
+
|
|
132
|
+
[**See Feature Requests**][requests]
|
|
133
|
+
|
|
79
134
|
## Contributors ✨
|
|
80
135
|
|
|
81
|
-
Thanks goes to these
|
|
136
|
+
Thanks goes to these people ([emoji key][emojis]):
|
|
82
137
|
|
|
83
138
|
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
84
|
-
<!-- prettier-ignore -->
|
|
139
|
+
<!-- prettier-ignore-start -->
|
|
140
|
+
<!-- markdownlint-disable -->
|
|
85
141
|
<table>
|
|
86
142
|
<tr>
|
|
87
|
-
<td align="center"><a href="https://github.com/benmonro"><img src="https://avatars3.githubusercontent.com/u/399236?v=4" width="100px;" alt="
|
|
88
|
-
<td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4" width="100px;" alt="
|
|
89
|
-
<td align="center"><a href="https://twitter.com/gnapse"><img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;" alt="
|
|
143
|
+
<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>
|
|
144
|
+
<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>
|
|
145
|
+
<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>
|
|
146
|
+
<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>
|
|
147
|
+
<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>
|
|
90
148
|
</tr>
|
|
91
149
|
</table>
|
|
92
150
|
|
|
151
|
+
<!-- markdownlint-enable -->
|
|
152
|
+
<!-- prettier-ignore-end -->
|
|
153
|
+
|
|
93
154
|
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
94
155
|
|
|
95
|
-
This project follows the [all-contributors]
|
|
156
|
+
This project follows the [all-contributors][all-contributors] specification.
|
|
157
|
+
Contributions of any kind welcome!
|
|
96
158
|
|
|
97
|
-
|
|
159
|
+
## LICENSE
|
|
160
|
+
|
|
161
|
+
MIT
|
|
162
|
+
|
|
163
|
+
<!-- prettier-ignore-start -->
|
|
164
|
+
[npm]: https://www.npmjs.com
|
|
165
|
+
[node]: https://nodejs.org
|
|
166
|
+
[build-badge]: https://img.shields.io/travis/testing-library/eslint-plugin-jest-dom.svg?style=flat-square
|
|
167
|
+
[build]: https://travis-ci.org/testing-library/eslint-plugin-jest-dom
|
|
168
|
+
[coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/eslint-plugin-jest-dom.svg?style=flat-square
|
|
169
|
+
[coverage]: https://codecov.io/github/testing-library/eslint-plugin-jest-dom
|
|
98
170
|
[version-badge]: https://img.shields.io/npm/v/eslint-plugin-jest-dom.svg?style=flat-square
|
|
171
|
+
[package]: https://www.npmjs.com/package/eslint-plugin-jest-dom
|
|
172
|
+
[downloads-badge]: https://img.shields.io/npm/dm/eslint-plugin-jest-dom.svg?style=flat-square
|
|
173
|
+
[npmtrends]: http://www.npmtrends.com/eslint-plugin-jest-dom
|
|
174
|
+
[license-badge]: https://img.shields.io/npm/l/eslint-plugin-jest-dom.svg?style=flat-square
|
|
175
|
+
[license]: https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/LICENSE
|
|
176
|
+
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
|
|
177
|
+
[prs]: http://makeapullrequest.com
|
|
178
|
+
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
|
|
179
|
+
[coc]: https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/other/CODE_OF_CONDUCT.md
|
|
180
|
+
[emojis]: https://github.com/all-contributors/all-contributors#emoji-key
|
|
181
|
+
[all-contributors]: https://github.com/all-contributors/all-contributors
|
|
182
|
+
[bugs]: https://github.com/testing-library/eslint-plugin-jest-dom/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug
|
|
183
|
+
[requests]: https://github.com/testing-library/eslint-plugin-jest-dom/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
|
|
184
|
+
[good-first-issue]: https://github.com/testing-library/eslint-plugin-jest-dom/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement+label%3A%22good+first+issue%22
|
|
185
|
+
<!-- prettier-ignore-end -->
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _default = ({
|
|
9
|
+
preferred,
|
|
10
|
+
negatedPreferred,
|
|
11
|
+
attributes
|
|
12
|
+
}) => context => {
|
|
13
|
+
const getCorrectFunctionFor = (node, negated = false) => (node.arguments.length === 1 || node.arguments[1].value === true || typeof node.arguments[1].value === "string" && node.arguments[1].value.toLowerCase() === "true" || node.arguments[1].value === "") && !negated ? preferred : negatedPreferred;
|
|
14
|
+
|
|
15
|
+
const isBannedArg = node => attributes.some(attr => attr === node.arguments[0].value);
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
[`CallExpression[callee.property.name=/${preferred}|${negatedPreferred}/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](node) {
|
|
19
|
+
if (!negatedPreferred.startsWith("toBe")) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const incorrectFunction = node.callee.property.name;
|
|
24
|
+
const correctFunction = incorrectFunction === preferred ? negatedPreferred : preferred;
|
|
25
|
+
context.report({
|
|
26
|
+
message: `Use ${correctFunction}() instead of not.${incorrectFunction}()`,
|
|
27
|
+
node,
|
|
28
|
+
fix: fixer => fixer.replaceTextRange([node.callee.object.property.range[0], node.range[1]], `${correctFunction}()`)
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
[`CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']`](node) {
|
|
33
|
+
const {
|
|
34
|
+
arguments: [{
|
|
35
|
+
property,
|
|
36
|
+
property: {
|
|
37
|
+
name
|
|
38
|
+
} = {}
|
|
39
|
+
}]
|
|
40
|
+
} = node.callee.object;
|
|
41
|
+
const matcher = node.callee.property.name;
|
|
42
|
+
const matcherArg = node.arguments.length && node.arguments[0].value;
|
|
43
|
+
|
|
44
|
+
if (!attributes.some(attr => attr === name)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const isNegated = matcher.endsWith("Falsy") || (matcher === "toBe" || matcher === "toEqual") && matcherArg !== true;
|
|
49
|
+
const correctFunction = getCorrectFunctionFor(node.callee.object, isNegated);
|
|
50
|
+
context.report({
|
|
51
|
+
node,
|
|
52
|
+
message: `Use ${correctFunction}() instead of checking .${name} directly`,
|
|
53
|
+
fix: fixer => [fixer.removeRange([property.range[0] - 1, property.range[1]]), fixer.replaceTextRange([node.callee.property.range[0], node.range[1]], `${correctFunction}()`)]
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
[`CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](node) {
|
|
58
|
+
const arg = node.arguments[0].value;
|
|
59
|
+
|
|
60
|
+
if (!isBannedArg(node)) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const correctFunction = getCorrectFunctionFor(node, true);
|
|
65
|
+
const incorrectFunction = node.callee.property.name;
|
|
66
|
+
context.report({
|
|
67
|
+
message: `Use ${correctFunction}() instead of not.${incorrectFunction}('${arg}')`,
|
|
68
|
+
node,
|
|
69
|
+
fix: fixer => fixer.replaceTextRange([node.callee.object.property.range[0], node.range[1]], `${correctFunction}()`)
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
[`CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]`](node) {
|
|
74
|
+
if (!isBannedArg(node)) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const correctFunction = getCorrectFunctionFor(node);
|
|
79
|
+
const incorrectFunction = node.callee.property.name;
|
|
80
|
+
const message = `Use ${correctFunction}() instead of ${incorrectFunction}(${node.arguments.map(({
|
|
81
|
+
raw
|
|
82
|
+
}) => raw).join(", ")})`;
|
|
83
|
+
context.report({
|
|
84
|
+
node: node.callee.property,
|
|
85
|
+
message,
|
|
86
|
+
fix: fixer => [fixer.replaceTextRange([node.callee.property.range[0], node.range[1]], `${correctFunction}()`)]
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
exports.default = _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.configs = exports.generateRecommendedConfig = exports.rules = void 0;
|
|
9
|
+
|
|
10
|
+
var _requireindex = _interopRequireDefault(require("requireindex"));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @fileoverview lint rules for use with jest-dom
|
|
14
|
+
* @author Ben Monro
|
|
15
|
+
*/
|
|
16
|
+
//------------------------------------------------------------------------------
|
|
17
|
+
// Requirements
|
|
18
|
+
//------------------------------------------------------------------------------
|
|
19
|
+
//------------------------------------------------------------------------------
|
|
20
|
+
// Plugin Definition
|
|
21
|
+
//------------------------------------------------------------------------------
|
|
22
|
+
// import all rules in src/rules
|
|
23
|
+
const rules = (0, _requireindex.default)(`${__dirname}/rules`);
|
|
24
|
+
exports.rules = rules;
|
|
25
|
+
|
|
26
|
+
const generateRecommendedConfig = allRules => Object.entries(allRules).reduce((memo, [name, rule]) => ({ ...memo,
|
|
27
|
+
...(rule.meta.docs.recommended ? {
|
|
28
|
+
[`jest-dom/${name}`]: "error"
|
|
29
|
+
} : {})
|
|
30
|
+
}), {});
|
|
31
|
+
|
|
32
|
+
exports.generateRecommendedConfig = generateRecommendedConfig;
|
|
33
|
+
const configs = {
|
|
34
|
+
recommended: {
|
|
35
|
+
plugins: ["jest-dom"],
|
|
36
|
+
rules: generateRecommendedConfig(rules)
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
exports.configs = configs;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.create = exports.meta = void 0;
|
|
9
|
+
|
|
10
|
+
var _createBannedAttributeRule = _interopRequireDefault(require("../createBannedAttributeRule"));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks
|
|
14
|
+
* @author Ben Monro
|
|
15
|
+
*/
|
|
16
|
+
const meta = {
|
|
17
|
+
docs: {
|
|
18
|
+
description: "prefer toBeChecked over checking attributes",
|
|
19
|
+
category: "jest-dom",
|
|
20
|
+
recommended: true,
|
|
21
|
+
url: "prefer-checked"
|
|
22
|
+
},
|
|
23
|
+
fixable: "code"
|
|
24
|
+
};
|
|
25
|
+
exports.meta = meta;
|
|
26
|
+
const create = (0, _createBannedAttributeRule.default)({
|
|
27
|
+
preferred: "toBeChecked",
|
|
28
|
+
negatedPreferred: "not.toBeChecked",
|
|
29
|
+
attributes: ["checked", "aria-checked"]
|
|
30
|
+
});
|
|
31
|
+
exports.create = create;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.create = exports.meta = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @fileoverview Prefer toBeEmpty over checking innerHTML
|
|
10
|
+
* @author Ben Monro
|
|
11
|
+
*/
|
|
12
|
+
const meta = {
|
|
13
|
+
docs: {
|
|
14
|
+
description: "Prefer toBeEmpty over checking innerHTML",
|
|
15
|
+
category: "jest-dom",
|
|
16
|
+
recommended: true,
|
|
17
|
+
url: "prefer-empty"
|
|
18
|
+
},
|
|
19
|
+
fixable: "code" // or "code" or "whitespace"
|
|
20
|
+
|
|
21
|
+
};
|
|
22
|
+
exports.meta = meta;
|
|
23
|
+
|
|
24
|
+
const create = context => ({
|
|
25
|
+
[`BinaryExpression[left.property.name='innerHTML'][right.value=''][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
|
|
26
|
+
context.report({
|
|
27
|
+
node,
|
|
28
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
29
|
+
fix: fixer => [fixer.removeRange([node.left.property.range[0] - 1, node.range[1]]), fixer.replaceText(node.parent.parent.property, Boolean(node.parent.parent.parent.arguments[0].value) === node.operator.startsWith("=") // binary expression XNOR matcher boolean
|
|
30
|
+
? "toBeEmpty" : "not.toBeEmpty"), fixer.remove(node.parent.parent.parent.arguments[0])]
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
[`BinaryExpression[left.property.name='firstChild'][right.value=null][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
|
|
35
|
+
context.report({
|
|
36
|
+
node,
|
|
37
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
38
|
+
fix: fixer => [fixer.removeRange([node.left.property.range[0] - 1, node.range[1]]), fixer.replaceText(node.parent.parent.property, Boolean(node.parent.parent.parent.arguments[0].value) === node.operator.startsWith("=") // binary expression XNOR matcher boolean
|
|
39
|
+
? "toBeEmpty" : "not.toBeEmpty"), fixer.remove(node.parent.parent.parent.arguments[0])]
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
[`MemberExpression[property.name = 'innerHTML'][parent.callee.name = 'expect'][parent.parent.property.name = /toBe$|to(Strict)?Equal/]`](node) {
|
|
44
|
+
if (node.parent.parent.parent.arguments[0].value) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
context.report({
|
|
49
|
+
node,
|
|
50
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
51
|
+
fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceText(node.parent.parent.property, "toBeEmpty"), fixer.remove(node.parent.parent.parent.arguments[0])]
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
[`MemberExpression[property.name='innerHTML'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal$/][parent.parent.object.callee.name='expect']`](node) {
|
|
56
|
+
if (node.parent.parent.parent.parent.arguments[0].value) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
context.report({
|
|
61
|
+
node,
|
|
62
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
63
|
+
fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceText(node.parent.parent.parent.property, "toBeEmpty"), fixer.remove(node.parent.parent.parent.parent.arguments[0])]
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
[`MemberExpression[property.name = 'firstChild'][parent.callee.name = 'expect'][parent.parent.property.name = /toBeNull$/]`](node) {
|
|
68
|
+
context.report({
|
|
69
|
+
node,
|
|
70
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
71
|
+
fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceText(node.parent.parent.property, "toBeEmpty")]
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
[`MemberExpression[property.name='firstChild'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal$/][parent.parent.object.callee.name='expect']`](node) {
|
|
76
|
+
if (node.parent.parent.parent.parent.arguments[0].value !== null) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
context.report({
|
|
81
|
+
node,
|
|
82
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
83
|
+
fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceText(node.parent.parent.parent.property, "toBeEmpty"), fixer.remove(node.parent.parent.parent.parent.arguments[0])]
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
[`MemberExpression[property.name='firstChild'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBeNull$/][parent.parent.object.callee.name='expect']`](node) {
|
|
88
|
+
context.report({
|
|
89
|
+
node,
|
|
90
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
91
|
+
fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceText(node.parent.parent.parent.property, "toBeEmpty")]
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
[`MemberExpression[property.name = 'firstChild'][parent.callee.name = 'expect'][parent.parent.property.name = /toBe$|to(Strict)?Equal/]`](node) {
|
|
96
|
+
if (node.parent.parent.parent.arguments[0].value !== null) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
context.report({
|
|
101
|
+
node,
|
|
102
|
+
message: "Use toBeEmpty instead of checking inner html.",
|
|
103
|
+
fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceText(node.parent.parent.property, "toBeEmpty"), fixer.remove(node.parent.parent.parent.arguments[0])]
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
exports.create = create;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.create = exports.meta = void 0;
|
|
9
|
+
|
|
10
|
+
var _createBannedAttributeRule = _interopRequireDefault(require("../createBannedAttributeRule"));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks
|
|
14
|
+
* @author Ben Monro
|
|
15
|
+
*/
|
|
16
|
+
const meta = {
|
|
17
|
+
docs: {
|
|
18
|
+
description: "prefer toBeDisabled or toBeEnabled over checking attributes",
|
|
19
|
+
category: "jest-dom",
|
|
20
|
+
recommended: true,
|
|
21
|
+
url: "prefer-enabled-disabled"
|
|
22
|
+
},
|
|
23
|
+
fixable: "code"
|
|
24
|
+
};
|
|
25
|
+
exports.meta = meta;
|
|
26
|
+
const create = (0, _createBannedAttributeRule.default)({
|
|
27
|
+
preferred: "toBeDisabled",
|
|
28
|
+
negatedPreferred: "toBeEnabled",
|
|
29
|
+
attributes: ["disabled"]
|
|
30
|
+
});
|
|
31
|
+
exports.create = create;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.create = exports.meta = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @fileoverview prefer toHaveFocus over checking activeElementa
|
|
10
|
+
* @author Ben Monro
|
|
11
|
+
*/
|
|
12
|
+
const variantsOfDoc = [// document:
|
|
13
|
+
`[object.name=document]`, // window.document || global.document:
|
|
14
|
+
`[object.object.name=/(global|window)$/][object.property.name=document]`, // global.window.document:
|
|
15
|
+
`[object.object.object.name='global'][object.object.property.name='window'][object.property.name=document]`];
|
|
16
|
+
const meta = {
|
|
17
|
+
docs: {
|
|
18
|
+
description: "prefer toHaveFocus over checking document.activeElement",
|
|
19
|
+
category: "jest-dom",
|
|
20
|
+
recommended: true
|
|
21
|
+
},
|
|
22
|
+
fixable: "code"
|
|
23
|
+
};
|
|
24
|
+
exports.meta = meta;
|
|
25
|
+
|
|
26
|
+
const create = context => ({
|
|
27
|
+
[variantsOfDoc.map(variant => `MemberExpression${variant}[property.name='activeElement'][parent.parent.object.callee.name='expect'][parent.parent.property.name='not'][parent.parent.parent.property.name=/to(Be|(Strict)?Equal)$/]`).join(", ")](node) {
|
|
28
|
+
const element = node.parent.parent.parent.parent.callee.parent.arguments[0];
|
|
29
|
+
const matcher = node.parent.parent.parent.parent.callee.property;
|
|
30
|
+
context.report({
|
|
31
|
+
node: node.parent,
|
|
32
|
+
message: `Use toHaveFocus instead of checking activeElement`,
|
|
33
|
+
fix: fixer => {
|
|
34
|
+
if (element.name) {
|
|
35
|
+
return [fixer.replaceText(node, element.name), fixer.remove(element), fixer.replaceText(matcher, "toHaveFocus")];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return [fixer.removeRange([node.range[0], element.range[0]]), fixer.insertTextAfterRange([element.range[1], element.range[1] + 1], ".not.toHaveFocus()")];
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
[variantsOfDoc.map(variant => `MemberExpression${variant}[property.name='activeElement'][parent.callee.object.object.callee.name='expect'][parent.callee.property.name=/to(Be|(Strict)?Equal)$/]`).join(", ")](node) {
|
|
44
|
+
const matcher = node.parent.callee.property;
|
|
45
|
+
context.report({
|
|
46
|
+
node: node.parent,
|
|
47
|
+
message: `Use toHaveFocus instead of checking activeElement`,
|
|
48
|
+
fix: fixer => [fixer.remove(node), fixer.replaceText(matcher, "toHaveFocus")]
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
[variantsOfDoc.map(variant => `MemberExpression${variant}[property.name='activeElement'][parent.callee.name='expect'][parent.parent.property.name=/to(Be|(Strict)?Equal)$/]`).join(", ")](node) {
|
|
53
|
+
const element = node.parent.parent.parent.arguments[0];
|
|
54
|
+
const matcher = node.parent.parent.property;
|
|
55
|
+
context.report({
|
|
56
|
+
node: node.parent,
|
|
57
|
+
message: `Use toHaveFocus instead of checking activeElement`,
|
|
58
|
+
fix: fixer => {
|
|
59
|
+
if (!element.name) {
|
|
60
|
+
return [fixer.removeRange([node.range[0], element.range[0]]), fixer.insertTextAfterRange([element.range[1], element.range[1] + 1], ".toHaveFocus()")];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return [fixer.replaceText(node, element.name), fixer.remove(element), fixer.replaceText(matcher, "toHaveFocus")];
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
[variantsOfDoc.map(variant => `MemberExpression${variant}[property.name='activeElement'][parent.callee.object.callee.name='expect'][parent.callee.property.name=/to(Be|(Strict)?Equal)$/]`).join(", ")](node) {
|
|
69
|
+
const matcher = node.parent.callee.property;
|
|
70
|
+
context.report({
|
|
71
|
+
node: node.parent,
|
|
72
|
+
message: `Use toHaveFocus instead of checking activeElement`,
|
|
73
|
+
fix: fixer => [fixer.remove(node), fixer.replaceText(matcher, "toHaveFocus")]
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
exports.create = create;
|