eslint-plugin-jest-dom 3.9.1 → 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.
@@ -0,0 +1,185 @@
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 toHaveStyle over checking element style
10
+ * @author Ben Monro
11
+ */
12
+ //------------------------------------------------------------------------------
13
+ // Rule Definition
14
+ //------------------------------------------------------------------------------
15
+ const camelCase = str => str.replace(/-([a-z])/g, c => c[1].toUpperCase());
16
+
17
+ const meta = {
18
+ docs: {
19
+ category: "Best Practices",
20
+ url: "prefer-to-have-style",
21
+ description: "prefer toHaveStyle over checking element style",
22
+ recommended: true
23
+ },
24
+ fixable: "code"
25
+ };
26
+ exports.meta = meta;
27
+
28
+ const create = context => {
29
+ function getReplacementStyleParam(styleName, styleValue) {
30
+ 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
+ }
32
+
33
+ return {
34
+ //expect(el.style.foo).toBe("bar");
35
+ [`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) {
36
+ const styleName = node.parent.property;
37
+ const [styleValue] = node.parent.parent.parent.parent.arguments;
38
+ const matcher = node.parent.parent.parent.property;
39
+ context.report({
40
+ node: node.property,
41
+ message: "Use toHaveStyle instead of asserting on element style",
42
+
43
+ fix(fixer) {
44
+ return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${context.getSourceCode().getText(styleValue)}}`)];
45
+ }
46
+
47
+ });
48
+ },
49
+
50
+ //expect(el.style.foo).not.toBe("bar");
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) {
52
+ const styleName = node.parent.property;
53
+ const styleValue = node.parent.parent.parent.parent.parent.arguments[0];
54
+ const matcher = node.parent.parent.parent.parent.property;
55
+ context.report({
56
+ node: node.property,
57
+ message: "Use toHaveStyle instead of asserting on element style",
58
+
59
+ fix(fixer) {
60
+ return [fixer.removeRange([node.object.range[1], styleName.range[1]]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, `{${styleName.name}:${context.getSourceCode().getText(styleValue)}}`)];
61
+ }
62
+
63
+ });
64
+ },
65
+
66
+ // expect(el.style).toContain("foo-bar")
67
+ [`MemberExpression[property.name=style][parent.parent.property.name=toContain][parent.parent.parent.arguments.0.type=/(Template)?Literal$/][parent.callee.name=expect]`](node) {
68
+ const [styleName] = node.parent.parent.parent.arguments;
69
+ const matcher = node.parent.parent.property;
70
+ context.report({
71
+ node: node.property,
72
+ message: "Use toHaveStyle instead of asserting on element style",
73
+
74
+ fix(fixer) {
75
+ 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))];
76
+ }
77
+
78
+ });
79
+ },
80
+
81
+ // expect(el.style).not.toContain("foo-bar")
82
+ [`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) {
83
+ const [styleName] = node.parent.parent.parent.parent.arguments;
84
+ const matcher = node.parent.parent.parent.property;
85
+ context.report({
86
+ node: node.property,
87
+ message: "Use toHaveStyle instead of asserting on element style",
88
+
89
+ fix(fixer) {
90
+ 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))];
91
+ }
92
+
93
+ });
94
+ },
95
+
96
+ //expect(el).toHaveAttribute("style", "foo: bar");
97
+ [`CallExpression[callee.property.name=toHaveAttribute][arguments.0.value=style][arguments.1][callee.object.callee.name=expect]`](node) {
98
+ context.report({
99
+ node: node.arguments[0],
100
+ message: "Use toHaveStyle instead of asserting on element style",
101
+
102
+ fix(fixer) {
103
+ return [fixer.replaceText(node.callee.property, "toHaveStyle"), fixer.removeRange([node.arguments[0].range[0], node.arguments[1].range[0]])];
104
+ }
105
+
106
+ });
107
+ },
108
+
109
+ //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) {
111
+ const styleName = node.parent.property;
112
+ const [styleValue] = node.parent.parent.parent.parent.arguments;
113
+ const matcher = node.parent.parent.parent.property;
114
+ const startOfStyleMemberExpression = node.object.range[1];
115
+ const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1];
116
+ context.report({
117
+ node: node.property,
118
+ 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
+
124
+ });
125
+ },
126
+
127
+ //expect(el.style["foo-bar"]).not.toBe("baz")
128
+ [`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) {
129
+ const styleName = node.parent.property;
130
+ const [styleValue] = node.parent.parent.parent.parent.parent.arguments;
131
+ const matcher = node.parent.parent.parent.parent.property;
132
+ 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",
136
+
137
+ fix(fixer) {
138
+ return [fixer.removeRange([node.object.range[1], endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))];
139
+ }
140
+
141
+ });
142
+ },
143
+
144
+ //expect(foo.style).toHaveProperty("foo", "bar")
145
+ [`MemberExpression[property.name=style][parent.parent.property.name=toHaveProperty][parent.callee.name=expect]`](node) {
146
+ const [styleName, styleValue] = node.parent.parent.parent.arguments;
147
+ const matcher = node.parent.parent.property;
148
+ context.report({
149
+ node: node.property,
150
+ message: "Use toHaveStyle instead of asserting on element style",
151
+
152
+ fix(fixer) {
153
+ if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
154
+ return null;
155
+ }
156
+
157
+ 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)}}`)];
158
+ }
159
+
160
+ });
161
+ },
162
+
163
+ //expect(foo.style).not.toHaveProperty("foo", "bar")
164
+ [`MemberExpression[property.name=style][parent.parent.property.name=not][parent.parent.parent.property.name=toHaveProperty][parent.callee.name=expect]`](node) {
165
+ const [styleName, styleValue] = node.parent.parent.parent.parent.arguments;
166
+ const matcher = node.parent.parent.parent.property;
167
+ context.report({
168
+ node: node.property,
169
+ message: "Use toHaveStyle instead of asserting on element style",
170
+
171
+ fix(fixer) {
172
+ if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
173
+ return null;
174
+ }
175
+
176
+ 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)}}`)];
177
+ }
178
+
179
+ });
180
+ }
181
+
182
+ };
183
+ };
184
+
185
+ exports.create = create;
@@ -0,0 +1,64 @@
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 toHaveAttribute over checking getAttribute/hasAttribute
10
+ * @author Ben Monro
11
+ */
12
+ const meta = {
13
+ docs: {
14
+ category: "Best Practices",
15
+ url: "prefer-to-have-text-content",
16
+ description: "Prefer toHaveTextContent over checking element.textContent",
17
+ recommended: true
18
+ },
19
+ fixable: "code"
20
+ };
21
+ exports.meta = meta;
22
+
23
+ const create = context => ({
24
+ [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](node) {
25
+ const expectedArg = node.parent.parent.parent.arguments[0];
26
+ const expectedArgSource = context.getSourceCode().getText(expectedArg);
27
+ context.report({
28
+ node: node.parent,
29
+ message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
30
+ fix: fixer => {
31
+ 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})`)];
32
+ }
33
+ });
34
+ },
35
+
36
+ [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
37
+ context.report({
38
+ node: node.parent,
39
+ message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
40
+ fix: fixer => [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.property.range, "toHaveTextContent")]
41
+ });
42
+ },
43
+
44
+ [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
45
+ context.report({
46
+ node: node.parent,
47
+ message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
48
+ fix: fixer => [fixer.removeRange([node.object.range[1], node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.parent.property.range, "toHaveTextContent")]
49
+ });
50
+ },
51
+
52
+ [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toContain$|toMatch$/]`](node) {
53
+ const expectedArg = node.parent.parent.parent.parent.arguments[0];
54
+ const expectedArgSource = context.getSourceCode().getText(expectedArg);
55
+ context.report({
56
+ node: node.parent,
57
+ message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
58
+ 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
+ });
60
+ }
61
+
62
+ });
63
+
64
+ exports.create = create;
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.create = exports.meta = void 0;
7
+
8
+ var _assignmentAst = require("../assignment-ast");
9
+
10
+ /**
11
+ * @fileoverview prefer toHaveAttribute over checking getAttribute/hasAttribute
12
+ * @author Ben Monro
13
+ */
14
+ //------------------------------------------------------------------------------
15
+ // Rule Definition
16
+ //------------------------------------------------------------------------------
17
+ const meta = {
18
+ docs: {
19
+ category: "Best Practices",
20
+ description: "prefer toHaveValue over checking element.value",
21
+ url: "prefer-to-have-value",
22
+ recommended: true
23
+ },
24
+ fixable: "code",
25
+ messages: {
26
+ "use-to-have-value": `Prefer .toHaveValue() over other attribute checks`
27
+ }
28
+ };
29
+ exports.meta = meta;
30
+ const messageId = "use-to-have-value";
31
+
32
+ const create = context => {
33
+ function isValidQueryNode(nodeWithValueProp) {
34
+ const {
35
+ query,
36
+ queryArg,
37
+ isDTLQuery
38
+ } = (0, _assignmentAst.getQueryNodeFrom)(context, nodeWithValueProp);
39
+ return !!query && isDTLQuery && !!query.match(/^(get|find|query)(All)?ByRole$/) && ["textbox", "dropdown"].includes(queryArg);
40
+ }
41
+
42
+ return {
43
+ // expect(element.value).toBe('foo') / toEqual / toStrictEqual
44
+ // expect(<query>.value).toBe('foo') / toEqual / toStrictEqual
45
+ // expect((await <query>).value).toBe('foo') / toEqual / toStrictEqual
46
+ [`CallExpression[callee.property.name=/to(Be|(Strict)?Equal)$/][callee.object.arguments.0.property.name=value][callee.object.callee.name=expect]`](node) {
47
+ const valueProp = node.callee.object.arguments[0].property;
48
+ const matcher = node.callee.property;
49
+ const queryNode = node.callee.object.arguments[0].object;
50
+
51
+ if (isValidQueryNode(queryNode)) {
52
+ context.report({
53
+ messageId,
54
+ node,
55
+
56
+ fix(fixer) {
57
+ return [fixer.remove(context.getSourceCode().getTokenBefore(valueProp)), fixer.remove(valueProp), fixer.replaceText(matcher, "toHaveValue")];
58
+ }
59
+
60
+ });
61
+ }
62
+ },
63
+
64
+ // expect(element.value).not.toBe('foo') / toEqual / toStrictEqual
65
+ // expect(<query>.value).not.toBe('foo') / toEqual / toStrictEqual
66
+ // expect((await <query>).value).not.toBe('foo') / toEqual / toStrictEqual
67
+ [`CallExpression[callee.property.name=/to(Be|(Strict)?Equal)$/][callee.object.object.callee.name=expect][callee.object.property.name=not][callee.object.object.arguments.0.property.name=value]`](node) {
68
+ const queryNode = node.callee.object.object.arguments[0].object;
69
+ const valueProp = node.callee.object.object.arguments[0].property;
70
+ const matcher = node.callee.property;
71
+
72
+ if (isValidQueryNode(queryNode)) {
73
+ context.report({
74
+ messageId,
75
+ node,
76
+
77
+ fix(fixer) {
78
+ return [fixer.removeRange([context.getSourceCode().getTokenBefore(valueProp).range[0], valueProp.range[1]]), fixer.replaceText(matcher, "toHaveValue")];
79
+ }
80
+
81
+ });
82
+ }
83
+ },
84
+
85
+ //expect(element).toHaveAttribute('value', 'foo') / Property
86
+ [`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
+ const matcher = node.callee.property;
88
+ const [prop, value] = node.arguments;
89
+ context.report({
90
+ messageId,
91
+ node,
92
+
93
+ fix(fixer) {
94
+ return [fixer.replaceText(matcher, "toHaveValue"), fixer.removeRange([prop.range[0], value.range[0]])];
95
+ }
96
+
97
+ });
98
+ }
99
+
100
+ };
101
+ };
102
+
103
+ exports.create = create;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "3.9.1",
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": [