eslint-plugin-jest-dom 4.0.2 → 5.0.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.
@@ -4,16 +4,15 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.meta = exports.create = void 0;
7
-
8
7
  /**
9
8
  * @fileoverview prefer toHaveStyle over checking element style
10
9
  * @author Ben Monro
11
10
  */
11
+
12
12
  //------------------------------------------------------------------------------
13
13
  // Rule Definition
14
14
  //------------------------------------------------------------------------------
15
15
  const camelCase = str => str.replace(/-([a-z])/g, c => c[1].toUpperCase());
16
-
17
16
  const meta = {
18
17
  docs: {
19
18
  category: "Best Practices",
@@ -24,20 +23,16 @@ const meta = {
24
23
  fixable: "code"
25
24
  };
26
25
  exports.meta = meta;
27
-
28
26
  const create = context => {
29
27
  function getReplacementObjectProperty(styleName) {
30
28
  if (styleName.type === "Literal") {
31
29
  return camelCase(styleName.value);
32
30
  }
33
-
34
31
  return `[${context.getSourceCode().getText(styleName)}]`;
35
32
  }
36
-
37
33
  function getReplacementStyleParam(styleName, styleValue) {
38
34
  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}\``}`;
39
35
  }
40
-
41
36
  return {
42
37
  //expect(el.style.foo).toBe("bar");
43
38
  [`MemberExpression[property.name=style][parent.computed=false][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.arguments.0.type=/(Template)?Literal/][parent.parent.callee.name=expect]`](node) {
@@ -47,14 +42,11 @@ const create = context => {
47
42
  context.report({
48
43
  node: node.property,
49
44
  message: "Use toHaveStyle instead of asserting on element style",
50
-
51
45
  fix(fixer) {
52
46
  return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${context.getSourceCode().getText(styleValue)}}`)];
53
47
  }
54
-
55
48
  });
56
49
  },
57
-
58
50
  //expect(el.style.foo).not.toBe("bar");
59
51
  [`MemberExpression[property.name=style][parent.computed=false][parent.parent.parent.property.name=not][parent.parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.parent.arguments.0.type=/(Template)?Literal$/][parent.parent.callee.name=expect]`](node) {
60
52
  const styleName = node.parent.property;
@@ -63,14 +55,11 @@ const create = context => {
63
55
  context.report({
64
56
  node: node.property,
65
57
  message: "Use toHaveStyle instead of asserting on element style",
66
-
67
58
  fix(fixer) {
68
59
  return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${context.getSourceCode().getText(styleValue)}}`)];
69
60
  }
70
-
71
61
  });
72
62
  },
73
-
74
63
  // expect(el.style).toContain("foo-bar")
75
64
  [`MemberExpression[property.name=style][parent.parent.property.name=toContain][parent.parent.parent.arguments.0.type=/(Template)?Literal$/][parent.callee.name=expect]`](node) {
76
65
  const [styleName] = node.parent.parent.parent.arguments;
@@ -78,14 +67,11 @@ const create = context => {
78
67
  context.report({
79
68
  node: node.property,
80
69
  message: "Use toHaveStyle instead of asserting on element style",
81
-
82
70
  fix(fixer) {
83
71
  return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleName, styleName.type === "Literal" ? `{${camelCase(styleName.value)}: expect.anything()}` : context.getSourceCode().getText(styleName))];
84
72
  }
85
-
86
73
  });
87
74
  },
88
-
89
75
  // expect(el.style).not.toContain("foo-bar")
90
76
  [`MemberExpression[property.name=style][parent.parent.property.name=not][parent.parent.parent.property.name=toContain][parent.parent.parent.parent.arguments.0.type=/(Template)?Literal$/]`](node) {
91
77
  const [styleName] = node.parent.parent.parent.parent.arguments;
@@ -93,27 +79,21 @@ const create = context => {
93
79
  context.report({
94
80
  node: node.property,
95
81
  message: "Use toHaveStyle instead of asserting on element style",
96
-
97
82
  fix(fixer) {
98
83
  return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleName, styleName.type === "Literal" ? `{${camelCase(styleName.value)}: expect.anything()}` : context.getSourceCode().getText(styleName))];
99
84
  }
100
-
101
85
  });
102
86
  },
103
-
104
87
  //expect(el).toHaveAttribute("style", "foo: bar");
105
88
  [`CallExpression[callee.property.name=toHaveAttribute][arguments.0.value=style][arguments.1][callee.object.callee.name=expect]`](node) {
106
89
  context.report({
107
90
  node: node.arguments[0],
108
91
  message: "Use toHaveStyle instead of asserting on element style",
109
-
110
92
  fix(fixer) {
111
93
  return [fixer.replaceText(node.callee.property, "toHaveStyle"), fixer.removeRange([node.arguments[0].range[0], node.arguments[1].range[0]])];
112
94
  }
113
-
114
95
  });
115
96
  },
116
-
117
97
  //expect(el.style["foo-bar"]).toBe("baz")
118
98
  [`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) {
119
99
  const styleName = node.parent.property;
@@ -122,20 +102,17 @@ const create = context => {
122
102
  const startOfStyleMemberExpression = node.object.range[1];
123
103
  const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1];
124
104
  let fix = null;
125
-
126
105
  if (typeof styleValue.value !== "number" && !(styleValue.value instanceof RegExp)) {
127
106
  fix = fixer => {
128
107
  return [fixer.removeRange([startOfStyleMemberExpression, endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, typeof styleName.value === "number" ? `{${getReplacementObjectProperty(styleValue)}: expect.anything()}` : getReplacementStyleParam(styleName, styleValue))];
129
108
  };
130
109
  }
131
-
132
110
  context.report({
133
111
  node: node.property,
134
112
  message: "Use toHaveStyle instead of asserting on element style",
135
113
  fix
136
114
  });
137
115
  },
138
-
139
116
  //expect(el.style["foo-bar"]).not.toBe("baz")
140
117
  [`MemberExpression[property.name=style][parent.computed=true][parent.parent.parent.property.name=not][parent.parent.parent.parent.parent.callee.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.parent.arguments.0.type=/(Template)?Literal/][parent.parent.callee.name=expect]`](node) {
141
118
  const styleName = node.parent.property;
@@ -143,20 +120,17 @@ const create = context => {
143
120
  const matcher = node.parent.parent.parent.parent.property;
144
121
  const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1];
145
122
  let fix = null;
146
-
147
123
  if (typeof styleName.value !== "number") {
148
124
  fix = fixer => {
149
125
  return [fixer.removeRange([node.object.range[1], endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))];
150
126
  };
151
127
  }
152
-
153
128
  context.report({
154
129
  node: node.property,
155
130
  message: "Use toHaveStyle instead of asserting on element style",
156
131
  fix
157
132
  });
158
133
  },
159
-
160
134
  //expect(foo.style).toHaveProperty("foo", "bar")
161
135
  [`MemberExpression[property.name=style][parent.parent.property.name=toHaveProperty][parent.callee.name=expect]`](node) {
162
136
  const [styleName, styleValue] = node.parent.parent.parent.arguments;
@@ -164,18 +138,14 @@ const create = context => {
164
138
  context.report({
165
139
  node: node.property,
166
140
  message: "Use toHaveStyle instead of asserting on element style",
167
-
168
141
  fix(fixer) {
169
142
  if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
170
143
  return null;
171
144
  }
172
-
173
145
  return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${camelCase(styleName.value)}: ${context.getSourceCode().getText(styleValue)}}`)];
174
146
  }
175
-
176
147
  });
177
148
  },
178
-
179
149
  //expect(foo.style).not.toHaveProperty("foo", "bar")
180
150
  [`MemberExpression[property.name=style][parent.parent.property.name=not][parent.parent.parent.property.name=toHaveProperty][parent.callee.name=expect]`](node) {
181
151
  const [styleName, styleValue] = node.parent.parent.parent.parent.arguments;
@@ -183,19 +153,14 @@ const create = context => {
183
153
  context.report({
184
154
  node: node.property,
185
155
  message: "Use toHaveStyle instead of asserting on element style",
186
-
187
156
  fix(fixer) {
188
157
  if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
189
158
  return null;
190
159
  }
191
-
192
160
  return [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceTextRange([styleName.range[0], styleValue.range[1]], `{${camelCase(styleName.value)}: ${context.getSourceCode().getText(styleValue)}}`)];
193
161
  }
194
-
195
162
  });
196
163
  }
197
-
198
164
  };
199
165
  };
200
-
201
166
  exports.create = create;
@@ -4,11 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.meta = exports.create = void 0;
7
-
8
7
  /**
9
8
  * @fileoverview prefer toHaveAttribute over checking getAttribute/hasAttribute
10
9
  * @author Ben Monro
11
10
  */
11
+
12
12
  const meta = {
13
13
  docs: {
14
14
  category: "Best Practices",
@@ -19,7 +19,6 @@ const meta = {
19
19
  fixable: "code"
20
20
  };
21
21
  exports.meta = meta;
22
-
23
22
  const create = context => ({
24
23
  [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](node) {
25
24
  const expectedArg = node.parent.parent.parent.arguments[0];
@@ -32,7 +31,6 @@ const create = context => ({
32
31
  }
33
32
  });
34
33
  },
35
-
36
34
  [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
37
35
  context.report({
38
36
  node: node.parent,
@@ -40,7 +38,6 @@ const create = context => ({
40
38
  fix: fixer => [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.property.range, "toHaveTextContent")]
41
39
  });
42
40
  },
43
-
44
41
  [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
45
42
  context.report({
46
43
  node: node.parent,
@@ -48,7 +45,6 @@ const create = context => ({
48
45
  fix: fixer => [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.parent.property.range, "toHaveTextContent")]
49
46
  });
50
47
  },
51
-
52
48
  [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toContain$|toMatch$/]`](node) {
53
49
  const expectedArg = node.parent.parent.parent.parent.arguments[0];
54
50
  const expectedArgSource = context.getSourceCode().getText(expectedArg);
@@ -58,7 +54,5 @@ const create = context => ({
58
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})`)]
59
55
  });
60
56
  }
61
-
62
57
  });
63
-
64
58
  exports.create = create;
@@ -4,16 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.meta = exports.create = void 0;
7
-
8
7
  var _assignmentAst = require("../assignment-ast");
9
-
10
8
  /**
11
9
  * @fileoverview prefer toHaveAttribute over checking getAttribute/hasAttribute
12
10
  * @author Ben Monro
13
11
  */
12
+
14
13
  //------------------------------------------------------------------------------
15
14
  // Rule Definition
16
15
  //------------------------------------------------------------------------------
16
+
17
17
  const meta = {
18
18
  docs: {
19
19
  category: "Best Practices",
@@ -28,7 +28,6 @@ const meta = {
28
28
  };
29
29
  exports.meta = meta;
30
30
  const messageId = "use-to-have-value";
31
-
32
31
  const create = context => {
33
32
  function isValidQueryNode(nodeWithValueProp) {
34
33
  const {
@@ -38,7 +37,6 @@ const create = context => {
38
37
  } = (0, _assignmentAst.getQueryNodeFrom)(context, nodeWithValueProp);
39
38
  return !!query && isDTLQuery && !!query.match(/^(get|find|query)(All)?ByRole$/) && ["textbox", "dropdown"].includes(queryArg);
40
39
  }
41
-
42
40
  return {
43
41
  // expect(element.value).toBe('foo') / toEqual / toStrictEqual
44
42
  // expect(<query>.value).toBe('foo') / toEqual / toStrictEqual
@@ -47,20 +45,16 @@ const create = context => {
47
45
  const valueProp = node.callee.object.arguments[0].property;
48
46
  const matcher = node.callee.property;
49
47
  const queryNode = node.callee.object.arguments[0].object;
50
-
51
48
  if (isValidQueryNode(queryNode)) {
52
49
  context.report({
53
50
  messageId,
54
51
  node,
55
-
56
52
  fix(fixer) {
57
53
  return [fixer.remove(context.getSourceCode().getTokenBefore(valueProp)), fixer.remove(valueProp), fixer.replaceText(matcher, "toHaveValue")];
58
54
  }
59
-
60
55
  });
61
56
  }
62
57
  },
63
-
64
58
  // expect(element.value).not.toBe('foo') / toEqual / toStrictEqual
65
59
  // expect(<query>.value).not.toBe('foo') / toEqual / toStrictEqual
66
60
  // expect((await <query>).value).not.toBe('foo') / toEqual / toStrictEqual
@@ -68,20 +62,16 @@ const create = context => {
68
62
  const queryNode = node.callee.object.object.arguments[0].object;
69
63
  const valueProp = node.callee.object.object.arguments[0].property;
70
64
  const matcher = node.callee.property;
71
-
72
65
  if (isValidQueryNode(queryNode)) {
73
66
  context.report({
74
67
  messageId,
75
68
  node,
76
-
77
69
  fix(fixer) {
78
70
  return [fixer.removeRange([context.getSourceCode().getTokenBefore(valueProp).range[0], valueProp.range[1]]), fixer.replaceText(matcher, "toHaveValue")];
79
71
  }
80
-
81
72
  });
82
73
  }
83
74
  },
84
-
85
75
  //expect(element).toHaveAttribute('value', 'foo') / Property
86
76
  [`CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=value][arguments.1][callee.object.callee.name=expect], CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=value][arguments.1][callee.object.object.callee.name=expect][callee.object.property.name=not]`](node) {
87
77
  const matcher = node.callee.property;
@@ -89,15 +79,11 @@ const create = context => {
89
79
  context.report({
90
80
  messageId,
91
81
  node,
92
-
93
82
  fix(fixer) {
94
83
  return [fixer.replaceText(matcher, "toHaveValue"), fixer.removeRange([prop.range[0], value.range[0]])];
95
84
  }
96
-
97
85
  });
98
86
  }
99
-
100
87
  };
101
88
  };
102
-
103
89
  exports.create = create;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "4.0.2",
3
+ "version": "5.0.0",
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": [
@@ -30,9 +30,10 @@
30
30
  ],
31
31
  "scripts": {
32
32
  "build": "kcd-scripts build",
33
- "pregenerate-readme-table": "yarn build",
34
- "generate-readme-table": "node build/generate-readme-table.js",
33
+ "pregenerate-readme-table": "npm run build",
34
+ "generate-readme-table": "eslint-doc-generator --ignore-config all",
35
35
  "lint": "kcd-scripts lint",
36
+ "lint:generate-readme-table": "npm run generate-readme-table -- --check",
36
37
  "setup": "npm install && npm run validate -s",
37
38
  "test": "kcd-scripts test",
38
39
  "test:coverage": "npm test -- --coverage",
@@ -41,19 +42,20 @@
41
42
  },
42
43
  "dependencies": {
43
44
  "@babel/runtime": "^7.16.3",
44
- "@testing-library/dom": "^8.11.1",
45
45
  "requireindex": "^1.2.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@typescript-eslint/parser": "^5.9.1",
49
49
  "eslint": "^8.7.0",
50
- "eslint-remote-tester": "^2.1.1",
51
- "eslint-remote-tester-repositories": "^0.0.5",
50
+ "eslint-doc-generator": "^1.0.0",
51
+ "eslint-remote-tester": "^3.0.0",
52
+ "eslint-remote-tester-repositories": "^1.0.1",
52
53
  "kcd-scripts": "^12.0.0",
53
54
  "typescript": "^4.5.3"
54
55
  },
55
56
  "peerDependencies": {
56
- "eslint": "^6.8.0 || ^7.0.0 || ^8.0.0"
57
+ "eslint": "^6.8.0 || ^7.0.0 || ^8.0.0",
58
+ "@testing-library/dom": "^8.0.0 || ^9.0.0"
57
59
  },
58
60
  "eslintConfig": {
59
61
  "extends": "./node_modules/kcd-scripts/eslint.js",