eslint-plugin-jest-dom 2.0.1 → 2.1.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/.eslintrc.json CHANGED
@@ -6,17 +6,16 @@
6
6
  "commonjs": true,
7
7
  "es6": true,
8
8
  "node": true
9
- // "jest/globals": true
10
9
  },
11
10
  "plugins": [
12
11
  "eslint-plugin",
13
12
  "prettier"
14
13
  ],
15
14
  "extends": [
16
- "plugin:eslint-plugin/recommended"
15
+ "plugin:eslint-plugin/recommended",
16
+ "prettier"
17
17
  ],
18
18
  "rules": {
19
- "prettier/prettier": "error",
20
19
  "no-var": "error"
21
20
  }
22
- }
21
+ }
package/README.md CHANGED
@@ -74,6 +74,7 @@ Name | ✔️ | 🛠 | Description
74
74
  [prefer-focus](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-focus.md) | ✔️ | 🛠 | prefer toHaveFocus over checking document.activeElement
75
75
  [prefer-required](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-required.md) | ✔️ | 🛠 | prefer toBeRequired over checking properties
76
76
  [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
77
+ [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
77
78
  <!-- __END AUTOGENERATED TABLE__ -->
78
79
 
79
80
  ## Contributors ✨
@@ -1,12 +1,12 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
- const fs = require('fs');
4
- const path = require('path');
5
- const rules = require('..').rules;
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const rules = require("..").rules;
6
6
 
7
- const README_LOCATION = path.resolve(__dirname, '..', 'README.md');
8
- const BEGIN_TABLE_MARKER = '<!-- __BEGIN AUTOGENERATED TABLE__ -->\n';
9
- const END_TABLE_MARKER = '\n<!-- __END AUTOGENERATED TABLE__ -->';
7
+ const README_LOCATION = path.resolve(__dirname, "..", "README.md");
8
+ const BEGIN_TABLE_MARKER = "<!-- __BEGIN AUTOGENERATED TABLE__ -->\n";
9
+ const END_TABLE_MARKER = "\n<!-- __END AUTOGENERATED TABLE__ -->";
10
10
 
11
11
  const expectedTableLines = Object.keys(rules)
12
12
  .sort()
@@ -17,19 +17,19 @@ const expectedTableLines = Object.keys(rules)
17
17
  lines.push(
18
18
  [
19
19
  `[${ruleId}](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/${ruleId}.md)`,
20
- rule.meta.docs.recommended ? '✔️' : '',
21
- rule.meta.fixable ? '🛠' : '',
22
- rule.meta.docs.description,
23
- ].join(' | ')
20
+ rule.meta.docs.recommended ? "✔️" : "",
21
+ rule.meta.fixable ? "🛠" : "",
22
+ rule.meta.docs.description
23
+ ].join(" | ")
24
24
  );
25
25
 
26
26
  return lines;
27
27
  },
28
- ['Name | ✔️ | 🛠 | Description', '----- | ----- | ----- | -----']
28
+ ["Name | ✔️ | 🛠 | Description", "----- | ----- | ----- | -----"]
29
29
  )
30
- .join('\n');
30
+ .join("\n");
31
31
 
32
- const readmeContents = fs.readFileSync(README_LOCATION, 'utf8');
32
+ const readmeContents = fs.readFileSync(README_LOCATION, "utf8");
33
33
 
34
34
  if (!readmeContents.includes(BEGIN_TABLE_MARKER)) {
35
35
  throw new Error(
@@ -0,0 +1,40 @@
1
+ # Prefer toHaveTextContent over checking element.textContent (prefer-to-have-text-content)
2
+
3
+ Please describe the origin of the rule here.
4
+
5
+ ## Rule Details
6
+
7
+ This rule aims to prevent checking of the `textContent` DOM node attribute in tests and prefer `toHaveTextContent` instead.
8
+
9
+ Examples of **incorrect** code for this rule:
10
+
11
+ ```js
12
+ expect(element.textContent).toBe("foo")
13
+ expect(container.firstChild.textContent).toBe("foo")
14
+ expect(getByText('foo').textContent).toBe("foo")
15
+ expect(screen.getByText('foo').textContent).toBe("foo")
16
+ expect(element.textContent).toEqual("foo")
17
+ expect(element.textContent).toContain("foo")
18
+ expect(element.textContent).not.toBe("foo")
19
+ expect(element.textContent).not.toContain("foo")
20
+
21
+ ```
22
+
23
+ Examples of **correct** code for this rule:
24
+
25
+ ```js
26
+
27
+
28
+ expect(string).toBe("foo"),
29
+ expect(element).toHaveTextContent("foo"),
30
+ expect(container.lastNode).toBe("foo")
31
+
32
+ ```
33
+
34
+ ## When Not To Use It
35
+
36
+ Don't use this rule if you don't care about the added readability and improvements that `toHaveTextContent` offers to your expects.
37
+
38
+ ## Further Reading
39
+
40
+ * [Docs on toHaveContent](https://github.com/testing-library/jest-dom#tohavetextcontent)
package/jest.config.js CHANGED
@@ -1,14 +1,14 @@
1
1
  module.exports = {
2
- testMatch: ['**/tests/**/*.js'],
2
+ testMatch: ["**/tests/**/*.js"],
3
3
  collectCoverage: true,
4
4
  coverageThreshold: {
5
5
  global: {
6
6
  branches: 96.55,
7
7
  functions: 100,
8
8
  lines: 98.97,
9
- statements: 0,
10
- },
9
+ statements: 0
10
+ }
11
11
  },
12
- testPathIgnorePatterns: ['<rootDir>/tests/fixtures/'],
13
- collectCoverageFrom: ['lib/**/*.js', '!**/node_modules/**'],
12
+ testPathIgnorePatterns: ["<rootDir>/tests/fixtures/"],
13
+ collectCoverageFrom: ["lib/**/*.js", "!**/node_modules/**"]
14
14
  };
@@ -2,7 +2,7 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
2
2
  function getCorrectFunctionFor(node, negated = false) {
3
3
  return (node.arguments.length === 1 ||
4
4
  node.arguments[1].value === true ||
5
- node.arguments[1].value === '') &&
5
+ node.arguments[1].value === "") &&
6
6
  !negated
7
7
  ? preferred
8
8
  : negatedPreferred;
@@ -15,7 +15,7 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
15
15
  [`CallExpression[callee.property.name=/${preferred}|${negatedPreferred}/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](
16
16
  node
17
17
  ) {
18
- if (negatedPreferred.startsWith('toBe')) {
18
+ if (negatedPreferred.startsWith("toBe")) {
19
19
  const incorrectFunction = node.callee.property.name;
20
20
 
21
21
  const correctFunction =
@@ -28,7 +28,7 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
28
28
  [node.callee.object.property.range[0], node.range[1]],
29
29
  `${correctFunction}()`
30
30
  );
31
- },
31
+ }
32
32
  });
33
33
  }
34
34
  },
@@ -37,14 +37,14 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
37
37
  node
38
38
  ) {
39
39
  const {
40
- arguments: [{ property, property: { name } = {} }],
40
+ arguments: [{ property, property: { name } = {} }]
41
41
  } = node.callee.object;
42
42
  const matcher = node.callee.property.name;
43
43
  const matcherArg = node.arguments.length && node.arguments[0].value;
44
44
  if (attributes.some(attr => attr === name)) {
45
45
  const isNegated =
46
- matcher.endsWith('Falsy') ||
47
- ((matcher === 'toBe' || matcher === 'toEqual') &&
46
+ matcher.endsWith("Falsy") ||
47
+ ((matcher === "toBe" || matcher === "toEqual") &&
48
48
  matcherArg !== true);
49
49
  const correctFunction = getCorrectFunctionFor(
50
50
  node.callee.object,
@@ -59,9 +59,9 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
59
59
  fixer.replaceTextRange(
60
60
  [node.callee.property.range[0], node.range[1]],
61
61
  `${correctFunction}()`
62
- ),
62
+ )
63
63
  ];
64
- },
64
+ }
65
65
  });
66
66
  }
67
67
  },
@@ -81,7 +81,7 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
81
81
  [node.callee.object.property.range[0], node.range[1]],
82
82
  `${correctFunction}()`
83
83
  );
84
- },
84
+ }
85
85
  });
86
86
  }
87
87
  },
@@ -95,7 +95,7 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
95
95
 
96
96
  const message = `Use ${correctFunction}() instead of ${incorrectFunction}(${node.arguments
97
97
  .map(({ raw }) => raw)
98
- .join(', ')})`;
98
+ .join(", ")})`;
99
99
  context.report({
100
100
  node: node.callee.property,
101
101
  message,
@@ -104,11 +104,11 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => context => {
104
104
  fixer.replaceTextRange(
105
105
  [node.callee.property.range[0], node.range[1]],
106
106
  `${correctFunction}()`
107
- ),
107
+ )
108
108
  ];
109
- },
109
+ }
110
110
  });
111
111
  }
112
- },
112
+ }
113
113
  };
114
114
  };
package/lib/index.js CHANGED
@@ -2,26 +2,26 @@
2
2
  * @fileoverview lint rules for use with jest-dom
3
3
  * @author Ben Monro
4
4
  */
5
- 'use strict';
5
+ "use strict";
6
6
 
7
7
  //------------------------------------------------------------------------------
8
8
  // Requirements
9
9
  //------------------------------------------------------------------------------
10
10
 
11
- let requireIndex = require('requireindex');
11
+ let requireIndex = require("requireindex");
12
12
 
13
13
  //------------------------------------------------------------------------------
14
14
  // Plugin Definition
15
15
  //------------------------------------------------------------------------------
16
16
 
17
17
  // import all rules in lib/rules
18
- module.exports.rules = requireIndex(__dirname + '/rules');
18
+ module.exports.rules = requireIndex(__dirname + "/rules");
19
19
 
20
20
  module.exports.generateRecommendedConfig = rules => {
21
21
  return Object.entries(rules).reduce(
22
22
  (memo, [name, rule]) =>
23
23
  rule.meta.docs.recommended
24
- ? { ...memo, [`jest-dom/${name}`]: 'error' }
24
+ ? { ...memo, [`jest-dom/${name}`]: "error" }
25
25
  : memo,
26
26
  {}
27
27
  );
@@ -29,7 +29,7 @@ module.exports.generateRecommendedConfig = rules => {
29
29
 
30
30
  module.exports.configs = {
31
31
  recommended: {
32
- plugins: ['jest-dom'],
33
- rules: module.exports.generateRecommendedConfig(module.exports.rules),
34
- },
32
+ plugins: ["jest-dom"],
33
+ rules: module.exports.generateRecommendedConfig(module.exports.rules)
34
+ }
35
35
  };
@@ -2,24 +2,24 @@
2
2
  * @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks
3
3
  * @author Ben Monro
4
4
  */
5
- 'use strict';
5
+ "use strict";
6
6
 
7
- const createBannedAttributeRule = require('../createBannedAttributeRule');
7
+ const createBannedAttributeRule = require("../createBannedAttributeRule");
8
8
 
9
9
  module.exports = {
10
10
  meta: {
11
11
  docs: {
12
- description: 'prefer toBeChecked over checking attributes',
13
- category: 'jest-dom',
12
+ description: "prefer toBeChecked over checking attributes",
13
+ category: "jest-dom",
14
14
  recommended: true,
15
- url: 'prefer-checked',
15
+ url: "prefer-checked"
16
16
  },
17
- fixable: 'code',
17
+ fixable: "code"
18
18
  },
19
19
 
20
20
  create: createBannedAttributeRule({
21
- preferred: 'toBeChecked',
22
- negatedPreferred: 'not.toBeChecked',
23
- attributes: ['checked', 'aria-checked'],
24
- }),
21
+ preferred: "toBeChecked",
22
+ negatedPreferred: "not.toBeChecked",
23
+ attributes: ["checked", "aria-checked"]
24
+ })
25
25
  };
@@ -6,12 +6,12 @@
6
6
  module.exports = {
7
7
  meta: {
8
8
  docs: {
9
- description: 'Prefer toBeEmpty over checking innerHTML',
10
- category: 'jest-dom',
9
+ description: "Prefer toBeEmpty over checking innerHTML",
10
+ category: "jest-dom",
11
11
  recommended: true,
12
- url: 'prefer-empty',
12
+ url: "prefer-empty"
13
13
  },
14
- fixable: 'code', // or "code" or "whitespace"
14
+ fixable: "code" // or "code" or "whitespace"
15
15
  },
16
16
 
17
17
  create: function(context) {
@@ -21,23 +21,23 @@ module.exports = {
21
21
  ) {
22
22
  context.report({
23
23
  node,
24
- message: 'Use toBeEmpty instead of checking inner html.',
24
+ message: "Use toBeEmpty instead of checking inner html.",
25
25
  fix(fixer) {
26
26
  return [
27
27
  fixer.removeRange([
28
28
  node.left.property.range[0] - 1,
29
- node.range[1],
29
+ node.range[1]
30
30
  ]),
31
31
  fixer.replaceText(
32
32
  node.parent.parent.property,
33
33
  !!node.parent.parent.parent.arguments[0].value ===
34
- node.operator.startsWith('=') // binary expression XNOR matcher boolean
35
- ? 'toBeEmpty'
36
- : 'not.toBeEmpty'
34
+ node.operator.startsWith("=") // binary expression XNOR matcher boolean
35
+ ? "toBeEmpty"
36
+ : "not.toBeEmpty"
37
37
  ),
38
- fixer.remove(node.parent.parent.parent.arguments[0]),
38
+ fixer.remove(node.parent.parent.parent.arguments[0])
39
39
  ];
40
- },
40
+ }
41
41
  });
42
42
  },
43
43
  [`BinaryExpression[left.property.name='firstChild'][right.value=null][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](
@@ -45,23 +45,23 @@ module.exports = {
45
45
  ) {
46
46
  context.report({
47
47
  node,
48
- message: 'Use toBeEmpty instead of checking inner html.',
48
+ message: "Use toBeEmpty instead of checking inner html.",
49
49
  fix(fixer) {
50
50
  return [
51
51
  fixer.removeRange([
52
52
  node.left.property.range[0] - 1,
53
- node.range[1],
53
+ node.range[1]
54
54
  ]),
55
55
  fixer.replaceText(
56
56
  node.parent.parent.property,
57
57
  !!node.parent.parent.parent.arguments[0].value ===
58
- node.operator.startsWith('=') // binary expression XNOR matcher boolean
59
- ? 'toBeEmpty'
60
- : 'not.toBeEmpty'
58
+ node.operator.startsWith("=") // binary expression XNOR matcher boolean
59
+ ? "toBeEmpty"
60
+ : "not.toBeEmpty"
61
61
  ),
62
- fixer.remove(node.parent.parent.parent.arguments[0]),
62
+ fixer.remove(node.parent.parent.parent.arguments[0])
63
63
  ];
64
- },
64
+ }
65
65
  });
66
66
  },
67
67
  [`MemberExpression[property.name = 'innerHTML'][parent.callee.name = 'expect'][parent.parent.property.name = /toBe$|to(Strict)?Equal/]`](
@@ -70,17 +70,17 @@ module.exports = {
70
70
  if (!node.parent.parent.parent.arguments[0].value) {
71
71
  context.report({
72
72
  node,
73
- message: 'Use toBeEmpty instead of checking inner html.',
73
+ message: "Use toBeEmpty instead of checking inner html.",
74
74
  fix(fixer) {
75
75
  return [
76
76
  fixer.removeRange([
77
77
  node.property.range[0] - 1,
78
- node.property.range[1],
78
+ node.property.range[1]
79
79
  ]),
80
- fixer.replaceText(node.parent.parent.property, 'toBeEmpty'),
81
- fixer.remove(node.parent.parent.parent.arguments[0]),
80
+ fixer.replaceText(node.parent.parent.property, "toBeEmpty"),
81
+ fixer.remove(node.parent.parent.parent.arguments[0])
82
82
  ];
83
- },
83
+ }
84
84
  });
85
85
  }
86
86
  },
@@ -90,20 +90,20 @@ module.exports = {
90
90
  if (!node.parent.parent.parent.parent.arguments[0].value) {
91
91
  context.report({
92
92
  node,
93
- message: 'Use toBeEmpty instead of checking inner html.',
93
+ message: "Use toBeEmpty instead of checking inner html.",
94
94
  fix(fixer) {
95
95
  return [
96
96
  fixer.removeRange([
97
97
  node.property.range[0] - 1,
98
- node.property.range[1],
98
+ node.property.range[1]
99
99
  ]),
100
100
  fixer.replaceText(
101
101
  node.parent.parent.parent.property,
102
- 'toBeEmpty'
102
+ "toBeEmpty"
103
103
  ),
104
- fixer.remove(node.parent.parent.parent.parent.arguments[0]),
104
+ fixer.remove(node.parent.parent.parent.parent.arguments[0])
105
105
  ];
106
- },
106
+ }
107
107
  });
108
108
  }
109
109
  },
@@ -112,16 +112,16 @@ module.exports = {
112
112
  ) {
113
113
  context.report({
114
114
  node,
115
- message: 'Use toBeEmpty instead of checking inner html.',
115
+ message: "Use toBeEmpty instead of checking inner html.",
116
116
  fix(fixer) {
117
117
  return [
118
118
  fixer.removeRange([
119
119
  node.property.range[0] - 1,
120
- node.property.range[1],
120
+ node.property.range[1]
121
121
  ]),
122
- fixer.replaceText(node.parent.parent.property, 'toBeEmpty'),
122
+ fixer.replaceText(node.parent.parent.property, "toBeEmpty")
123
123
  ];
124
- },
124
+ }
125
125
  });
126
126
  },
127
127
 
@@ -131,20 +131,20 @@ module.exports = {
131
131
  if (node.parent.parent.parent.parent.arguments[0].value === null) {
132
132
  context.report({
133
133
  node,
134
- message: 'Use toBeEmpty instead of checking inner html.',
134
+ message: "Use toBeEmpty instead of checking inner html.",
135
135
  fix(fixer) {
136
136
  return [
137
137
  fixer.removeRange([
138
138
  node.property.range[0] - 1,
139
- node.property.range[1],
139
+ node.property.range[1]
140
140
  ]),
141
141
  fixer.replaceText(
142
142
  node.parent.parent.parent.property,
143
- 'toBeEmpty'
143
+ "toBeEmpty"
144
144
  ),
145
- fixer.remove(node.parent.parent.parent.parent.arguments[0]),
145
+ fixer.remove(node.parent.parent.parent.parent.arguments[0])
146
146
  ];
147
- },
147
+ }
148
148
  });
149
149
  }
150
150
  },
@@ -154,19 +154,16 @@ module.exports = {
154
154
  ) {
155
155
  context.report({
156
156
  node,
157
- message: 'Use toBeEmpty instead of checking inner html.',
157
+ message: "Use toBeEmpty instead of checking inner html.",
158
158
  fix(fixer) {
159
159
  return [
160
160
  fixer.removeRange([
161
161
  node.property.range[0] - 1,
162
- node.property.range[1],
162
+ node.property.range[1]
163
163
  ]),
164
- fixer.replaceText(
165
- node.parent.parent.parent.property,
166
- 'toBeEmpty'
167
- ),
164
+ fixer.replaceText(node.parent.parent.parent.property, "toBeEmpty")
168
165
  ];
169
- },
166
+ }
170
167
  });
171
168
  },
172
169
  [`MemberExpression[property.name = 'firstChild'][parent.callee.name = 'expect'][parent.parent.property.name = /toBe$|to(Strict)?Equal/]`](
@@ -175,20 +172,20 @@ module.exports = {
175
172
  if (node.parent.parent.parent.arguments[0].value === null) {
176
173
  context.report({
177
174
  node,
178
- message: 'Use toBeEmpty instead of checking inner html.',
175
+ message: "Use toBeEmpty instead of checking inner html.",
179
176
  fix(fixer) {
180
177
  return [
181
178
  fixer.removeRange([
182
179
  node.property.range[0] - 1,
183
- node.property.range[1],
180
+ node.property.range[1]
184
181
  ]),
185
- fixer.replaceText(node.parent.parent.property, 'toBeEmpty'),
186
- fixer.remove(node.parent.parent.parent.arguments[0]),
182
+ fixer.replaceText(node.parent.parent.property, "toBeEmpty"),
183
+ fixer.remove(node.parent.parent.parent.arguments[0])
187
184
  ];
188
- },
185
+ }
189
186
  });
190
187
  }
191
- },
188
+ }
192
189
  };
193
- },
190
+ }
194
191
  };
@@ -2,25 +2,25 @@
2
2
  * @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks
3
3
  * @author Ben Monro
4
4
  */
5
- 'use strict';
5
+ "use strict";
6
6
 
7
- const createBannedAttributeRule = require('../createBannedAttributeRule');
7
+ const createBannedAttributeRule = require("../createBannedAttributeRule");
8
8
 
9
9
  module.exports = {
10
10
  meta: {
11
11
  docs: {
12
12
  description:
13
- 'prefer toBeDisabled or toBeEnabled over checking attributes',
14
- category: 'jest-dom',
13
+ "prefer toBeDisabled or toBeEnabled over checking attributes",
14
+ category: "jest-dom",
15
15
  recommended: true,
16
- url: 'prefer-enabled-disabled',
16
+ url: "prefer-enabled-disabled"
17
17
  },
18
- fixable: 'code',
18
+ fixable: "code"
19
19
  },
20
20
 
21
21
  create: createBannedAttributeRule({
22
- preferred: 'toBeDisabled',
23
- negatedPreferred: 'toBeEnabled',
24
- attributes: ['disabled'],
25
- }),
22
+ preferred: "toBeDisabled",
23
+ negatedPreferred: "toBeEnabled",
24
+ attributes: ["disabled"]
25
+ })
26
26
  };