eslint-plugin-jest-dom 5.5.0 β†’ 5.7.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
@@ -123,19 +123,20 @@ module.exports = [
123
123
  πŸ”§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
124
124
  πŸ’‘ Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
125
125
 
126
- | NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ |
127
- | :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- | :- |
128
- | [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes | βœ… | πŸ”§ | |
129
- | [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML | βœ… | πŸ”§ | |
130
- | [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes | βœ… | πŸ”§ | |
131
- | [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement | βœ… | πŸ”§ | |
132
- | [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node | βœ… | πŸ”§ | πŸ’‘ |
133
- | [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties | βœ… | πŸ”§ | |
134
- | [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking getAttribute/hasAttribute | βœ… | πŸ”§ | |
135
- | [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className | βœ… | πŸ”§ | |
136
- | [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style | βœ… | πŸ”§ | |
137
- | [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent | βœ… | πŸ”§ | |
138
- | [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value | βœ… | πŸ”§ | |
126
+ | NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ |
127
+ | :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :-- | :-- | :-- |
128
+ | [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes | βœ… | πŸ”§ | |
129
+ | [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML | βœ… | πŸ”§ | |
130
+ | [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes | βœ… | πŸ”§ | |
131
+ | [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement | βœ… | πŸ”§ | |
132
+ | [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node | βœ… | πŸ”§ | πŸ’‘ |
133
+ | [prefer-pressed](docs/rules/prefer-pressed.md) | prefer toBePressed over checking attributes | | πŸ”§ | |
134
+ | [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties | βœ… | πŸ”§ | |
135
+ | [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking getAttribute/hasAttribute | βœ… | πŸ”§ | |
136
+ | [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className | βœ… | πŸ”§ | |
137
+ | [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style | βœ… | πŸ”§ | |
138
+ | [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent | βœ… | πŸ”§ | |
139
+ | [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value | βœ… | πŸ”§ | |
139
140
 
140
141
  <!-- end auto-generated rules list -->
141
142
 
package/dist/context.js CHANGED
@@ -7,7 +7,7 @@ exports.getScope = getScope;
7
7
  exports.getSourceCode = getSourceCode;
8
8
  /* istanbul ignore next */
9
9
  function getSourceCode(context) {
10
- if ('sourceCode' in context) {
10
+ if ("sourceCode" in context) {
11
11
  return context.sourceCode;
12
12
  }
13
13
  return context.getSourceCode();
@@ -8,8 +8,10 @@ var _assignmentAst = require("./assignment-ast");
8
8
  var _default = ({
9
9
  preferred,
10
10
  negatedPreferred,
11
- attributes
11
+ attributes,
12
+ excludeValues = []
12
13
  }) => context => {
14
+ const isExcludedValue = node => excludeValues.length > 0 && node.arguments.length >= 2 && node.arguments[1].type === "Literal" && typeof node.arguments[1].value === "string" && excludeValues.some(v => v.toLowerCase() === node.arguments[1].value.toLowerCase());
13
15
  const getCorrectFunctionFor = (node, negated = false) => (node.arguments.length === 1 || node.arguments[1].value === true || node.arguments[1].type !== "Literal" || typeof node.arguments[1].value === "string" && node.arguments[1].value.toLowerCase() === "true" || node.arguments[1].value === "") && !negated ? preferred : negatedPreferred;
14
16
  const isBannedArg = node => node.arguments.length && attributes.some(attr => attr === node.arguments[0].value);
15
17
 
@@ -62,6 +64,9 @@ var _default = ({
62
64
  if (!isBannedArg(node)) {
63
65
  return;
64
66
  }
67
+ if (isExcludedValue(node)) {
68
+ return;
69
+ }
65
70
  const arg = node.arguments[0].value;
66
71
  const correctFunction = getCorrectFunctionFor(node, true);
67
72
  const incorrectFunction = node.callee.property.name;
@@ -75,6 +80,9 @@ var _default = ({
75
80
  if (!isBannedArg(node)) {
76
81
  return;
77
82
  }
83
+ if (isExcludedValue(node)) {
84
+ return;
85
+ }
78
86
  const {
79
87
  isDTLQuery
80
88
  } = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
package/dist/index.js CHANGED
@@ -28,7 +28,12 @@ const allRules = Object.entries(rules).reduce((memo, [name]) => ({
28
28
  [`jest-dom/${name}`]: "error"
29
29
  }
30
30
  }), {});
31
- const recommendedRules = allRules;
31
+ const recommendedRules = Object.entries(rules).filter(([_, rule]) => rule.meta.docs.recommended).reduce((memo, [name]) => ({
32
+ ...memo,
33
+ ...{
34
+ [`jest-dom/${name}`]: "error"
35
+ }
36
+ }), {});
32
37
  const plugin = {
33
38
  meta: {
34
39
  name: _package.name,
@@ -23,5 +23,6 @@ const meta = exports.meta = {
23
23
  const create = exports.create = (0, _createBannedAttributeRule.default)({
24
24
  preferred: "toBeChecked",
25
25
  negatedPreferred: "not.toBeChecked",
26
- attributes: ["checked", "aria-checked"]
26
+ attributes: ["checked", "aria-checked"],
27
+ excludeValues: ["mixed"]
27
28
  });
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.meta = exports.create = void 0;
8
+ var _createBannedAttributeRule = _interopRequireDefault(require("../createBannedAttributeRule"));
9
+ /**
10
+ * @fileoverview prefer toBePressed or not.toBePressed over attribute checks
11
+ */
12
+
13
+ const meta = exports.meta = {
14
+ docs: {
15
+ description: "prefer toBePressed over checking attributes",
16
+ category: "Best Practices",
17
+ recommended: false,
18
+ // todo: make this recommended in next major
19
+ url: "prefer-pressed"
20
+ },
21
+ fixable: "code"
22
+ };
23
+ const create = exports.create = (0, _createBannedAttributeRule.default)({
24
+ preferred: "toBePressed",
25
+ negatedPreferred: "not.toBePressed",
26
+ attributes: ["aria-pressed"],
27
+ excludeValues: ["mixed"]
28
+ });
@@ -28,7 +28,7 @@ const create = context => ({
28
28
  context.report({
29
29
  node: node.parent,
30
30
  message: `Use toHaveAttribute instead of asserting on getAttribute`,
31
- fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `not.toHaveAttribute(${context.getSourceCode().getText(node.arguments[0])})`)]
31
+ fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `not.toHaveAttribute(${(0, _context.getSourceCode)(context).getText(node.arguments[0])})`)]
32
32
  });
33
33
  },
34
34
  [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](node) {
@@ -82,7 +82,7 @@ const create = context => ({
82
82
  context.report({
83
83
  node: node.parent,
84
84
  message: `Use toHaveAttribute instead of asserting on hasAttribute`,
85
- fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `${node.parent.parent.property.name === "toBeFalsy" ? "not." : ""}toHaveAttribute(${context.getSourceCode().getText(node.arguments[0])})`)]
85
+ fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `${node.parent.parent.property.name === "toBeFalsy" ? "not." : ""}toHaveAttribute(${(0, _context.getSourceCode)(context).getText(node.arguments[0])})`)]
86
86
  });
87
87
  }
88
88
  });
@@ -143,7 +143,7 @@ const create = context => ({
143
143
  [[`CallExpression[callee.object.callee.name=expect][callee.property.name=toHaveAttribute][arguments.0.type=/Literal/][arguments.1.type=/Literal$/]`, `CallExpression[callee.object.callee.name=expect][callee.property.name=toHaveProperty][arguments.0.type=/Literal/][arguments.1.type=/Literal$/]`].join(",")](node) {
144
144
  const matcher = node.callee.property;
145
145
  const [classArg, classValueArg] = node.arguments;
146
- const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
146
+ const classNameValue = (0, _context.getSourceCode)(context).getText(classArg).slice(1, -1);
147
147
  if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
148
148
  return;
149
149
  }
@@ -165,7 +165,7 @@ const create = context => ({
165
165
  //[callee.object.property.name=/toHaveAttribute|toHaveProperty/][arguments.0.value=class][arguments.1.type=/Literal$/]
166
166
  const matcher = node.callee.property;
167
167
  const [classArg, classValueArg] = node.arguments;
168
- const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
168
+ const classNameValue = (0, _context.getSourceCode)(context).getText(classArg).slice(1, -1);
169
169
  if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
170
170
  return;
171
171
  }
@@ -187,7 +187,7 @@ const create = context => ({
187
187
  const matcher = node.callee.property;
188
188
  const [classArg, classValue] = node.arguments;
189
189
  const classValueArg = classValue.arguments[0];
190
- const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
190
+ const classNameValue = (0, _context.getSourceCode)(context).getText(classArg).slice(1, -1);
191
191
  if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
192
192
  return;
193
193
  }
@@ -31,7 +31,7 @@ const create = context => {
31
31
  return `[${(0, _context.getSourceCode)(context).getText(styleName)}]`;
32
32
  }
33
33
  function getReplacementStyleParam(styleName, styleValue) {
34
- return styleName.type === "Literal" ? `{${camelCase(styleName.value)}: ${context.getSourceCode().getText(styleValue)}}` : `${(0, _context.getSourceCode)(context).getText(styleName).slice(0, -1)}: ${styleValue.type === "TemplateLiteral" ? (0, _context.getSourceCode)(context).getText(styleValue).substring(1) : `${styleValue.value}\``}`;
34
+ return styleName.type === "Literal" ? `{${camelCase(styleName.value)}: ${(0, _context.getSourceCode)(context).getText(styleValue)}}` : `${(0, _context.getSourceCode)(context).getText(styleName).slice(0, -1)}: ${styleValue.type === "TemplateLiteral" ? (0, _context.getSourceCode)(context).getText(styleValue).substring(1) : `${styleValue.value}\``}`;
35
35
  }
36
36
  return {
37
37
  //expect(el.style.foo).toBe("bar");
@@ -43,7 +43,7 @@ const create = context => {
43
43
  node: node.property,
44
44
  message: "Use toHaveStyle instead of asserting on element style",
45
45
  fix(fixer) {
46
- return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${context.getSourceCode().getText(styleValue)}}`)];
46
+ return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${(0, _context.getSourceCode)(context).getText(styleValue)}}`)];
47
47
  }
48
48
  });
49
49
  },
@@ -56,7 +56,7 @@ const create = context => {
56
56
  node: node.property,
57
57
  message: "Use toHaveStyle instead of asserting on element style",
58
58
  fix(fixer) {
59
- return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${context.getSourceCode().getText(styleValue)}}`)];
59
+ return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${(0, _context.getSourceCode)(context).getText(styleValue)}}`)];
60
60
  }
61
61
  });
62
62
  },
@@ -142,7 +142,7 @@ const create = context => {
142
142
  if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
143
143
  return null;
144
144
  }
145
- return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${getReplacementObjectProperty(styleName)}: ${context.getSourceCode().getText(styleValue)}}`)];
145
+ return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${getReplacementObjectProperty(styleName)}: ${(0, _context.getSourceCode)(context).getText(styleValue)}}`)];
146
146
  }
147
147
  });
148
148
  },
@@ -157,7 +157,7 @@ const create = context => {
157
157
  if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
158
158
  return null;
159
159
  }
160
- return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${getReplacementObjectProperty(styleName)}: ${context.getSourceCode().getText(styleValue)}}`)];
160
+ return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${getReplacementObjectProperty(styleName)}: ${(0, _context.getSourceCode)(context).getText(styleValue)}}`)];
161
161
  }
162
162
  });
163
163
  }
@@ -27,7 +27,7 @@ const create = context => ({
27
27
  node: node.parent,
28
28
  message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
29
29
  fix: fixer => {
30
- return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, expectedArg.type === "Literal" ? expectedArg.regex ? expectedArgSource : new RegExp(expectedArg.value.toString().replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")) : `new RegExp(${expectedArgSource})`)];
30
+ return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, expectedArg.type === "Literal" ? expectedArg.regex ? expectedArgSource : new RegExp(expectedArg.value.toString().replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")).toString() : `new RegExp(${expectedArgSource})`)];
31
31
  }
32
32
  });
33
33
  },
@@ -51,7 +51,7 @@ const create = context => ({
51
51
  context.report({
52
52
  node: node.parent,
53
53
  message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
54
- fix: fixer => [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, expectedArg.type === "Literal" ? expectedArg.regex ? expectedArgSource : new RegExp(expectedArg.value.toString().replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")) : `new RegExp(${expectedArgSource})`)]
54
+ fix: fixer => [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, expectedArg.type === "Literal" ? expectedArg.regex ? expectedArgSource : new RegExp(expectedArg.value.toString().replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")).toString() : `new RegExp(${expectedArgSource})`)]
55
55
  });
56
56
  }
57
57
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "5.5.0",
3
+ "version": "5.7.0",
4
4
  "description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom",
5
5
  "keywords": [
6
6
  "eslint",
@@ -32,10 +32,8 @@
32
32
  ],
33
33
  "scripts": {
34
34
  "build": "kcd-scripts build",
35
- "pregenerate-readme-table": "npm run build",
36
- "generate-readme-table": "eslint-doc-generator",
35
+ "regenerate-docs": "npm run build && eslint-doc-generator",
37
36
  "lint": "kcd-scripts lint",
38
- "lint:generate-readme-table": "npm run generate-readme-table -- --check",
39
37
  "setup": "npm install && npm run validate -s",
40
38
  "test": "kcd-scripts test",
41
39
  "test:coverage": "npm test -- --coverage",
@@ -94,19 +92,20 @@
94
92
  "requireindex": "^1.2.0"
95
93
  },
96
94
  "devDependencies": {
97
- "@testing-library/dom": "^8.20.0",
95
+ "@testing-library/dom": "^10.4.1",
98
96
  "@typescript-eslint/parser": "^5.9.1",
99
97
  "eslint": "^8.7.0",
100
- "eslint-doc-generator": "^1.0.0",
98
+ "eslint-doc-generator": "^3.0.0",
101
99
  "eslint-remote-tester": "^3.0.0",
102
100
  "eslint-remote-tester-repositories": "^1.0.1",
103
101
  "kcd-scripts": "^12.0.0",
102
+ "prettier": "^2.0.0",
104
103
  "semver": "^7.6.0",
105
104
  "typescript": "^5.1.3"
106
105
  },
107
106
  "peerDependencies": {
108
107
  "@testing-library/dom": "^8.0.0 || ^9.0.0 || ^10.0.0",
109
- "eslint": "^6.8.0 || ^7.0.0 || ^8.0.0 || ^9.0.0"
108
+ "eslint": "^6.8.0 || ^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0"
110
109
  },
111
110
  "peerDependenciesMeta": {
112
111
  "@testing-library/dom": {