eslint-plugin-jest-dom 3.9.4 → 4.0.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
@@ -12,10 +12,7 @@
12
12
  [![version][version-badge]][package]
13
13
  [![downloads][downloads-badge]][npmtrends]
14
14
  [![MIT License][license-badge]][license]
15
-
16
- <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
17
- [![All Contributors](https://img.shields.io/badge/all_contributors-17-orange.svg?style=flat-square)](#contributors-)
18
- <!-- ALL-CONTRIBUTORS-BADGE:END -->
15
+ [![All Contributors][all-contributors-badge]](#contributors-)
19
16
  [![PRs Welcome][prs-badge]][prs]
20
17
  [![Code of Conduct][coc-badge]][coc]
21
18
  <!-- prettier-ignore-end -->
@@ -183,8 +180,8 @@ MIT
183
180
  <!-- prettier-ignore-start -->
184
181
  [npm]: https://www.npmjs.com
185
182
  [node]: https://nodejs.org
186
- [build-badge]: https://img.shields.io/travis/testing-library/eslint-plugin-jest-dom.svg?style=flat-square
187
- [build]: https://travis-ci.org/testing-library/eslint-plugin-jest-dom
183
+ [build-badge]: https://img.shields.io/github/workflow/status/testing-library/eslint-plugin-jest-dom/validate?logo=github&style=flat-square
184
+ [build]: https://github.com/testing-library/eslint-plugin-jest-dom/actions?query=workflow%3Avalidate
188
185
  [coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/eslint-plugin-jest-dom.svg?style=flat-square
189
186
  [coverage]: https://codecov.io/github/testing-library/eslint-plugin-jest-dom
190
187
  [version-badge]: https://img.shields.io/npm/v/eslint-plugin-jest-dom.svg?style=flat-square
@@ -199,6 +196,7 @@ MIT
199
196
  [coc]: https://github.com/testing-library/eslint-plugin-jest-dom/blob/main/other/CODE_OF_CONDUCT.md
200
197
  [emojis]: https://github.com/all-contributors/all-contributors#emoji-key
201
198
  [all-contributors]: https://github.com/all-contributors/all-contributors
199
+ [all-contributors-badge]: https://img.shields.io/github/all-contributors/testing-library/eslint-plugin-jest-dom?color=orange&style=flat-square
202
200
  [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
203
201
  [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
204
202
  [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
@@ -70,7 +70,11 @@ const create = context => {
70
70
  negatedMatcher,
71
71
  expect
72
72
  }) {
73
- // only report on dom nodes which we can resolve to RTL queries.
73
+ if (matcherNode.parent.parent.type !== "CallExpression") {
74
+ return;
75
+ } // only report on dom nodes which we can resolve to RTL queries.
76
+
77
+
74
78
  if (!queryNode || !queryNode.name && !queryNode.property) return; // toHaveLength() is only invalid with 0 or 1
75
79
 
76
80
  if (matcherNode.name === "toHaveLength" && matcherArguments.length) {
@@ -90,13 +90,19 @@ const create = context => ({
90
90
  } = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp);
91
91
  if (!isDTLQuery) return; // don't report here if using `expect.foo()`
92
92
 
93
- if (classValue.type === "CallExpression" && classValue.callee.type === "MemberExpression" && classValue.callee.object.name === "expect") return;
93
+ if (classValue.type === "CallExpression" && classValue.callee.type === "MemberExpression" && classValue.callee.object.name === "expect") {
94
+ return;
95
+ }
96
+
94
97
  context.report({
95
98
  node: matcher,
96
99
  messageId,
97
100
 
98
101
  fix(fixer) {
99
- if (checkedProp.name === "classList" && matcher.name !== "toContain") return;
102
+ if (checkedProp.name === "classList" && matcher.name !== "toContain") {
103
+ return;
104
+ }
105
+
100
106
  return [fixer.removeRange([classNameProp.range[1], checkedProp.range[1]]), fixer.replaceText(matcher, "toHaveClass"), fixer.replaceText(classValue, `${context.getSourceCode().getText(classValue)}${matcher.name === "toContain" ? "" : ", { exact: true }"}`)];
101
107
  }
102
108
 
@@ -141,7 +147,10 @@ const create = context => ({
141
147
  messageId,
142
148
 
143
149
  fix(fixer) {
144
- if (className.name === "classList" && matcher.name !== "toContain") return;
150
+ if (className.name === "classList" && matcher.name !== "toContain") {
151
+ return;
152
+ }
153
+
145
154
  return [fixer.removeRange([classNameProp.range[1], className.range[1]]), fixer.replaceText(matcher, "toHaveClass"), fixer.replaceText(classValue, `${context.getSourceCode().getText(classValue)}${matcher.name === "toContain" ? "" : ", { exact: true }"}`)];
146
155
  }
147
156
 
@@ -154,7 +163,11 @@ const create = context => ({
154
163
  const matcher = node.callee.property;
155
164
  const [classArg, classValueArg] = node.arguments;
156
165
  const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
157
- if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
166
+
167
+ if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
168
+ return;
169
+ }
170
+
158
171
  const {
159
172
  isDTLQuery
160
173
  } = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
@@ -177,7 +190,11 @@ const create = context => ({
177
190
  const matcher = node.callee.property;
178
191
  const [classArg, classValueArg] = node.arguments;
179
192
  const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
180
- if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
193
+
194
+ if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
195
+ return;
196
+ }
197
+
181
198
  const {
182
199
  isDTLQuery
183
200
  } = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.object.arguments[0]);
@@ -200,7 +217,11 @@ const create = context => ({
200
217
  const [classArg, classValue] = node.arguments;
201
218
  const classValueArg = classValue.arguments[0];
202
219
  const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
203
- if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
220
+
221
+ if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
222
+ return;
223
+ }
224
+
204
225
  const {
205
226
  isDTLQuery
206
227
  } = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
@@ -26,6 +26,14 @@ const meta = {
26
26
  exports.meta = meta;
27
27
 
28
28
  const create = context => {
29
+ function getReplacementObjectProperty(styleName) {
30
+ if (styleName.type === "Literal") {
31
+ return camelCase(styleName.value);
32
+ }
33
+
34
+ return `[${context.getSourceCode().getText(styleName)}]`;
35
+ }
36
+
29
37
  function getReplacementStyleParam(styleName, styleValue) {
30
38
  return styleName.type === "Literal" ? `{${camelCase(styleName.value)}: ${context.getSourceCode().getText(styleValue)}}` : `${context.getSourceCode().getText(styleName).slice(0, -1)}: ${styleValue.type === "TemplateLiteral" ? context.getSourceCode().getText(styleValue).substring(1) : `${styleValue.value}\``}`;
31
39
  }
@@ -107,20 +115,24 @@ const create = context => {
107
115
  },
108
116
 
109
117
  //expect(el.style["foo-bar"]).toBe("baz")
110
- [`MemberExpression[property.name=style][parent.computed=true][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.arguments.0.type=/(Template)?Literal/][parent.parent.callee.name=expect]`](node) {
118
+ [`MemberExpression[property.name=style][parent.computed=true][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.arguments.0.type=/((Template)?Literal|Identifier)/][parent.parent.callee.name=expect]`](node) {
111
119
  const styleName = node.parent.property;
112
120
  const [styleValue] = node.parent.parent.parent.parent.arguments;
113
121
  const matcher = node.parent.parent.parent.property;
114
122
  const startOfStyleMemberExpression = node.object.range[1];
115
123
  const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1];
124
+ let fix = null;
125
+
126
+ if (typeof styleValue.value !== "number" && !(styleValue.value instanceof RegExp)) {
127
+ fix = fixer => {
128
+ return [fixer.removeRange([startOfStyleMemberExpression, endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, typeof styleName.value === "number" ? `{${getReplacementObjectProperty(styleValue)}: expect.anything()}` : getReplacementStyleParam(styleName, styleValue))];
129
+ };
130
+ }
131
+
116
132
  context.report({
117
133
  node: node.property,
118
134
  message: "Use toHaveStyle instead of asserting on element style",
119
-
120
- fix(fixer) {
121
- return [fixer.removeRange([startOfStyleMemberExpression, endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))];
122
- }
123
-
135
+ fix
124
136
  });
125
137
  },
126
138
 
@@ -130,14 +142,18 @@ const create = context => {
130
142
  const [styleValue] = node.parent.parent.parent.parent.parent.arguments;
131
143
  const matcher = node.parent.parent.parent.parent.property;
132
144
  const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1];
133
- context.report({
134
- node: node.property,
135
- message: "Use toHaveStyle instead of asserting on element style",
145
+ let fix = null;
136
146
 
137
- fix(fixer) {
147
+ if (typeof styleName.value !== "number") {
148
+ fix = fixer => {
138
149
  return [fixer.removeRange([node.object.range[1], endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))];
139
- }
150
+ };
151
+ }
140
152
 
153
+ context.report({
154
+ node: node.property,
155
+ message: "Use toHaveStyle instead of asserting on element style",
156
+ fix
141
157
  });
142
158
  },
143
159
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "3.9.4",
3
+ "version": "4.0.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": [
@@ -41,27 +41,26 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@babel/runtime": "^7.16.3",
44
- "@testing-library/dom": "^7.31.2",
44
+ "@testing-library/dom": "^8.11.1",
45
45
  "requireindex": "^1.2.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@typescript-eslint/parser": "^4.33.0",
49
- "eslint": "^7.31.0",
50
- "eslint-remote-tester": "^2.0.1",
51
- "eslint-remote-tester-repositories": "^0.0.3",
52
- "kcd-scripts": "^11.2.2",
48
+ "@typescript-eslint/parser": "^5.9.1",
49
+ "eslint": "^8.7.0",
50
+ "eslint-remote-tester": "^2.1.1",
51
+ "eslint-remote-tester-repositories": "^0.0.5",
52
+ "kcd-scripts": "^12.0.0",
53
53
  "typescript": "^4.5.3"
54
54
  },
55
55
  "peerDependencies": {
56
- "eslint": ">=6.8"
56
+ "eslint": "^6.8.0 || ^7.0.0 || ^8.0.0"
57
57
  },
58
58
  "eslintConfig": {
59
59
  "extends": "./node_modules/kcd-scripts/eslint.js",
60
60
  "rules": {
61
- "babel/quotes": "off",
61
+ "consistent-return": "off",
62
62
  "max-lines-per-function": "off",
63
- "testing-library/no-dom-import": "off",
64
- "consistent-return": "off"
63
+ "testing-library/no-dom-import": "off"
65
64
  }
66
65
  },
67
66
  "eslintIgnore": [
@@ -71,7 +70,7 @@
71
70
  "eslint-remote-tester-results"
72
71
  ],
73
72
  "engines": {
74
- "node": "^10.12.0 || >=12.0.0",
73
+ "node": "^12.22.0 || ^14.17.0 || >=16.0.0",
75
74
  "npm": ">=6",
76
75
  "yarn": ">=1"
77
76
  }