eslint-plugin-jest-dom 3.8.0 → 3.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md 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
- [![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors-)
17
+ [![All Contributors](https://img.shields.io/badge/all_contributors-17-orange.svg?style=flat-square)](#contributors-)
18
18
  <!-- ALL-CONTRIBUTORS-BADGE:END -->
19
19
  [![PRs Welcome][prs-badge]][prs]
20
20
  [![Code of Conduct][coc-badge]][coc]
@@ -46,7 +46,7 @@ should be installed as one of your project's `devDependencies`:
46
46
  npm install --save-dev eslint-plugin-jest-dom
47
47
  ```
48
48
 
49
- This library has a required `peerDependencies` listing for [`eslint`][eslint]
49
+ This library has a required `peerDependencies` listing for [`ESLint`](https://eslint.org/).
50
50
 
51
51
  ## Usage
52
52
 
@@ -103,17 +103,17 @@ module.exports = {
103
103
 
104
104
  | Name | 👍 | 🔧 | Description |
105
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-in-document](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-in-document.md) | 👍 | 🔧 | Prefer .toBeInTheDocument() for asserting the existence of a DOM node |
111
- | [prefer-required](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-required.md) | 👍 | 🔧 | prefer toBeRequired over checking properties |
112
- | [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 |
113
- | [prefer-to-have-class](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-class.md) | 👍 | 🔧 | prefer toHaveClass over checking element className |
114
- | [prefer-to-have-style](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-style.md) | 👍 | 🔧 | prefer toHaveStyle over checking element style |
115
- | [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 |
116
- | [prefer-to-have-value](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-value.md) | 👍 | 🔧 | prefer toHaveValue over checking element.value |
106
+ | [prefer-checked](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/docs/rules/prefer-checked.md) | 👍 | 🔧 | prefer toBeChecked over checking attributes |
107
+ | [prefer-empty](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/docs/rules/prefer-empty.md) | 👍 | 🔧 | Prefer toBeEmpty over checking innerHTML |
108
+ | [prefer-enabled-disabled](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/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/main/docs/rules/prefer-focus.md) | 👍 | 🔧 | prefer toHaveFocus over checking document.activeElement |
110
+ | [prefer-in-document](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/docs/rules/prefer-in-document.md) | 👍 | 🔧 | Prefer .toBeInTheDocument() for asserting the existence of a DOM node |
111
+ | [prefer-required](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/docs/rules/prefer-required.md) | 👍 | 🔧 | prefer toBeRequired over checking properties |
112
+ | [prefer-to-have-attribute](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/docs/rules/prefer-to-have-attribute.md) | 👍 | 🔧 | prefer toHaveAttribute over checking getAttribute/hasAttribute |
113
+ | [prefer-to-have-class](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/docs/rules/prefer-to-have-class.md) | 👍 | 🔧 | prefer toHaveClass over checking element className |
114
+ | [prefer-to-have-style](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/docs/rules/prefer-to-have-style.md) | 👍 | 🔧 | prefer toHaveStyle over checking element style |
115
+ | [prefer-to-have-text-content](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/docs/rules/prefer-to-have-text-content.md) | 👍 | 🔧 | Prefer toHaveTextContent over checking element.textContent |
116
+ | [prefer-to-have-value](https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/docs/rules/prefer-to-have-value.md) | 👍 | 🔧 | prefer toHaveValue over checking element.value |
117
117
 
118
118
  <!-- __END AUTOGENERATED TABLE__ -->
119
119
 
@@ -161,6 +161,11 @@ Thanks goes to these people ([emoji key][emojis]):
161
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
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>
163
163
  </tr>
164
+ <tr>
165
+ <td align="center"><a href="https://github.com/G-Rath"><img src="https://avatars.githubusercontent.com/u/3151613?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gareth Jones</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=G-Rath" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=G-Rath" title="Code">💻</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3AG-Rath" title="Bug reports">🐛</a></td>
166
+ <td align="center"><a href="https://github.com/huyenltnguyen"><img src="https://avatars.githubusercontent.com/u/25715018?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Huyen Nguyen</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=huyenltnguyen" title="Documentation">📖</a></td>
167
+ <td align="center"><a href="https://github.com/mdotwills"><img src="https://avatars.githubusercontent.com/u/5505611?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Matthew</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3Amdotwills" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=mdotwills" title="Code">💻</a></td>
168
+ </tr>
164
169
  </table>
165
170
 
166
171
  <!-- markdownlint-restore -->
@@ -187,11 +192,11 @@ MIT
187
192
  [downloads-badge]: https://img.shields.io/npm/dm/eslint-plugin-jest-dom.svg?style=flat-square
188
193
  [npmtrends]: http://www.npmtrends.com/eslint-plugin-jest-dom
189
194
  [license-badge]: https://img.shields.io/npm/l/eslint-plugin-jest-dom.svg?style=flat-square
190
- [license]: https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/LICENSE
195
+ [license]: https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/LICENSE
191
196
  [prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
192
197
  [prs]: http://makeapullrequest.com
193
198
  [coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
194
- [coc]: https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/other/CODE_OF_CONDUCT.md
199
+ [coc]: https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/other/CODE_OF_CONDUCT.md
195
200
  [emojis]: https://github.com/all-contributors/all-contributors#emoji-key
196
201
  [all-contributors]: https://github.com/all-contributors/all-contributors
197
202
  [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
@@ -14,7 +14,7 @@ var _default = ({
14
14
  }) => context => {
15
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;
16
16
 
17
- const isBannedArg = node => attributes.some(attr => attr === node.arguments[0].value); //expect(el).not.toBeEnabled() => expect(el).toBeDisabled()
17
+ const isBannedArg = node => node.arguments.length && attributes.some(attr => attr === node.arguments[0].value); //expect(el).not.toBeEnabled() => expect(el).toBeDisabled()
18
18
 
19
19
 
20
20
  return {
@@ -34,6 +34,10 @@ var _default = ({
34
34
 
35
35
  //expect(getByText('foo').<attribute>).toBeTruthy()
36
36
  "CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']"(node) {
37
+ if (!node.callee.object.arguments.length) {
38
+ return;
39
+ }
40
+
37
41
  const {
38
42
  arguments: [{
39
43
  object,
@@ -64,12 +68,11 @@ var _default = ({
64
68
  },
65
69
 
66
70
  "CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']"(node) {
67
- const arg = node.arguments[0].value;
68
-
69
71
  if (!isBannedArg(node)) {
70
72
  return;
71
73
  }
72
74
 
75
+ const arg = node.arguments[0].value;
73
76
  const correctFunction = getCorrectFunctionFor(node, true);
74
77
  const incorrectFunction = node.callee.property.name;
75
78
  context.report({
@@ -31,7 +31,7 @@ const meta = {
31
31
  exports.meta = meta;
32
32
 
33
33
  function isAntonymMatcher(matcherNode, matcherArguments) {
34
- return matcherNode.name === "toBeNull" || usesToBeOrToEqualWithNull(matcherNode, matcherArguments) || usesToHaveLengthZero(matcherNode, matcherArguments);
34
+ return matcherNode.name === "toBeNull" || matcherNode.name === "toBeFalsy" || usesToBeOrToEqualWithNull(matcherNode, matcherArguments) || usesToHaveLengthZero(matcherNode, matcherArguments);
35
35
  }
36
36
 
37
37
  function usesToBeOrToEqualWithNull(matcherNode, matcherArguments) {
@@ -43,7 +43,7 @@ function usesToHaveLengthZero(matcherNode, matcherArguments) {
43
43
  }
44
44
 
45
45
  const create = context => {
46
- const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual)$/;
46
+ const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual|toBeTruthy|toBeFalsy)$/;
47
47
 
48
48
  function getLengthValue(matcherArguments) {
49
49
  let lengthValue;
@@ -73,7 +73,7 @@ const create = context => {
73
73
  // only report on dom nodes which we can resolve to RTL queries.
74
74
  if (!queryNode || !queryNode.name && !queryNode.property) return; // toHaveLength() is only invalid with 0 or 1
75
75
 
76
- if (matcherNode.name === "toHaveLength") {
76
+ if (matcherNode.name === "toHaveLength" && matcherArguments.length) {
77
77
  const lengthValue = getLengthValue(matcherArguments); // isNotToHaveLengthZero represents .not.toHaveLength(0) which is a valid use of toHaveLength
78
78
 
79
79
  const isNotToHaveLengthZero = usesToHaveLengthZero(matcherNode, matcherArguments) && negatedMatcher;
@@ -86,7 +86,7 @@ const create = context => {
86
86
 
87
87
 
88
88
  if (matcherNode.name === "toBe" || matcherNode.name === "toEqual") {
89
- if (!usesToBeOrToEqualWithNull(matcherNode, matcherArguments)) {
89
+ if (!matcherArguments.length || !usesToBeOrToEqualWithNull(matcherNode, matcherArguments)) {
90
90
  return;
91
91
  }
92
92
  }
@@ -130,6 +130,10 @@ const create = context => {
130
130
  return {
131
131
  // expect(<query>).not.<matcher>
132
132
  [`CallExpression[callee.object.object.callee.name='expect'][callee.object.property.name='not'][callee.property.name=${alternativeMatchers}], CallExpression[callee.object.callee.name='expect'][callee.object.property.name='not'][callee.object.arguments.0.argument.callee.name=${alternativeMatchers}]`](node) {
133
+ if (!node.callee.object.object.arguments.length) {
134
+ return;
135
+ }
136
+
133
137
  const arg = node.callee.object.object.arguments[0];
134
138
  const queryNode = arg.type === "AwaitExpression" ? arg.argument.callee : arg.callee;
135
139
  const matcherNode = node.callee.property;
@@ -43,14 +43,14 @@ const create = context => ({
43
43
 
44
44
  [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
45
45
  const arg = node.parent.parent.parent.arguments;
46
- const isNullOrEmpty = arg.length > 0 && (arg[0].value === null || arg[0].value === "");
46
+ const isNull = arg.length > 0 && arg[0].value === null;
47
47
  const sourceCode = context.getSourceCode();
48
48
  context.report({
49
49
  node: node.parent,
50
50
  message: `Use toHaveAttribute instead of asserting on getAttribute`,
51
51
  fix: fixer => {
52
- const lastFixer = isNullOrEmpty ? fixer.replaceText(node.parent.parent.parent.arguments[0], sourceCode.getText(node.arguments[0])) : fixer.insertTextBefore(node.parent.parent.parent.arguments[0], `${sourceCode.getText(node.arguments[0])}, `);
53
- return [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceText(node.parent.parent.property, `${isNullOrEmpty ? "not." : ""}toHaveAttribute`), lastFixer];
52
+ const lastFixer = isNull ? fixer.replaceText(node.parent.parent.parent.arguments[0], sourceCode.getText(node.arguments[0])) : fixer.insertTextBefore(node.parent.parent.parent.arguments[0], `${sourceCode.getText(node.arguments[0])}, `);
53
+ return [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceText(node.parent.parent.property, `${isNull ? "not." : ""}toHaveAttribute`), lastFixer];
54
54
  }
55
55
  });
56
56
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "3.8.0",
3
+ "version": "3.9.2",
4
4
  "description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom",
5
5
  "main": "dist/index.js",
6
6
  "keywords": [
@@ -46,8 +46,8 @@
46
46
  },
47
47
  "devDependencies": {
48
48
  "@typescript-eslint/parser": "^4.8.2",
49
- "eslint": "7.23",
50
- "eslint-remote-tester": "^0.3.3",
49
+ "eslint": "^7.31.0",
50
+ "eslint-remote-tester": "^1.2.0",
51
51
  "jest-extended": "^0.11.5",
52
52
  "kcd-scripts": "6.5.1",
53
53
  "typescript": "^4.1.2"