eslint-plugin-jest-dom 5.1.0 → 5.2.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
@@ -47,6 +47,17 @@ This library has a required `peerDependencies` listing for [`ESLint`](https://es
47
47
 
48
48
  ## Usage
49
49
 
50
+ > [!NOTE]
51
+ >
52
+ > `eslint.config.js` is supported, though most of the plugin documentation still
53
+ > currently uses `.eslintrc` syntax; compatible versions of configs are available
54
+ > prefixed with `flat/` and may be subject to small breaking changes while ESLint
55
+ > v9 is being finalized.
56
+ >
57
+ > Refer to the
58
+ > [ESLint documentation on the new configuration file format](https://eslint.org/docs/latest/use/configure/configuration-files-new)
59
+ > for more.
60
+
50
61
  Add `jest-dom` to the plugins section of your `.eslintrc.js` configuration file.
51
62
  You can omit the `eslint-plugin-` prefix:
52
63
 
@@ -78,8 +89,7 @@ This plugin exports a recommended configuration that enforces good `jest-dom`
78
89
  practices _(you can find more info about enabled rules in
79
90
  [Supported Rules section](#supported-rules))_.
80
91
 
81
- To enable this configuration use the `extends` property in your `.eslintrc.js`
82
- config file:
92
+ To enable this configuration with `.eslintrc`, use the `extends` property:
83
93
 
84
94
  ```javascript
85
95
  module.exports = {
@@ -90,6 +100,20 @@ module.exports = {
90
100
  };
91
101
  ```
92
102
 
103
+ To enable this configuration with `eslint.config.js`, use
104
+ `jestDom.configs['flat/recommended']`:
105
+
106
+ ```javascript
107
+ module.exports = [
108
+ {
109
+ files: [
110
+ /* glob matching your test files */
111
+ ],
112
+ ...require("eslint-plugin-jest-dom").configs["flat/recommended"],
113
+ },
114
+ ];
115
+ ```
116
+
93
117
  ## Supported Rules
94
118
 
95
119
  <!-- begin auto-generated rules list -->
@@ -97,7 +121,7 @@ module.exports = {
97
121
  💼 Configurations enabled in.\
98
122
  ✅ Set in the `recommended` configuration.\
99
123
  🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
100
- 💡 Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
124
+ 💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
101
125
 
102
126
  | Name                        | Description | 💼 | 🔧 | 💡 |
103
127
  | :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- | :- |
package/dist/index.js CHANGED
@@ -4,8 +4,9 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.rules = exports.generateRecommendedConfig = exports.generateAllRulesConfig = exports.configs = void 0;
7
+ exports.rules = exports.default = exports.configs = void 0;
8
8
  var _requireindex = _interopRequireDefault(require("requireindex"));
9
+ var _package = require("../package.json");
9
10
  /**
10
11
  * @fileoverview lint rules for use with jest-dom
11
12
  * @author Ben Monro
@@ -19,31 +20,45 @@ var _requireindex = _interopRequireDefault(require("requireindex"));
19
20
  // Plugin Definition
20
21
  //------------------------------------------------------------------------------
21
22
 
22
- // import all rules in src/rules
23
- const rules = (0, _requireindex.default)(`${__dirname}/rules`);
24
- exports.rules = rules;
25
- const generateRecommendedConfig = allRules => Object.entries(allRules).reduce((memo, [name, rule]) => ({
26
- ...memo,
27
- ...(rule.meta.docs.recommended ? {
28
- [`jest-dom/${name}`]: "error"
29
- } : {})
30
- }), {});
31
- exports.generateRecommendedConfig = generateRecommendedConfig;
32
- const generateAllRulesConfig = allRules => Object.entries(allRules).reduce((memo, [name]) => ({
23
+ // import all rules in src/rules and re-export them for .eslintrc configs
24
+ const rules = exports.rules = (0, _requireindex.default)(`${__dirname}/rules`);
25
+ const allRules = Object.entries(rules).reduce((memo, [name]) => ({
33
26
  ...memo,
34
27
  ...{
35
28
  [`jest-dom/${name}`]: "error"
36
29
  }
37
30
  }), {});
38
- exports.generateAllRulesConfig = generateAllRulesConfig;
39
- const configs = {
40
- recommended: {
41
- plugins: ["jest-dom"],
42
- rules: generateRecommendedConfig(rules)
31
+ const recommendedRules = allRules;
32
+ const plugin = {
33
+ meta: {
34
+ name: _package.name,
35
+ version: _package.version
43
36
  },
44
- all: {
45
- plugins: ["jest-dom"],
46
- rules: generateAllRulesConfig(rules)
47
- }
37
+ configs: {
38
+ recommended: {
39
+ plugins: ["jest-dom"],
40
+ rules: recommendedRules
41
+ },
42
+ all: {
43
+ plugins: ["jest-dom"],
44
+ rules: allRules
45
+ }
46
+ },
47
+ rules
48
+ };
49
+ plugin.configs["flat/recommended"] = {
50
+ plugins: {
51
+ "jest-dom": plugin
52
+ },
53
+ rules: recommendedRules
54
+ };
55
+ plugin.configs["flat/all"] = {
56
+ plugins: {
57
+ "jest-dom": plugin
58
+ },
59
+ rules: allRules
48
60
  };
49
- exports.configs = configs;
61
+ var _default = exports.default = plugin; // explicitly export config to allow using this plugin in CJS-based
62
+ // eslint.config.js files without needing to deal with the .default
63
+ // and also retain backwards compatibility with `.eslintrc` configs
64
+ const configs = exports.configs = plugin.configs;
package/dist/queries.js CHANGED
@@ -17,5 +17,4 @@ let theQueries = ["findAllBy", "findBy", "getAllBy", "getBy", "queryAllBy", "que
17
17
  }
18
18
  }
19
19
  })();
20
- const queries = theQueries;
21
- exports.queries = queries;
20
+ const queries = exports.queries = theQueries;
@@ -11,7 +11,7 @@ var _createBannedAttributeRule = _interopRequireDefault(require("../createBanned
11
11
  * @author Ben Monro
12
12
  */
13
13
 
14
- const meta = {
14
+ const meta = exports.meta = {
15
15
  docs: {
16
16
  description: "prefer toBeChecked over checking attributes",
17
17
  category: "Best Practices",
@@ -20,10 +20,8 @@ const meta = {
20
20
  },
21
21
  fixable: "code"
22
22
  };
23
- exports.meta = meta;
24
- const create = (0, _createBannedAttributeRule.default)({
23
+ const create = exports.create = (0, _createBannedAttributeRule.default)({
25
24
  preferred: "toBeChecked",
26
25
  negatedPreferred: "not.toBeChecked",
27
26
  attributes: ["checked", "aria-checked"]
28
- });
29
- exports.create = create;
27
+ });
@@ -9,7 +9,7 @@ exports.meta = exports.create = void 0;
9
9
  * @author Ben Monro
10
10
  */
11
11
 
12
- const meta = {
12
+ const meta = exports.meta = {
13
13
  docs: {
14
14
  description: "Prefer toBeEmpty over checking innerHTML",
15
15
  category: "Best Practices",
@@ -18,7 +18,6 @@ const meta = {
18
18
  },
19
19
  fixable: "code" // or "code" or "whitespace"
20
20
  };
21
- exports.meta = meta;
22
21
  const create = context => {
23
22
  function isNonEmptyStringOrTemplateLiteral(node) {
24
23
  return !['""', "''", "``", "null"].includes(context.getSourceCode().getText(node));
@@ -11,7 +11,7 @@ var _createBannedAttributeRule = _interopRequireDefault(require("../createBanned
11
11
  * @author Ben Monro
12
12
  */
13
13
 
14
- const meta = {
14
+ const meta = exports.meta = {
15
15
  docs: {
16
16
  description: "prefer toBeDisabled or toBeEnabled over checking attributes",
17
17
  category: "Best Practices",
@@ -20,10 +20,8 @@ const meta = {
20
20
  },
21
21
  fixable: "code"
22
22
  };
23
- exports.meta = meta;
24
- const create = (0, _createBannedAttributeRule.default)({
23
+ const create = exports.create = (0, _createBannedAttributeRule.default)({
25
24
  preferred: "toBeDisabled",
26
25
  negatedPreferred: "toBeEnabled",
27
26
  attributes: ["disabled"]
28
- });
29
- exports.create = create;
27
+ });
@@ -16,7 +16,7 @@ const variantsOfDoc = [
16
16
  `[object.object.name=/(global|window)$/][object.property.name=document]`,
17
17
  // global.window.document:
18
18
  `[object.object.object.name='global'][object.object.property.name='window'][object.property.name=document]`];
19
- const meta = {
19
+ const meta = exports.meta = {
20
20
  docs: {
21
21
  url: "prefer-focus",
22
22
  description: "prefer toHaveFocus over checking document.activeElement",
@@ -25,7 +25,6 @@ const meta = {
25
25
  },
26
26
  fixable: "code"
27
27
  };
28
- exports.meta = meta;
29
28
  const create = context => ({
30
29
  [variantsOfDoc.map(variant => `MemberExpression${variant}[property.name='activeElement'][parent.parent.object.callee.name='expect'][parent.parent.property.name='not'][parent.parent.parent.property.name=/to(Be|(Strict)?Equal)$/]`).join(", ")](node) {
31
30
  const element = node.parent.parent.parent.parent.callee.parent.arguments[0];
@@ -13,7 +13,7 @@ var _assignmentAst = require("../assignment-ast");
13
13
 
14
14
  /*eslint complexity: ["error", {"max": 20}]*/
15
15
 
16
- const meta = {
16
+ const meta = exports.meta = {
17
17
  type: "suggestion",
18
18
  docs: {
19
19
  category: "Best Practices",
@@ -29,7 +29,6 @@ const meta = {
29
29
  },
30
30
  hasSuggestions: true
31
31
  };
32
- exports.meta = meta;
33
32
  function isAntonymMatcher(matcherNode, matcherArguments) {
34
33
  return matcherNode.name === "toBeNull" || matcherNode.name === "toBeFalsy" || usesToBeOrToEqualWithNull(matcherNode, matcherArguments) || usesToHaveLengthZero(matcherNode, matcherArguments);
35
34
  }
@@ -11,7 +11,7 @@ var _createBannedAttributeRule = _interopRequireDefault(require("../createBanned
11
11
  * @author Ben Monro
12
12
  */
13
13
 
14
- const meta = {
14
+ const meta = exports.meta = {
15
15
  docs: {
16
16
  description: "prefer toBeRequired over checking properties",
17
17
  category: "Best Practices",
@@ -20,10 +20,8 @@ const meta = {
20
20
  },
21
21
  fixable: "code"
22
22
  };
23
- exports.meta = meta;
24
- const create = (0, _createBannedAttributeRule.default)({
23
+ const create = exports.create = (0, _createBannedAttributeRule.default)({
25
24
  preferred: "toBeRequired",
26
25
  negatedPreferred: "not.toBeRequired",
27
26
  attributes: ["required", "aria-required"]
28
- });
29
- exports.create = create;
27
+ });
@@ -13,7 +13,7 @@ exports.meta = exports.create = void 0;
13
13
  // Rule Definition
14
14
  //------------------------------------------------------------------------------
15
15
 
16
- const meta = {
16
+ const meta = exports.meta = {
17
17
  docs: {
18
18
  category: "Best Practices",
19
19
  description: "prefer toHaveAttribute over checking getAttribute/hasAttribute ",
@@ -22,7 +22,6 @@ const meta = {
22
22
  },
23
23
  fixable: "code"
24
24
  };
25
- exports.meta = meta;
26
25
  const create = context => ({
27
26
  [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeNull/]`](node) {
28
27
  context.report({
@@ -13,8 +13,9 @@ var _assignmentAst = require("../assignment-ast");
13
13
  //------------------------------------------------------------------------------
14
14
  // Rule Definition
15
15
  //------------------------------------------------------------------------------
16
+
16
17
  const messageId = "use-to-have-class";
17
- const meta = {
18
+ const meta = exports.meta = {
18
19
  docs: {
19
20
  category: "Best Practices",
20
21
  url: "prefer-to-have-class",
@@ -26,7 +27,6 @@ const meta = {
26
27
  },
27
28
  fixable: "code"
28
29
  };
29
- exports.meta = meta;
30
30
  const create = context => ({
31
31
  //expect(el.classList.contains("foo")).toBe(true)
32
32
  [`CallExpression[callee.object.callee.name=expect][callee.object.arguments.0.callee.object.property.name=classList][callee.object.arguments.0.callee.property.name=contains][callee.property.name=/toBe(Truthy|Falsy)?|to(Strict)?Equal/]`](node) {
@@ -13,7 +13,7 @@ exports.meta = exports.create = void 0;
13
13
  // Rule Definition
14
14
  //------------------------------------------------------------------------------
15
15
  const camelCase = str => str.replace(/-([a-z])/g, c => c[1].toUpperCase());
16
- const meta = {
16
+ const meta = exports.meta = {
17
17
  docs: {
18
18
  category: "Best Practices",
19
19
  url: "prefer-to-have-style",
@@ -22,7 +22,6 @@ const meta = {
22
22
  },
23
23
  fixable: "code"
24
24
  };
25
- exports.meta = meta;
26
25
  const create = context => {
27
26
  function getReplacementObjectProperty(styleName) {
28
27
  if (styleName.type === "Literal") {
@@ -142,7 +141,7 @@ const create = context => {
142
141
  if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
143
142
  return null;
144
143
  }
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)}}`)];
144
+ 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)}}`)];
146
145
  }
147
146
  });
148
147
  },
@@ -157,7 +156,7 @@ const create = context => {
157
156
  if (!styleValue || !["Literal", "TemplateLiteral"].includes(styleValue.type)) {
158
157
  return null;
159
158
  }
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)}}`)];
159
+ 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)}}`)];
161
160
  }
162
161
  });
163
162
  }
@@ -9,7 +9,7 @@ exports.meta = exports.create = void 0;
9
9
  * @author Ben Monro
10
10
  */
11
11
 
12
- const meta = {
12
+ const meta = exports.meta = {
13
13
  docs: {
14
14
  category: "Best Practices",
15
15
  url: "prefer-to-have-text-content",
@@ -18,7 +18,6 @@ const meta = {
18
18
  },
19
19
  fixable: "code"
20
20
  };
21
- exports.meta = meta;
22
21
  const create = context => ({
23
22
  [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](node) {
24
23
  const expectedArg = node.parent.parent.parent.arguments[0];
@@ -14,7 +14,7 @@ var _assignmentAst = require("../assignment-ast");
14
14
  // Rule Definition
15
15
  //------------------------------------------------------------------------------
16
16
 
17
- const meta = {
17
+ const meta = exports.meta = {
18
18
  docs: {
19
19
  category: "Best Practices",
20
20
  description: "prefer toHaveValue over checking element.value",
@@ -26,7 +26,6 @@ const meta = {
26
26
  "use-to-have-value": `Prefer .toHaveValue() over other attribute checks`
27
27
  }
28
28
  };
29
- exports.meta = meta;
30
29
  const messageId = "use-to-have-value";
31
30
  const create = context => {
32
31
  function isValidQueryNode(nodeWithValueProp) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "5.1.0",
3
+ "version": "5.2.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": [
@@ -31,7 +31,7 @@
31
31
  "scripts": {
32
32
  "build": "kcd-scripts build",
33
33
  "pregenerate-readme-table": "npm run build",
34
- "generate-readme-table": "eslint-doc-generator --ignore-config all",
34
+ "generate-readme-table": "eslint-doc-generator",
35
35
  "lint": "kcd-scripts lint",
36
36
  "lint:generate-readme-table": "npm run generate-readme-table -- --check",
37
37
  "setup": "npm install && npm run validate -s",