eslint-plugin-jest-dom 5.10.1 → 5.10.3

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.
@@ -14,6 +14,24 @@ var _context = require("../context");
14
14
  //------------------------------------------------------------------------------
15
15
 
16
16
  const messageId = "use-to-have-class";
17
+
18
+ /**
19
+ * `classList[x]` is a class name only when `x` is an index. Keys like
20
+ * `classList["length"]` or `classList["value"]` read other properties.
21
+ */
22
+ const isClassListIndex = ({
23
+ property
24
+ }) => {
25
+ let key;
26
+ if (property.type === "Literal") {
27
+ key = property.value;
28
+ } else if (property.type === "TemplateLiteral" && property.expressions.length === 0) {
29
+ key = property.quasis[0].value.cooked;
30
+ } else {
31
+ return true;
32
+ }
33
+ return typeof key === "number" || typeof key === "string" && /^(0|[1-9]\d*)$/.test(key);
34
+ };
17
35
  const meta = exports.meta = {
18
36
  docs: {
19
37
  category: "Best Practices",
@@ -46,11 +64,14 @@ const create = context => ({
46
64
  });
47
65
  },
48
66
  //expect(el.classList[0]).toBe("bar")
49
- [`CallExpression[callee.object.callee.name=expect][callee.object.arguments.0.object.property.name=classList][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
67
+ [`CallExpression[callee.object.callee.name=expect][callee.object.arguments.0.object.property.name=classList][callee.object.arguments.0.computed=true][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
50
68
  const [classValue] = node.arguments;
51
69
  const matcher = node.callee.property;
52
70
  const classNameProp = node.callee.object.arguments[0].object;
53
71
  const expectArg = node.callee.object.arguments[0];
72
+ if (!isClassListIndex(expectArg)) {
73
+ return;
74
+ }
54
75
  context.report({
55
76
  node: matcher,
56
77
  messageId,
@@ -64,7 +85,10 @@ const create = context => ({
64
85
  });
65
86
  },
66
87
  //expect(el.classList[0]).not.toBe("bar")
67
- [`CallExpression[callee.object.object.callee.name=expect][callee.object.object.arguments.0.object.property.name=classList][callee.object.property.name=not][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
88
+ [`CallExpression[callee.object.object.callee.name=expect][callee.object.object.arguments.0.object.property.name=classList][callee.object.object.arguments.0.computed=true][callee.object.property.name=not][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
89
+ if (!isClassListIndex(node.callee.object.object.arguments[0])) {
90
+ return;
91
+ }
68
92
  //can't autofix this case because the class could be in another element of the classList array.
69
93
  context.report({
70
94
  node,
@@ -106,7 +106,7 @@ const create = context => {
106
106
  },
107
107
  //expect(element).toHaveAttribute('value', 'foo') / Property
108
108
  //expect(element).toHaveAttribute('aria-valuenow', 'foo') / Property
109
- [`CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=/^(value|aria-valuenow)$/][arguments.1][callee.object.callee.name=expect], CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=/(value|aria-valuenow)$/][arguments.1][callee.object.object.callee.name=expect][callee.object.property.name=not]`](node) {
109
+ [`CallExpression[callee.property.name=/^toHave(Attribute|Property)$/][arguments.0.value=/^(value|aria-valuenow)$/][arguments.1][callee.object.callee.name=expect], CallExpression[callee.property.name=/^toHave(Attribute|Property)$/][arguments.0.value=/^(value|aria-valuenow)$/][arguments.1][callee.object.object.callee.name=expect][callee.object.property.name=not]`](node) {
110
110
  const matcher = node.callee.property;
111
111
  const [prop, value] = node.arguments;
112
112
  context.report({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "5.10.1",
3
+ "version": "5.10.3",
4
4
  "description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom",
5
5
  "keywords": [
6
6
  "eslint",
@@ -39,6 +39,11 @@
39
39
  "test:coverage": "npm test -- --coverage",
40
40
  "test:update": "npm test:coverage -- --updateSnapshot"
41
41
  },
42
+ "commitlint": {
43
+ "extends": [
44
+ "@commitlint/config-conventional"
45
+ ]
46
+ },
42
47
  "dependencies": {
43
48
  "requireindex": "^1.2.0"
44
49
  },
@@ -48,7 +53,7 @@
48
53
  "@testing-library/dom": "^10.4.1",
49
54
  "@typescript-eslint/parser": "^8.0.0",
50
55
  "eslint": "^9.0.0",
51
- "eslint-config-prettier": "^8.10.2",
56
+ "eslint-config-prettier": "^10.1.8",
52
57
  "eslint-doc-generator": "^3.0.0",
53
58
  "eslint-plugin-import": "^2.32.0",
54
59
  "eslint-plugin-jest": "^29.16.0",