eslint-plugin-jest 24.4.2 → 26.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.
Files changed (77) hide show
  1. package/README.md +75 -50
  2. package/docs/rules/expect-expect.md +42 -1
  3. package/docs/rules/max-nested-describe.md +4 -5
  4. package/docs/rules/no-conditional-expect.md +57 -3
  5. package/docs/rules/no-conditional-in-test.md +79 -0
  6. package/docs/rules/no-deprecated-functions.md +5 -0
  7. package/docs/rules/no-done-callback.md +3 -3
  8. package/docs/rules/no-if.md +5 -0
  9. package/docs/rules/no-standalone-expect.md +3 -3
  10. package/docs/rules/no-test-return-statement.md +1 -2
  11. package/docs/rules/prefer-comparison-matcher.md +55 -0
  12. package/docs/rules/prefer-equality-matcher.md +29 -0
  13. package/docs/rules/prefer-expect-assertions.md +126 -0
  14. package/docs/rules/prefer-expect-resolves.md +53 -0
  15. package/docs/rules/prefer-hooks-on-top.md +72 -48
  16. package/docs/rules/{lowercase-name.md → prefer-lowercase-title.md} +7 -7
  17. package/docs/rules/prefer-snapshot-hint.md +188 -0
  18. package/docs/rules/prefer-to-be.md +53 -0
  19. package/docs/rules/require-hook.md +187 -0
  20. package/docs/rules/require-top-level-describe.md +28 -0
  21. package/docs/rules/{valid-describe.md → valid-describe-callback.md} +1 -1
  22. package/docs/rules/valid-expect-in-promise.md +55 -14
  23. package/docs/rules/valid-expect.md +13 -0
  24. package/docs/rules/valid-title.md +30 -2
  25. package/lib/index.js +2 -3
  26. package/lib/processors/snapshot-processor.js +1 -1
  27. package/lib/rules/consistent-test-it.js +20 -20
  28. package/lib/rules/detectJestVersion.js +29 -0
  29. package/lib/rules/expect-expect.js +25 -11
  30. package/lib/rules/max-nested-describe.js +5 -5
  31. package/lib/rules/no-conditional-expect.js +9 -9
  32. package/lib/rules/no-conditional-in-test.js +60 -0
  33. package/lib/rules/no-deprecated-functions.js +14 -32
  34. package/lib/rules/no-done-callback.js +10 -10
  35. package/lib/rules/no-export.js +6 -6
  36. package/lib/rules/no-focused-tests.js +11 -11
  37. package/lib/rules/no-identical-title.js +3 -3
  38. package/lib/rules/no-if.js +13 -11
  39. package/lib/rules/no-interpolation-in-snapshots.js +6 -6
  40. package/lib/rules/no-jasmine-globals.js +10 -10
  41. package/lib/rules/no-large-snapshots.js +11 -11
  42. package/lib/rules/no-standalone-expect.js +14 -14
  43. package/lib/rules/no-test-prefixes.js +6 -6
  44. package/lib/rules/no-test-return-statement.js +8 -8
  45. package/lib/rules/prefer-comparison-matcher.js +139 -0
  46. package/lib/rules/prefer-equality-matcher.js +98 -0
  47. package/lib/rules/prefer-expect-assertions.js +93 -11
  48. package/lib/rules/prefer-expect-resolves.js +48 -0
  49. package/lib/rules/prefer-hooks-on-top.js +1 -1
  50. package/lib/rules/{lowercase-name.js → prefer-lowercase-title.js} +20 -1
  51. package/lib/rules/prefer-snapshot-hint.js +112 -0
  52. package/lib/rules/prefer-spy-on.js +9 -9
  53. package/lib/rules/prefer-to-be.js +136 -0
  54. package/lib/rules/prefer-to-contain.js +19 -67
  55. package/lib/rules/prefer-to-have-length.js +9 -14
  56. package/lib/rules/prefer-todo.js +9 -9
  57. package/lib/rules/require-hook.js +121 -0
  58. package/lib/rules/require-top-level-describe.js +40 -6
  59. package/lib/rules/utils.js +34 -30
  60. package/lib/rules/{valid-describe.js → valid-describe-callback.js} +9 -9
  61. package/lib/rules/valid-expect-in-promise.js +336 -67
  62. package/lib/rules/valid-expect.js +36 -19
  63. package/lib/rules/valid-title.js +61 -61
  64. package/package.json +40 -27
  65. package/CHANGELOG.md +0 -513
  66. package/docs/rules/no-expect-resolves.md +0 -47
  67. package/docs/rules/no-truthy-falsy.md +0 -53
  68. package/docs/rules/no-try-expect.md +0 -63
  69. package/docs/rules/prefer-inline-snapshots.md +0 -51
  70. package/docs/rules/prefer-to-be-null.md +0 -33
  71. package/docs/rules/prefer-to-be-undefined.md +0 -33
  72. package/lib/rules/no-expect-resolves.js +0 -40
  73. package/lib/rules/no-truthy-falsy.js +0 -58
  74. package/lib/rules/no-try-expect.js +0 -89
  75. package/lib/rules/prefer-inline-snapshots.js +0 -69
  76. package/lib/rules/prefer-to-be-null.js +0 -67
  77. package/lib/rules/prefer-to-be-undefined.js +0 -67
@@ -1,63 +0,0 @@
1
- # Prevent catch assertions in tests (`no-try-expect`)
2
-
3
- ## Deprecated
4
-
5
- This rule has been deprecated in favor of
6
- [`no-conditional-expect`](no-conditional-expect.md).
7
-
8
- ---
9
-
10
- This rule prevents the use of `expect` inside `catch` blocks.
11
-
12
- ## Rule Details
13
-
14
- Expectations inside a `catch` block can be silently skipped. While Jest provides
15
- an `expect.assertions(number)` helper, it might be cumbersome to add this to
16
- every single test. Using `toThrow` concisely guarantees that an exception was
17
- thrown, and that its contents match expectations.
18
-
19
- The following patterns are warnings:
20
-
21
- ```js
22
- it('foo', () => {
23
- try {
24
- foo(); // `foo` may be refactored to not throw exceptions, yet still appears to be tested here.
25
- } catch (err) {
26
- expect(err).toMatch(/foo error/);
27
- }
28
- });
29
-
30
- it('bar', async () => {
31
- try {
32
- await foo();
33
- } catch (err) {
34
- expect(err).toMatch(/foo error/);
35
- }
36
- });
37
-
38
- it('baz', async () => {
39
- try {
40
- await foo();
41
- } catch (err) {
42
- expect(err).toMatchObject({ code: 'MODULE_NOT_FOUND' });
43
- }
44
- });
45
- ```
46
-
47
- The following patterns are not warnings:
48
-
49
- ```js
50
- it('foo', () => {
51
- expect(() => foo()).toThrow(/foo error/);
52
- });
53
-
54
- it('bar', async () => {
55
- await expect(fooPromise).rejects.toThrow(/foo error/);
56
- });
57
-
58
- it('baz', async () => {
59
- await expect(() => foo()).rejects.toThrow(
60
- expect.objectContaining({ code: 'MODULE_NOT_FOUND' }),
61
- );
62
- });
63
- ```
@@ -1,51 +0,0 @@
1
- # Suggest using inline snapshots (`prefer-inline-snapshots`)
2
-
3
- ## Deprecated
4
-
5
- This rule has been deprecated in favor of
6
- [`no-restricted-matchers`](no-restricted-matchers.md) with the following config:
7
-
8
- ```json
9
- {
10
- "rules": {
11
- "jest/no-restricted-matchers": [
12
- "error",
13
- {
14
- "toThrowErrorMatchingSnapshot": "Use `toThrowErrorMatchingInlineSnapshot()` instead",
15
- "toMatchSnapshot": "Use `toMatchInlineSnapshot()` instead"
16
- }
17
- ]
18
- }
19
- }
20
- ```
21
-
22
- ---
23
-
24
- In order to make snapshot tests more manageable and reviewable
25
- `toMatchInlineSnapshot()` and `toThrowErrorMatchingInlineSnapshot` should be
26
- used to write the snapshots' inline in the test file.
27
-
28
- ## Rule details
29
-
30
- This rule triggers a warning if `toMatchSnapshot()` or
31
- `toThrowErrorMatchingSnapshot` is used to capture a snapshot.
32
-
33
- The following pattern is considered warning:
34
-
35
- ```js
36
- expect(obj).toMatchSnapshot();
37
- ```
38
-
39
- ```js
40
- expect(error).toThrowErrorMatchingSnapshot();
41
- ```
42
-
43
- The following pattern is not warning:
44
-
45
- ```js
46
- expect(obj).toMatchInlineSnapshot();
47
- ```
48
-
49
- ```js
50
- expect(error).toThrowErrorMatchingInlineSnapshot();
51
- ```
@@ -1,33 +0,0 @@
1
- # Suggest using `toBeNull()` (`prefer-to-be-null`)
2
-
3
- In order to have a better failure message, `toBeNull()` should be used upon
4
- asserting expectations on null value.
5
-
6
- ## Rule details
7
-
8
- This rule triggers a warning if `toBe()`, `toEqual()` or `toStrictEqual()` is
9
- used to assert a null value.
10
-
11
- ```js
12
- expect(null).toBe(null);
13
- ```
14
-
15
- This rule is enabled by default.
16
-
17
- ### Default configuration
18
-
19
- The following patterns are considered warnings:
20
-
21
- ```js
22
- expect(null).toBe(null);
23
-
24
- expect(null).toEqual(null);
25
-
26
- expect(null).toStrictEqual(null);
27
- ```
28
-
29
- The following pattern is not warning:
30
-
31
- ```js
32
- expect(null).toBeNull();
33
- ```
@@ -1,33 +0,0 @@
1
- # Suggest using `toBeUndefined()` (`prefer-to-be-undefined`)
2
-
3
- In order to have a better failure message, `toBeUndefined()` should be used upon
4
- asserting expectations on undefined value.
5
-
6
- ## Rule details
7
-
8
- This rule triggers a warning if `toBe()`, `toEqual()` or `toStrictEqual()` is
9
- used to assert an undefined value.
10
-
11
- ```js
12
- expect(undefined).toBe(undefined);
13
- ```
14
-
15
- This rule is enabled by default.
16
-
17
- ### Default configuration
18
-
19
- The following patterns are considered warnings:
20
-
21
- ```js
22
- expect(undefined).toBe(undefined);
23
-
24
- expect(undefined).toEqual(undefined);
25
-
26
- expect(undefined).toStrictEqual(undefined);
27
- ```
28
-
29
- The following pattern is not warning:
30
-
31
- ```js
32
- expect(undefined).toBeUndefined();
33
- ```
@@ -1,40 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _utils = require("./utils");
9
-
10
- var _default = (0, _utils.createRule)({
11
- name: __filename,
12
- meta: {
13
- docs: {
14
- category: 'Best Practices',
15
- description: 'Disallow expect.resolves',
16
- recommended: false
17
- },
18
- deprecated: true,
19
- replacedBy: ['no-restricted-matchers'],
20
- messages: {
21
- expectResolves: 'Use `expect(await promise)` instead.'
22
- },
23
- schema: [],
24
- type: 'suggestion'
25
- },
26
- defaultOptions: [],
27
- create: context => ({
28
- MemberExpression(node) {
29
- if ((0, _utils.isExpectCall)(node.object) && (0, _utils.isSupportedAccessor)(node.property, _utils.ModifierName.resolves)) {
30
- context.report({
31
- node: node.property,
32
- messageId: 'expectResolves'
33
- });
34
- }
35
- }
36
-
37
- })
38
- });
39
-
40
- exports.default = _default;
@@ -1,58 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _utils = require("./utils");
9
-
10
- // todo: refactor into "ban-matchers"
11
- var _default = (0, _utils.createRule)({
12
- name: __filename,
13
- meta: {
14
- docs: {
15
- category: 'Best Practices',
16
- description: 'Disallow using `toBeTruthy()` & `toBeFalsy()`',
17
- recommended: false
18
- },
19
- deprecated: true,
20
- replacedBy: ['no-restricted-matchers'],
21
- messages: {
22
- avoidMatcher: 'Avoid {{ matcherName }}'
23
- },
24
- type: 'suggestion',
25
- schema: []
26
- },
27
- defaultOptions: [],
28
-
29
- create(context) {
30
- return {
31
- CallExpression(node) {
32
- if (!(0, _utils.isExpectCall)(node)) {
33
- return;
34
- }
35
-
36
- const {
37
- matcher
38
- } = (0, _utils.parseExpectCall)(node);
39
-
40
- if (!matcher || !['toBeTruthy', 'toBeFalsy'].includes(matcher.name)) {
41
- return;
42
- }
43
-
44
- context.report({
45
- messageId: 'avoidMatcher',
46
- node: matcher.node.property,
47
- data: {
48
- matcherName: matcher.name
49
- }
50
- });
51
- }
52
-
53
- };
54
- }
55
-
56
- });
57
-
58
- exports.default = _default;
@@ -1,89 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _utils = require("./utils");
9
-
10
- var _default = (0, _utils.createRule)({
11
- name: __filename,
12
- meta: {
13
- docs: {
14
- description: 'Prefer using toThrow for exception tests',
15
- category: 'Best Practices',
16
- recommended: 'error'
17
- },
18
- deprecated: true,
19
- replacedBy: ['no-conditional-expect'],
20
- messages: {
21
- noTryExpect: ['Tests should use Jest‘s exception helpers.', 'Use "expect(() => yourFunction()).toThrow()" for synchronous tests,', 'or "await expect(yourFunction()).rejects.toThrow()" for async tests'].join(' ')
22
- },
23
- type: 'problem',
24
- schema: []
25
- },
26
- defaultOptions: [],
27
-
28
- create(context) {
29
- let isTest = false;
30
- let catchDepth = 0;
31
-
32
- function isThrowExpectCall(node) {
33
- return catchDepth > 0 && (0, _utils.isExpectCall)(node);
34
- }
35
-
36
- return {
37
- CallExpression(node) {
38
- if ((0, _utils.isTestCaseCall)(node)) {
39
- isTest = true;
40
- } else if (isTest && isThrowExpectCall(node)) {
41
- context.report({
42
- messageId: 'noTryExpect',
43
- node
44
- });
45
- }
46
- },
47
-
48
- FunctionDeclaration(node) {
49
- const declaredVariables = context.getDeclaredVariables(node);
50
- const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
51
-
52
- if (testCallExpressions.length > 0) {
53
- isTest = true;
54
- }
55
- },
56
-
57
- CatchClause() {
58
- if (isTest) {
59
- ++catchDepth;
60
- }
61
- },
62
-
63
- 'CatchClause:exit'() {
64
- if (isTest) {
65
- --catchDepth;
66
- }
67
- },
68
-
69
- 'CallExpression:exit'(node) {
70
- if ((0, _utils.isTestCaseCall)(node)) {
71
- isTest = false;
72
- }
73
- },
74
-
75
- 'FunctionDeclaration:exit'(node) {
76
- const declaredVariables = context.getDeclaredVariables(node);
77
- const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
78
-
79
- if (testCallExpressions.length > 0) {
80
- isTest = false;
81
- }
82
- }
83
-
84
- };
85
- }
86
-
87
- });
88
-
89
- exports.default = _default;
@@ -1,69 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _experimentalUtils = require("@typescript-eslint/experimental-utils");
9
-
10
- var _utils = require("./utils");
11
-
12
- var _default = (0, _utils.createRule)({
13
- name: __filename,
14
- meta: {
15
- docs: {
16
- category: 'Best Practices',
17
- description: 'Suggest using inline snapshots',
18
- recommended: false
19
- },
20
- deprecated: true,
21
- replacedBy: ['no-restricted-matchers'],
22
- messages: {
23
- toMatch: 'Use toMatchInlineSnapshot() instead',
24
- toMatchError: 'Use toThrowErrorMatchingInlineSnapshot() instead'
25
- },
26
- fixable: 'code',
27
- schema: [],
28
- type: 'suggestion'
29
- },
30
- defaultOptions: [],
31
-
32
- create(context) {
33
- return {
34
- CallExpression(node) {
35
- const {
36
- callee
37
- } = node;
38
-
39
- if (callee.type !== _experimentalUtils.AST_NODE_TYPES.MemberExpression || callee.property.type !== _experimentalUtils.AST_NODE_TYPES.Identifier) {
40
- return;
41
- }
42
-
43
- if (callee.property.name === 'toMatchSnapshot') {
44
- context.report({
45
- fix(fixer) {
46
- return [fixer.replaceText(callee.property, 'toMatchInlineSnapshot')];
47
- },
48
-
49
- messageId: 'toMatch',
50
- node: callee.property
51
- });
52
- } else if (callee.property.name === 'toThrowErrorMatchingSnapshot') {
53
- context.report({
54
- fix(fixer) {
55
- return [fixer.replaceText(callee.property, 'toThrowErrorMatchingInlineSnapshot')];
56
- },
57
-
58
- messageId: 'toMatchError',
59
- node: callee.property
60
- });
61
- }
62
- }
63
-
64
- };
65
- }
66
-
67
- });
68
-
69
- exports.default = _default;
@@ -1,67 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _experimentalUtils = require("@typescript-eslint/experimental-utils");
9
-
10
- var _utils = require("./utils");
11
-
12
- const isNullLiteral = node => node.type === _experimentalUtils.AST_NODE_TYPES.Literal && node.value === null;
13
- /**
14
- * Checks if the given `ParsedExpectMatcher` is a call to one of the equality matchers,
15
- * with a `null` literal as the sole argument.
16
- *
17
- * @param {ParsedExpectMatcher} matcher
18
- *
19
- * @return {matcher is ParsedEqualityMatcherCall<MaybeTypeCast<NullLiteral>>}
20
- */
21
-
22
-
23
- const isNullEqualityMatcher = matcher => (0, _utils.isParsedEqualityMatcherCall)(matcher) && isNullLiteral((0, _utils.followTypeAssertionChain)(matcher.arguments[0]));
24
-
25
- var _default = (0, _utils.createRule)({
26
- name: __filename,
27
- meta: {
28
- docs: {
29
- category: 'Best Practices',
30
- description: 'Suggest using `toBeNull()`',
31
- recommended: false
32
- },
33
- messages: {
34
- useToBeNull: 'Use toBeNull() instead'
35
- },
36
- fixable: 'code',
37
- type: 'suggestion',
38
- schema: []
39
- },
40
- defaultOptions: [],
41
-
42
- create(context) {
43
- return {
44
- CallExpression(node) {
45
- if (!(0, _utils.isExpectCall)(node)) {
46
- return;
47
- }
48
-
49
- const {
50
- matcher
51
- } = (0, _utils.parseExpectCall)(node);
52
-
53
- if (matcher && isNullEqualityMatcher(matcher)) {
54
- context.report({
55
- fix: fixer => [fixer.replaceText(matcher.node.property, 'toBeNull'), fixer.remove(matcher.arguments[0])],
56
- messageId: 'useToBeNull',
57
- node: matcher.node.property
58
- });
59
- }
60
- }
61
-
62
- };
63
- }
64
-
65
- });
66
-
67
- exports.default = _default;
@@ -1,67 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _experimentalUtils = require("@typescript-eslint/experimental-utils");
9
-
10
- var _utils = require("./utils");
11
-
12
- const isUndefinedIdentifier = node => node.type === _experimentalUtils.AST_NODE_TYPES.Identifier && node.name === 'undefined';
13
- /**
14
- * Checks if the given `ParsedExpectMatcher` is a call to one of the equality matchers,
15
- * with a `undefined` identifier as the sole argument.
16
- *
17
- * @param {ParsedExpectMatcher} matcher
18
- *
19
- * @return {matcher is ParsedEqualityMatcherCall<MaybeTypeCast<UndefinedIdentifier>>}
20
- */
21
-
22
-
23
- const isUndefinedEqualityMatcher = matcher => (0, _utils.isParsedEqualityMatcherCall)(matcher) && isUndefinedIdentifier((0, _utils.followTypeAssertionChain)(matcher.arguments[0]));
24
-
25
- var _default = (0, _utils.createRule)({
26
- name: __filename,
27
- meta: {
28
- docs: {
29
- category: 'Best Practices',
30
- description: 'Suggest using `toBeUndefined()`',
31
- recommended: false
32
- },
33
- messages: {
34
- useToBeUndefined: 'Use toBeUndefined() instead'
35
- },
36
- fixable: 'code',
37
- type: 'suggestion',
38
- schema: []
39
- },
40
- defaultOptions: [],
41
-
42
- create(context) {
43
- return {
44
- CallExpression(node) {
45
- if (!(0, _utils.isExpectCall)(node)) {
46
- return;
47
- }
48
-
49
- const {
50
- matcher
51
- } = (0, _utils.parseExpectCall)(node);
52
-
53
- if (matcher && isUndefinedEqualityMatcher(matcher)) {
54
- context.report({
55
- fix: fixer => [fixer.replaceText(matcher.node.property, 'toBeUndefined'), fixer.remove(matcher.arguments[0])],
56
- messageId: 'useToBeUndefined',
57
- node: matcher.node.property
58
- });
59
- }
60
- }
61
-
62
- };
63
- }
64
-
65
- });
66
-
67
- exports.default = _default;