eslint-plugin-jest-dom 2.0.0 → 3.0.1

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.
Files changed (40) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +137 -50
  3. package/dist/createBannedAttributeRule.js +93 -0
  4. package/dist/index.js +39 -0
  5. package/dist/rules/prefer-checked.js +31 -0
  6. package/dist/rules/prefer-empty.js +109 -0
  7. package/dist/rules/prefer-enabled-disabled.js +31 -0
  8. package/dist/rules/prefer-focus.js +79 -0
  9. package/dist/rules/prefer-required.js +31 -0
  10. package/dist/rules/prefer-to-have-attribute.js +93 -0
  11. package/dist/rules/prefer-to-have-text-content.js +58 -0
  12. package/package.json +42 -29
  13. package/.all-contributorsrc +0 -46
  14. package/.eslintignore +0 -1
  15. package/.eslintrc.json +0 -22
  16. package/.prettierrc.json +0 -4
  17. package/.travis.yml +0 -19
  18. package/build/generate-readme-table.js +0 -57
  19. package/docs/rules/prefer-checked.md +0 -73
  20. package/docs/rules/prefer-empty.md +0 -60
  21. package/docs/rules/prefer-enabled-disabled.md +0 -76
  22. package/docs/rules/prefer-focus.md +0 -49
  23. package/docs/rules/prefer-required.md +0 -74
  24. package/docs/rules/prefer-to-have-attribute.md +0 -42
  25. package/jest.config.js +0 -14
  26. package/lib/createBannedAttributeRule.js +0 -114
  27. package/lib/index.js +0 -35
  28. package/lib/rules/prefer-checked.js +0 -25
  29. package/lib/rules/prefer-empty.js +0 -170
  30. package/lib/rules/prefer-enabled-disabled.js +0 -26
  31. package/lib/rules/prefer-focus.js +0 -127
  32. package/lib/rules/prefer-required.js +0 -25
  33. package/lib/rules/prefer-to-have-attribute.js +0 -171
  34. package/tests/__snapshots__/index.test.js.snap +0 -75
  35. package/tests/fixtures/createBannedAttributeTestCases.js +0 -159
  36. package/tests/index.test.js +0 -14
  37. package/tests/lib/rules/no-attribute-checking.js +0 -52
  38. package/tests/lib/rules/prefer-empty.js +0 -222
  39. package/tests/lib/rules/prefer-focus.js +0 -138
  40. package/tests/lib/rules/prefer-to-have-attribute.js +0 -191
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.create = exports.meta = void 0;
9
+
10
+ var _createBannedAttributeRule = _interopRequireDefault(require("../createBannedAttributeRule"));
11
+
12
+ /**
13
+ * @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks
14
+ * @author Ben Monro
15
+ */
16
+ const meta = {
17
+ docs: {
18
+ description: "prefer toBeRequired over checking properties",
19
+ category: "jest-dom",
20
+ recommended: true,
21
+ url: "prefer-required"
22
+ },
23
+ fixable: "code"
24
+ };
25
+ exports.meta = meta;
26
+ const create = (0, _createBannedAttributeRule.default)({
27
+ preferred: "toBeRequired",
28
+ negatedPreferred: "not.toBeRequired",
29
+ attributes: ["required", "aria-required"]
30
+ });
31
+ exports.create = create;
@@ -0,0 +1,93 @@
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
+ //------------------------------------------------------------------------------
13
+ // Rule Definition
14
+ //------------------------------------------------------------------------------
15
+ const meta = {
16
+ docs: {
17
+ description: "prefer toHaveAttribute over checking getAttribute/hasAttribute ",
18
+ recommended: true
19
+ },
20
+ fixable: "code"
21
+ };
22
+ exports.meta = meta;
23
+
24
+ const create = context => ({
25
+ [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeNull/]`](node) {
26
+ context.report({
27
+ node: node.parent,
28
+ message: `Use toHaveAttribute instead of asserting on getAttribute`,
29
+ fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `not.toHaveAttribute(${node.arguments[0].raw})`)]
30
+ });
31
+ },
32
+
33
+ [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](node) {
34
+ context.report({
35
+ node: node.parent,
36
+ message: `Use toHaveAttribute instead of asserting on getAttribute`,
37
+ fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceText(node.parent.parent.property, "toHaveAttribute"), fixer.replaceText(node.parent.parent.parent.arguments[0], `${node.arguments[0].raw}, expect.string${node.parent.parent.property.name.slice(2)}ing(${node.parent.parent.parent.arguments[0].raw})`)]
38
+ });
39
+ },
40
+
41
+ [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
42
+ const arg = node.parent.parent.parent.arguments;
43
+ const isNullOrEmpty = arg.length > 0 && (arg[0].value === null || arg[0].value === "");
44
+ context.report({
45
+ node: node.parent,
46
+ message: `Use toHaveAttribute instead of asserting on getAttribute`,
47
+ fix: fixer => {
48
+ const lastFixer = isNullOrEmpty ? fixer.replaceText(node.parent.parent.parent.arguments[0], node.arguments[0].raw) : fixer.insertTextBefore(node.parent.parent.parent.arguments[0], `${node.arguments[0].raw}, `);
49
+ return [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceText(node.parent.parent.property, `${isNullOrEmpty ? "not." : ""}toHaveAttribute`), lastFixer];
50
+ }
51
+ });
52
+ },
53
+
54
+ [`CallExpression[callee.property.name='hasAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeNull|toBeUndefined|toBeDefined/]`](node) {
55
+ context.report({
56
+ node: node.parent.parent.property,
57
+ message: "Invalid matcher for hasAttribute"
58
+ });
59
+ },
60
+
61
+ [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeUndefined|toBeDefined/]`](node) {
62
+ context.report({
63
+ node: node.parent.parent.property,
64
+ message: "Invalid matcher for getAttribute"
65
+ });
66
+ },
67
+
68
+ [`CallExpression[callee.property.name='hasAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Striclty)?Equal/]`](node) {
69
+ if (typeof node.parent.parent.parent.arguments[0].value === "boolean") {
70
+ context.report({
71
+ node: node.parent,
72
+ message: `Use toHaveAttribute instead of asserting on hasAttribute`,
73
+ fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceText(node.parent.parent.property, `${node.parent.parent.parent.arguments[0].value === false ? "not." : ""}toHaveAttribute`), fixer.replaceText(node.parent.parent.parent.arguments[0], node.arguments[0].raw)]
74
+ });
75
+ } else {
76
+ context.report({
77
+ node: node.parent.parent.property,
78
+ message: "Invalid matcher for hasAttribute"
79
+ });
80
+ }
81
+ },
82
+
83
+ [`CallExpression[callee.property.name='hasAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeTruthy|toBeFalsy/]`](node) {
84
+ context.report({
85
+ node: node.parent,
86
+ message: `Use toHaveAttribute instead of asserting on hasAttribute`,
87
+ fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `${node.parent.parent.property.name === "toBeFalsy" ? "not." : ""}toHaveAttribute(${node.arguments[0].raw})`)]
88
+ });
89
+ }
90
+
91
+ });
92
+
93
+ exports.create = create;
@@ -0,0 +1,58 @@
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
+ description: "Prefer toHaveTextContent over checking element.textContent",
15
+ recommended: true
16
+ },
17
+ fixable: "code"
18
+ };
19
+ exports.meta = meta;
20
+
21
+ const create = context => ({
22
+ [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](node) {
23
+ const expectedArg = node.parent.parent.parent.arguments[0];
24
+ context.report({
25
+ node: node.parent,
26
+ message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
27
+ fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, `/${expectedArg.value}/`)]
28
+ });
29
+ },
30
+
31
+ [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
32
+ context.report({
33
+ node: node.parent,
34
+ message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
35
+ fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.property.range, "toHaveTextContent")]
36
+ });
37
+ },
38
+
39
+ [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
40
+ context.report({
41
+ node: node.parent,
42
+ message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
43
+ fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.parent.property.range, "toHaveTextContent")]
44
+ });
45
+ },
46
+
47
+ [`MemberExpression[property.name='textContent'][parent.callee.name='expect'][parent.parent.property.name='not'][parent.parent.parent.property.name=/toContain$|toMatch$/]`](node) {
48
+ const expectedArg = node.parent.parent.parent.parent.arguments[0];
49
+ context.report({
50
+ node: node.parent,
51
+ message: `Use toHaveTextContent instead of asserting on DOM node attributes`,
52
+ fix: fixer => [fixer.removeRange([node.property.range[0] - 1, node.property.range[1]]), fixer.replaceTextRange(node.parent.parent.parent.property.range, "toHaveTextContent"), fixer.replaceTextRange(expectedArg.range, `/${expectedArg.value}/`)]
53
+ });
54
+ }
55
+
56
+ });
57
+
58
+ exports.create = create;
package/package.json CHANGED
@@ -1,46 +1,59 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "2.0.0",
4
- "description": "lint rules for use with jest-dom",
3
+ "version": "3.0.1",
4
+ "description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom",
5
+ "main": "dist/index.js",
5
6
  "keywords": [
6
7
  "eslint",
7
8
  "eslintplugin",
8
9
  "eslint-plugin"
9
10
  ],
10
11
  "author": "Ben Monro",
11
- "main": "lib/index.js",
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/testing-library/eslint-plugin-jest-dom"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/testing-library/eslint-plugin-jest-dom/issues"
19
+ },
20
+ "homepage": "https://github.com/testing-library/eslint-plugin-jest-dom#readme",
21
+ "files": [
22
+ "dist"
23
+ ],
12
24
  "scripts": {
13
- "lint": "eslint .",
14
- "test": "jest",
15
- "generate-readme-table": "node build/generate-readme-table.js"
25
+ "build": "kcd-scripts build",
26
+ "generate-readme-table": "node build/generate-readme-table.js",
27
+ "lint": "kcd-scripts lint",
28
+ "setup": "npm install && npm run validate -s",
29
+ "test": "kcd-scripts test",
30
+ "test:update": "npm test -- --updateSnapshot --coverage",
31
+ "validate": "kcd-scripts validate"
16
32
  },
17
33
  "dependencies": {
18
- "requireindex": "~1.2.0"
34
+ "@babel/runtime": "^7.9.6",
35
+ "requireindex": "^1.2.0"
19
36
  },
20
37
  "devDependencies": {
21
- "eslint": "^6.6.0",
22
- "eslint-plugin-eslint-plugin": "^2.1.0",
23
- "eslint-plugin-prettier": "^3.1.1",
24
- "eslint-plugin-standard": "^4.0.1",
25
- "jest": "^24.9.0",
26
- "kcd-scripts": "^1.9.0",
27
- "prettier": "^1.18.2"
28
- },
29
- "engines": {
30
- "node": ">=0.10.0"
31
- },
32
- "license": "ISC",
33
- "directories": {
34
- "doc": "docs",
35
- "lib": "lib",
36
- "test": "tests"
38
+ "kcd-scripts": "^6.0.0"
37
39
  },
38
- "repository": {
39
- "type": "git",
40
- "url": "git+https://github.com/testing-library/eslint-plugin-jest-dom.git"
40
+ "peerDependencies": {
41
+ "eslint": "^7.0.0"
41
42
  },
42
- "bugs": {
43
- "url": "https://github.com/testing-library/eslint-plugin-jest-dom/issues"
43
+ "eslintConfig": {
44
+ "extends": "./node_modules/kcd-scripts/eslint.js",
45
+ "rules": {
46
+ "babel/quotes": "off"
47
+ }
44
48
  },
45
- "homepage": "https://github.com/testing-library/eslint-plugin-jest-dom#readme"
49
+ "eslintIgnore": [
50
+ "node_modules",
51
+ "coverage",
52
+ "dist"
53
+ ],
54
+ "engines": {
55
+ "node": "^10.12.0 || >=12.0.0",
56
+ "npm": ">=6",
57
+ "yarn": ">=1"
58
+ }
46
59
  }
@@ -1,46 +0,0 @@
1
- {
2
- "files": [
3
- "README.md"
4
- ],
5
- "imageSize": 100,
6
- "commit": false,
7
- "contributors": [
8
- {
9
- "login": "benmonro",
10
- "name": "Ben Monro",
11
- "avatar_url": "https://avatars3.githubusercontent.com/u/399236?v=4",
12
- "profile": "https://github.com/benmonro",
13
- "contributions": [
14
- "doc",
15
- "code",
16
- "example",
17
- "test"
18
- ]
19
- },
20
- {
21
- "login": "nickmccurdy",
22
- "name": "Nick McCurdy",
23
- "avatar_url": "https://avatars0.githubusercontent.com/u/927220?v=4",
24
- "profile": "https://nickmccurdy.com/",
25
- "contributions": [
26
- "code",
27
- "doc",
28
- "test"
29
- ]
30
- },
31
- {
32
- "login": "gnapse",
33
- "name": "Ernesto García",
34
- "avatar_url": "https://avatars0.githubusercontent.com/u/15199?v=4",
35
- "profile": "https://twitter.com/gnapse",
36
- "contributions": [
37
- "doc"
38
- ]
39
- }
40
- ],
41
- "contributorsPerLine": 7,
42
- "projectName": "eslint-plugin-jest-dom",
43
- "projectOwner": "testing-library",
44
- "repoType": "github",
45
- "repoHost": "https://github.com"
46
- }
package/.eslintignore DELETED
@@ -1 +0,0 @@
1
- coverage/
package/.eslintrc.json DELETED
@@ -1,22 +0,0 @@
1
- {
2
- "parserOptions": {
3
- "ecmaVersion": 2018
4
- },
5
- "env": {
6
- "commonjs": true,
7
- "es6": true,
8
- "node": true
9
- // "jest/globals": true
10
- },
11
- "plugins": [
12
- "eslint-plugin",
13
- "prettier"
14
- ],
15
- "extends": [
16
- "plugin:eslint-plugin/recommended"
17
- ],
18
- "rules": {
19
- "prettier/prettier": "error",
20
- "no-var": "error"
21
- }
22
- }
package/.prettierrc.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "trailingComma": "es5",
3
- "singleQuote": true
4
- }
package/.travis.yml DELETED
@@ -1,19 +0,0 @@
1
- sudo: false
2
- language: node_js
3
- env:
4
- - FORCE_COLOR=true
5
- cache:
6
- directories:
7
- - ~/.npm
8
- notifications:
9
- email: false
10
- node_js: "10"
11
- install: npm install
12
- script:
13
- - npm run test
14
- - npm run lint
15
- after_success: npx semantic-release
16
- branches:
17
- only:
18
- - master
19
- - next
@@ -1,57 +0,0 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
- const rules = require('..').rules;
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__ -->';
10
-
11
- const expectedTableLines = Object.keys(rules)
12
- .sort()
13
- .reduce(
14
- (lines, ruleId) => {
15
- const rule = rules[ruleId];
16
-
17
- lines.push(
18
- [
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(' | ')
24
- );
25
-
26
- return lines;
27
- },
28
- ['Name | ✔️ | 🛠 | Description', '----- | ----- | ----- | -----']
29
- )
30
- .join('\n');
31
-
32
- const readmeContents = fs.readFileSync(README_LOCATION, 'utf8');
33
-
34
- if (!readmeContents.includes(BEGIN_TABLE_MARKER)) {
35
- throw new Error(
36
- `Could not find '${BEGIN_TABLE_MARKER}' marker in README.md.`
37
- );
38
- }
39
-
40
- if (!readmeContents.includes(END_TABLE_MARKER)) {
41
- throw new Error(`Could not find '${END_TABLE_MARKER}' marker in README.md.`);
42
- }
43
-
44
- const linesStartIndex =
45
- readmeContents.indexOf(BEGIN_TABLE_MARKER) + BEGIN_TABLE_MARKER.length;
46
- const linesEndIndex = readmeContents.indexOf(END_TABLE_MARKER);
47
-
48
- const updatedReadmeContents =
49
- readmeContents.slice(0, linesStartIndex) +
50
- expectedTableLines +
51
- readmeContents.slice(linesEndIndex);
52
-
53
- if (module.parent) {
54
- module.exports = updatedReadmeContents;
55
- } else {
56
- fs.writeFileSync(README_LOCATION, updatedReadmeContents);
57
- }
@@ -1,73 +0,0 @@
1
- # prefer not.toBeChecked() or not.toBeChecked() over toHaveProperty('checked', true|false) (prefer-enabled-checked)
2
-
3
- ## Rule Details
4
-
5
- This rule aims to prevent false positives and improve readability and should only be used with the `@testing-library/jest-dom` package. See below for examples of those potential issues and why this rule is recommended. The rule is autofixable and will replace any instances of `.toHaveProperty()` or `.toHaveAttribute()` with `.toBeChecked()` or `not.toBeChecked()` as appropriate.
6
-
7
- ### False positives
8
-
9
- Consider these 2 snippets:
10
-
11
- ```js
12
- const { getByRole } = render(<input type="checkbox" che />);
13
- const element = getByRole('checkbox');
14
- expect(element).toHaveProperty('checked'); // passes
15
-
16
- const { getByRole } = render(<input type="checkbox" />);
17
- const element = getByRole('checkbox');
18
- expect(element).toHaveProperty('checked'); // also passes 😱
19
- ```
20
-
21
- ### Readability
22
-
23
- Consider the following snippets:
24
-
25
- ```js
26
- const { getByRole } = render(<input type="checkbox" />);
27
- const element = getByRole('checkbox');
28
-
29
- expect(element).toHaveAttribute('checked', false); // fails
30
- expect(element).toHaveAttribute('checked', ''); // fails
31
- expect(element).not.toHaveAttribute('checked', ''); // passes
32
-
33
- expect(element).not.toHaveAttribute('checked', true); // passes.
34
- expect(element).not.toHaveAttribute('checked', false); // also passes.
35
- ```
36
-
37
- As you can see, using `toHaveAttribute` in this case is confusing, unintuitive and can even lead to false positive tests.
38
-
39
- Examples of **incorrect** code for this rule:
40
-
41
- ```js
42
- expect(element).toHaveProperty('checked', true);
43
- expect(element).toHaveAttribute('checked', false);
44
-
45
- expect(element).toHaveAttribute('checked');
46
- expect(element).not.toHaveProperty('checked');
47
-
48
- expect(element).not.not.toBeChecked();
49
- ```
50
-
51
- Examples of **correct** code for this rule:
52
-
53
- ```js
54
- expect(element).not.toBeChecked();
55
-
56
- expect(element).toBeChecked();
57
-
58
- expect(element).toHaveProperty('value', 'foo');
59
-
60
- expect(element).toHaveAttribute('aria-label');
61
- ```
62
-
63
- ## When Not To Use It
64
-
65
- Don't use this rule if you:
66
-
67
- - don't use `jest-dom`
68
- - want to allow `.toHaveProperty('checked', true|false);`
69
-
70
- ## Further reading
71
-
72
- - [not.toBeChecked](https://github.com/testing-library/jest-dom#not.toBeChecked)
73
- - [not.toBeChecked](https://github.com/testing-library/jest-dom#not.toBeChecked)
@@ -1,60 +0,0 @@
1
- # Prefer toBeEmpty over checking innerHTML / firstChild (prefer-empty)
2
-
3
- This rule ensures people will use toBeEmpty() rather than checking dom nodes/properties. It is primarily aimed at consistently using jest-dom for readability.
4
-
5
- ## Rule Details
6
-
7
- This autofixable rule aims to ensure usage of `.toBeEmpty()`
8
-
9
- Examples of **incorrect** code for this rule:
10
-
11
- ```js
12
-
13
- expect(element.innerHTML).toBe('foo');
14
- expect(element.innerHTML).not.toBe('foo');
15
- expect(element.firstChild).toBe('foo');
16
- expect(element.firstChild).not.toBe('foo');
17
- expect(getByText("foo").innerHTML).toBe('foo');
18
- expect(getByText("foo").innerHTML).not.toBe('foo');
19
- expect(getByText("foo").firstChild).toBe('foo');
20
- expect(getByText("foo").firstChild).not.toBe('foo');
21
- expect(element.innerHTML === '').toBe(true);
22
- expect(element.innerHTML !== '').toBe(true);
23
- expect(element.innerHTML === '').toBe(false);
24
- expect(element.innerHTML !== '').toBe(false);
25
- expect(element.firstChild === null).toBe(true);
26
- expect(element.firstChild !== null).toBe(false);
27
- expect(element.firstChild === null).toBe(false);
28
-
29
- ```
30
-
31
- Examples of **correct** code for this rule:
32
-
33
- ```js
34
- expect(element.innerHTML).toBe('');
35
- expect(element.innerHTML).toBe(null);
36
- expect(element.innerHTML).not.toBe(null);
37
- expect(element.innerHTML).not.toBe('');
38
- expect(element.firstChild).toBeNull();
39
- expect(element.firstChild).toBe(null);
40
- expect(element.firstChild).not.toBe(null);
41
- expect(element.firstChild).not.toBeNull();
42
- expect(getByText('foo').innerHTML).toBe('');
43
- expect(getByText('foo').innerHTML).toStrictEqual('');
44
- expect(getByText('foo').innerHTML).toStrictEqual(null);
45
- expect(getByText('foo').firstChild).toBe(null);
46
- expect(getByText('foo').firstChild).not.toBe(null);
47
- expect(element.innerHTML === 'foo').toBe(true);
48
-
49
-
50
- ```
51
-
52
- ## When Not To Use It
53
-
54
- Don't use this rule if you don't care if people use `.toBeEmpty()`.
55
-
56
- ## Further Reading
57
-
58
- <https://github.com/testing-library/jest-dom#tobeempty>
59
-
60
- <https://github.com/testing-library/jest-dom/blob/master/src/to-be-empty.js>
@@ -1,76 +0,0 @@
1
- # prefer toBeDisabled() or toBeEnabled() over toHaveProperty('disabled', true|false) (prefer-enabled-disabled)
2
-
3
- ## Rule Details
4
-
5
- This rule aims to prevent false positives and improve readability and should only be used with the `@testing-library/jest-dom` package. See below for examples of those potential issues and why this rule is recommended. The rule is autofixable and will replace any instances of `.toHaveProperty()` or `.toHaveAttribute()` with `.toBeEnabled()` or `toBeDisabled()` as appropriate.
6
-
7
- In addition, to avoid double negatives and confusing syntax, `expect(element).not.toBeDisabled()` is also reported and auto-fixed to `expect(element).toBeEnabled()` and vice versa.
8
-
9
- ### False positives
10
-
11
- Consider these 2 snippets:
12
-
13
- ```js
14
- const { getByRole } = render(<input type="checkbox" disabled />);
15
- const element = getByRole('checkbox');
16
- expect(element).toHaveProperty('disabled'); // passes
17
-
18
- const { getByRole } = render(<input type="checkbox" />);
19
- const element = getByRole('checkbox');
20
- expect(element).toHaveProperty('disabled'); // also passes 😱
21
- ```
22
-
23
- ### Readability
24
-
25
- Consider the following snippets:
26
-
27
- ```js
28
- const { getByRole } = render(<input type="checkbox" />);
29
- const element = getByRole('checkbox');
30
-
31
- expect(element).toHaveAttribute('disabled', false); // fails
32
- expect(element).toHaveAttribute('disabled', ''); // fails
33
- expect(element).not.toHaveAttribute('disabled', ''); // passes
34
-
35
- expect(element).not.toHaveAttribute('disabled', true); // passes.
36
- expect(element).not.toHaveAttribute('disabled', false); // also passes.
37
- ```
38
-
39
- As you can see, using `toHaveAttribute` in this case is confusing, unintuitive and can even lead to false positive tests.
40
-
41
- Examples of **incorrect** code for this rule:
42
-
43
- ```js
44
- expect(element).toHaveProperty('disabled', true);
45
- expect(element).toHaveAttribute('disabled', false);
46
-
47
- expect(element).toHaveAttribute('disabled');
48
- expect(element).not.toHaveProperty('disabled');
49
-
50
- expect(element).not.toBeDisabled();
51
- expect(element).not.toBeEnabled();
52
- ```
53
-
54
- Examples of **correct** code for this rule:
55
-
56
- ```js
57
- expect(element).toBeEnabled();
58
-
59
- expect(element).toBeDisabled();
60
-
61
- expect(element).toHaveProperty('checked', true);
62
-
63
- expect(element).toHaveAttribute('checked');
64
- ```
65
-
66
- ## When Not To Use It
67
-
68
- Don't use this rule if you:
69
-
70
- - don't use `jest-dom`
71
- - want to allow `.toHaveProperty('disabled', true|false);`
72
-
73
- ## Further reading
74
-
75
- - [toBeDisabled](https://github.com/testing-library/jest-dom#tobedisabled)
76
- - [toBeEnabled](https://github.com/testing-library/jest-dom#tobeenabled)