@depup/eslint-plugin-jest 29.15.1-depup.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 (158) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +31 -0
  3. package/changes.json +10 -0
  4. package/docs/rules/consistent-test-it.md +96 -0
  5. package/docs/rules/expect-expect.md +152 -0
  6. package/docs/rules/max-expects.md +78 -0
  7. package/docs/rules/max-nested-describe.md +134 -0
  8. package/docs/rules/no-alias-methods.md +57 -0
  9. package/docs/rules/no-commented-out-tests.md +68 -0
  10. package/docs/rules/no-conditional-expect.md +142 -0
  11. package/docs/rules/no-conditional-in-test.md +123 -0
  12. package/docs/rules/no-confusing-set-timeout.md +64 -0
  13. package/docs/rules/no-deprecated-functions.md +59 -0
  14. package/docs/rules/no-disabled-tests.md +72 -0
  15. package/docs/rules/no-done-callback.md +100 -0
  16. package/docs/rules/no-duplicate-hooks.md +79 -0
  17. package/docs/rules/no-error-equal.md +37 -0
  18. package/docs/rules/no-export.md +52 -0
  19. package/docs/rules/no-focused-tests.md +67 -0
  20. package/docs/rules/no-hooks.md +178 -0
  21. package/docs/rules/no-identical-title.md +59 -0
  22. package/docs/rules/no-interpolation-in-snapshots.md +67 -0
  23. package/docs/rules/no-jasmine-globals.md +69 -0
  24. package/docs/rules/no-large-snapshots.md +180 -0
  25. package/docs/rules/no-mocks-import.md +34 -0
  26. package/docs/rules/no-restricted-jest-methods.md +53 -0
  27. package/docs/rules/no-restricted-matchers.md +64 -0
  28. package/docs/rules/no-standalone-expect.md +106 -0
  29. package/docs/rules/no-test-prefixes.md +42 -0
  30. package/docs/rules/no-test-return-statement.md +50 -0
  31. package/docs/rules/no-unnecessary-assertion.md +41 -0
  32. package/docs/rules/no-unneeded-async-expect-function.md +41 -0
  33. package/docs/rules/no-untyped-mock-factory.md +74 -0
  34. package/docs/rules/padding-around-after-all-blocks.md +34 -0
  35. package/docs/rules/padding-around-after-each-blocks.md +38 -0
  36. package/docs/rules/padding-around-all.md +20 -0
  37. package/docs/rules/padding-around-before-all-blocks.md +37 -0
  38. package/docs/rules/padding-around-before-each-blocks.md +38 -0
  39. package/docs/rules/padding-around-describe-blocks.md +42 -0
  40. package/docs/rules/padding-around-expect-groups.md +44 -0
  41. package/docs/rules/padding-around-test-blocks.md +48 -0
  42. package/docs/rules/prefer-called-with.md +39 -0
  43. package/docs/rules/prefer-comparison-matcher.md +62 -0
  44. package/docs/rules/prefer-each.md +58 -0
  45. package/docs/rules/prefer-ending-with-an-expect.md +170 -0
  46. package/docs/rules/prefer-equality-matcher.md +36 -0
  47. package/docs/rules/prefer-expect-assertions.md +230 -0
  48. package/docs/rules/prefer-expect-resolves.md +62 -0
  49. package/docs/rules/prefer-hooks-in-order.md +137 -0
  50. package/docs/rules/prefer-hooks-on-top.md +124 -0
  51. package/docs/rules/prefer-importing-jest-globals.md +85 -0
  52. package/docs/rules/prefer-jest-mocked.md +40 -0
  53. package/docs/rules/prefer-lowercase-title.md +128 -0
  54. package/docs/rules/prefer-mock-promise-shorthand.md +41 -0
  55. package/docs/rules/prefer-mock-return-shorthand.md +50 -0
  56. package/docs/rules/prefer-snapshot-hint.md +192 -0
  57. package/docs/rules/prefer-spy-on.md +46 -0
  58. package/docs/rules/prefer-strict-equal.md +29 -0
  59. package/docs/rules/prefer-to-be.md +63 -0
  60. package/docs/rules/prefer-to-contain.md +53 -0
  61. package/docs/rules/prefer-to-have-been-called-times.md +43 -0
  62. package/docs/rules/prefer-to-have-been-called.md +35 -0
  63. package/docs/rules/prefer-to-have-length.md +41 -0
  64. package/docs/rules/prefer-todo.md +33 -0
  65. package/docs/rules/require-hook.md +187 -0
  66. package/docs/rules/require-to-throw-message.md +43 -0
  67. package/docs/rules/require-top-level-describe.md +86 -0
  68. package/docs/rules/unbound-method.md +61 -0
  69. package/docs/rules/valid-describe-callback.md +69 -0
  70. package/docs/rules/valid-expect-in-promise.md +79 -0
  71. package/docs/rules/valid-expect-with-promise.md +43 -0
  72. package/docs/rules/valid-expect.md +155 -0
  73. package/docs/rules/valid-mock-module-path.md +72 -0
  74. package/docs/rules/valid-title.md +319 -0
  75. package/index.d.ts +28 -0
  76. package/lib/globals.json +15 -0
  77. package/lib/index.js +85 -0
  78. package/lib/rules/consistent-test-it.js +101 -0
  79. package/lib/rules/expect-expect.js +102 -0
  80. package/lib/rules/max-expects.js +70 -0
  81. package/lib/rules/max-nested-describe.js +59 -0
  82. package/lib/rules/no-alias-methods.js +62 -0
  83. package/lib/rules/no-commented-out-tests.js +44 -0
  84. package/lib/rules/no-conditional-expect.js +80 -0
  85. package/lib/rules/no-conditional-in-test.js +63 -0
  86. package/lib/rules/no-confusing-set-timeout.js +61 -0
  87. package/lib/rules/no-deprecated-functions.js +81 -0
  88. package/lib/rules/no-disabled-tests.js +57 -0
  89. package/lib/rules/no-done-callback.js +120 -0
  90. package/lib/rules/no-duplicate-hooks.js +52 -0
  91. package/lib/rules/no-error-equal.js +41 -0
  92. package/lib/rules/no-export.js +61 -0
  93. package/lib/rules/no-focused-tests.js +63 -0
  94. package/lib/rules/no-hooks.js +51 -0
  95. package/lib/rules/no-identical-title.js +73 -0
  96. package/lib/rules/no-interpolation-in-snapshots.js +43 -0
  97. package/lib/rules/no-jasmine-globals.js +140 -0
  98. package/lib/rules/no-large-snapshots.js +100 -0
  99. package/lib/rules/no-mocks-import.js +46 -0
  100. package/lib/rules/no-restricted-jest-methods.js +53 -0
  101. package/lib/rules/no-restricted-matchers.js +60 -0
  102. package/lib/rules/no-standalone-expect.js +114 -0
  103. package/lib/rules/no-test-prefixes.js +48 -0
  104. package/lib/rules/no-test-return-statement.js +62 -0
  105. package/lib/rules/no-unnecessary-assertion.js +87 -0
  106. package/lib/rules/no-unneeded-async-expect-function.js +57 -0
  107. package/lib/rules/no-untyped-mock-factory.js +61 -0
  108. package/lib/rules/padding-around-after-all-blocks.js +17 -0
  109. package/lib/rules/padding-around-after-each-blocks.js +17 -0
  110. package/lib/rules/padding-around-all.js +15 -0
  111. package/lib/rules/padding-around-before-all-blocks.js +17 -0
  112. package/lib/rules/padding-around-before-each-blocks.js +17 -0
  113. package/lib/rules/padding-around-describe-blocks.js +17 -0
  114. package/lib/rules/padding-around-expect-groups.js +21 -0
  115. package/lib/rules/padding-around-test-blocks.js +17 -0
  116. package/lib/rules/prefer-called-with.js +47 -0
  117. package/lib/rules/prefer-comparison-matcher.js +111 -0
  118. package/lib/rules/prefer-each.js +77 -0
  119. package/lib/rules/prefer-ending-with-an-expect.js +102 -0
  120. package/lib/rules/prefer-equality-matcher.js +87 -0
  121. package/lib/rules/prefer-expect-assertions.js +237 -0
  122. package/lib/rules/prefer-expect-resolves.js +47 -0
  123. package/lib/rules/prefer-hooks-in-order.js +67 -0
  124. package/lib/rules/prefer-hooks-on-top.js +41 -0
  125. package/lib/rules/prefer-importing-jest-globals.js +126 -0
  126. package/lib/rules/prefer-jest-mocked.js +66 -0
  127. package/lib/rules/prefer-lowercase-title.js +128 -0
  128. package/lib/rules/prefer-mock-promise-shorthand.js +88 -0
  129. package/lib/rules/prefer-mock-return-shorthand.js +122 -0
  130. package/lib/rules/prefer-snapshot-hint.js +111 -0
  131. package/lib/rules/prefer-spy-on.js +80 -0
  132. package/lib/rules/prefer-strict-equal.js +46 -0
  133. package/lib/rules/prefer-to-be.js +98 -0
  134. package/lib/rules/prefer-to-contain.js +82 -0
  135. package/lib/rules/prefer-to-have-been-called-times.js +70 -0
  136. package/lib/rules/prefer-to-have-been-called.js +59 -0
  137. package/lib/rules/prefer-to-have-length.js +57 -0
  138. package/lib/rules/prefer-todo.js +72 -0
  139. package/lib/rules/require-hook.js +93 -0
  140. package/lib/rules/require-to-throw-message.js +45 -0
  141. package/lib/rules/require-top-level-describe.js +86 -0
  142. package/lib/rules/unbound-method.js +86 -0
  143. package/lib/rules/utils/accessors.js +127 -0
  144. package/lib/rules/utils/ast-utils.js +59 -0
  145. package/lib/rules/utils/detectJestVersion.js +24 -0
  146. package/lib/rules/utils/followTypeAssertionChain.js +10 -0
  147. package/lib/rules/utils/index.js +71 -0
  148. package/lib/rules/utils/misc.js +168 -0
  149. package/lib/rules/utils/padding.js +308 -0
  150. package/lib/rules/utils/parseJestFnCall.js +325 -0
  151. package/lib/rules/utils/ts.js +73 -0
  152. package/lib/rules/valid-describe-callback.js +94 -0
  153. package/lib/rules/valid-expect-in-promise.js +311 -0
  154. package/lib/rules/valid-expect-with-promise.js +54 -0
  155. package/lib/rules/valid-expect.js +302 -0
  156. package/lib/rules/valid-mock-module-path.js +90 -0
  157. package/lib/rules/valid-title.js +252 -0
  158. package/package.json +164 -0
@@ -0,0 +1,142 @@
1
+ # jest/no-conditional-expect
2
+
3
+ 📝 Disallow calling `expect` conditionally.
4
+
5
+ 💼 This rule is enabled in the ✅ `recommended`
6
+ [config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
7
+
8
+ <!-- end auto-generated rule header -->
9
+
10
+ This rule prevents the use of `expect` in conditional blocks, such as `if`s &
11
+ `catch`s.
12
+
13
+ This includes using `expect` in callbacks to functions named `catch`, which are
14
+ assumed to be promises.
15
+
16
+ ## Rule details
17
+
18
+ Jest only considers a test to have failed if it throws an error, meaning if
19
+ calls to assertion functions like `expect` occur in conditional code such as a
20
+ `catch` statement, tests can end up passing but not actually test anything.
21
+
22
+ Additionally, conditionals tend to make tests more brittle and complex, as they
23
+ increase the amount of mental thinking needed to understand what is actually
24
+ being tested.
25
+
26
+ While `expect.assertions` & `expect.hasAssertions` can help prevent tests from
27
+ silently being skipped, when combined with conditionals they typically result in
28
+ even more complexity being introduced.
29
+
30
+ The following patterns are warnings:
31
+
32
+ ```js
33
+ it('foo', () => {
34
+ doTest && expect(1).toBe(2);
35
+ });
36
+
37
+ it('bar', () => {
38
+ if (!skipTest) {
39
+ expect(1).toEqual(2);
40
+ }
41
+ });
42
+
43
+ it('baz', async () => {
44
+ try {
45
+ await foo();
46
+ } catch (err) {
47
+ expect(err).toMatchObject({ code: 'MODULE_NOT_FOUND' });
48
+ }
49
+ });
50
+
51
+ it('throws an error', async () => {
52
+ await foo().catch(error => expect(error).toBeInstanceOf(error));
53
+ });
54
+ ```
55
+
56
+ The following patterns are not warnings:
57
+
58
+ ```js
59
+ it('foo', () => {
60
+ expect(!value).toBe(false);
61
+ });
62
+
63
+ function getValue() {
64
+ if (process.env.FAIL) {
65
+ return 1;
66
+ }
67
+
68
+ return 2;
69
+ }
70
+
71
+ it('foo', () => {
72
+ expect(getValue()).toBe(2);
73
+ });
74
+
75
+ it('validates the request', () => {
76
+ try {
77
+ processRequest(request);
78
+ } catch {
79
+ // ignore errors
80
+ } finally {
81
+ expect(validRequest).toHaveBeenCalledWith(request);
82
+ }
83
+ });
84
+
85
+ it('throws an error', async () => {
86
+ await expect(foo).rejects.toThrow(Error);
87
+ });
88
+ ```
89
+
90
+ ### How to catch a thrown error for testing without violating this rule
91
+
92
+ A common situation that comes up with this rule is when wanting to test
93
+ properties on a thrown error, as Jest's `toThrow` matcher only checks the
94
+ `message` property.
95
+
96
+ Most people write something like this:
97
+
98
+ ```typescript
99
+ describe('when the http request fails', () => {
100
+ it('includes the status code in the error', async () => {
101
+ try {
102
+ await makeRequest(url);
103
+ } catch (error) {
104
+ expect(error).toHaveProperty('statusCode', 404);
105
+ }
106
+ });
107
+ });
108
+ ```
109
+
110
+ As stated above, the problem with this is that if `makeRequest()` doesn't throw
111
+ the test will still pass as if the `expect` had been called.
112
+
113
+ While you can use `expect.assertions` & `expect.hasAssertions` for these
114
+ situations, they only work with `expect`.
115
+
116
+ A better way to handle this situation is to introduce a wrapper to handle the
117
+ catching, and otherwise return a specific "no error thrown" error if nothing is
118
+ thrown by the wrapped function:
119
+
120
+ ```typescript
121
+ class NoErrorThrownError extends Error {}
122
+
123
+ const getError = async <TError>(call: () => unknown): Promise<TError> => {
124
+ try {
125
+ await call();
126
+
127
+ throw new NoErrorThrownError();
128
+ } catch (error: unknown) {
129
+ return error as TError;
130
+ }
131
+ };
132
+
133
+ describe('when the http request fails', () => {
134
+ it('includes the status code in the error', async () => {
135
+ const error = await getError(async () => makeRequest(url));
136
+
137
+ // check that the returned error wasn't that no error was thrown
138
+ expect(error).not.toBeInstanceOf(NoErrorThrownError);
139
+ expect(error).toHaveProperty('statusCode', 404);
140
+ });
141
+ });
142
+ ```
@@ -0,0 +1,123 @@
1
+ # jest/no-conditional-in-test
2
+
3
+ 📝 Disallow conditional logic in tests.
4
+
5
+ <!-- end auto-generated rule header -->
6
+
7
+ Conditional logic in tests is usually an indication that a test is attempting to
8
+ cover too much, and not testing the logic it intends to. Each branch of code
9
+ executing within a conditional statement will usually be better served by a test
10
+ devoted to it.
11
+
12
+ ## Rule details
13
+
14
+ This rule reports on any use of a conditional statement such as `if`, `switch`,
15
+ and ternary expressions.
16
+
17
+ Examples of **incorrect** code for this rule:
18
+
19
+ ```js
20
+ it('foo', () => {
21
+ if (true) {
22
+ doTheThing();
23
+ }
24
+ });
25
+
26
+ it('bar', () => {
27
+ switch (mode) {
28
+ case 'none':
29
+ generateNone();
30
+ case 'single':
31
+ generateOne();
32
+ case 'multiple':
33
+ generateMany();
34
+ }
35
+
36
+ expect(fixtures.length).toBeGreaterThan(-1);
37
+ });
38
+
39
+ it('qux', async () => {
40
+ const promiseValue = () => {
41
+ return something instanceof Promise
42
+ ? something
43
+ : Promise.resolve(something);
44
+ };
45
+
46
+ await expect(promiseValue()).resolves.toBe(1);
47
+ });
48
+ ```
49
+
50
+ Examples of **correct** code for this rule:
51
+
52
+ ```js
53
+ describe('my tests', () => {
54
+ if (true) {
55
+ it('foo', () => {
56
+ doTheThing();
57
+ });
58
+ }
59
+ });
60
+
61
+ beforeEach(() => {
62
+ switch (mode) {
63
+ case 'none':
64
+ generateNone();
65
+ case 'single':
66
+ generateOne();
67
+ case 'multiple':
68
+ generateMany();
69
+ }
70
+ });
71
+
72
+ it('bar', () => {
73
+ expect(fixtures.length).toBeGreaterThan(-1);
74
+ });
75
+
76
+ const promiseValue = something => {
77
+ return something instanceof Promise ? something : Promise.resolve(something);
78
+ };
79
+
80
+ it('qux', async () => {
81
+ await expect(promiseValue()).resolves.toBe(1);
82
+ });
83
+ ```
84
+
85
+ ## Options
86
+
87
+ ```json
88
+ {
89
+ "jest/no-conditional-in-test": [
90
+ "error",
91
+ {
92
+ "allowOptionalChaining": true
93
+ }
94
+ ]
95
+ }
96
+ ```
97
+
98
+ ### `allowOptionalChaining`
99
+
100
+ Default: `true`
101
+
102
+ When set to `false`, optional chaining (`?.`) inside test bodies will be
103
+ reported as a conditional.
104
+
105
+ Examples of **incorrect** code when `allowOptionalChaining` is `false`:
106
+
107
+ ```js
108
+ it('foo', () => {
109
+ const value = obj?.bar;
110
+ });
111
+
112
+ it('bar', () => {
113
+ obj?.foo();
114
+ });
115
+ ```
116
+
117
+ Examples of **correct** code when `allowOptionalChaining` is `false`:
118
+
119
+ ```js
120
+ it('foo', () => {
121
+ const value = obj!.bar;
122
+ });
123
+ ```
@@ -0,0 +1,64 @@
1
+ # jest/no-confusing-set-timeout
2
+
3
+ 📝 Disallow confusing usages of jest.setTimeout.
4
+
5
+ <!-- end auto-generated rule header -->
6
+
7
+ While `jest.setTimeout` can be called multiple times anywhere within a single
8
+ test file only the last call before any test functions run will have an effect.
9
+
10
+ ## Rule details
11
+
12
+ this rule checks for several confusing usages of `jest.setTimeout` that looks
13
+ like it applies to specific tests within the same file, such as:
14
+
15
+ - being called anywhere other than in global scope
16
+ - being called multiple times
17
+ - being called after other Jest functions like hooks, `describe`, `test`, or
18
+ `it`
19
+
20
+ Examples of **incorrect** code for this rule:
21
+
22
+ ```js
23
+ describe('test foo', () => {
24
+ jest.setTimeout(1000);
25
+ it('test-description', () => {
26
+ // test logic;
27
+ });
28
+ });
29
+
30
+ describe('test bar', () => {
31
+ it('test-description', () => {
32
+ jest.setTimeout(1000);
33
+ // test logic;
34
+ });
35
+ });
36
+
37
+ test('foo-bar', () => {
38
+ jest.setTimeout(1000);
39
+ });
40
+
41
+ describe('unit test', () => {
42
+ beforeEach(() => {
43
+ jest.setTimeout(1000);
44
+ });
45
+ });
46
+ ```
47
+
48
+ Examples of **correct** code for this rule:
49
+
50
+ ```js
51
+ jest.setTimeout(500);
52
+ test('test test', () => {
53
+ // do some stuff
54
+ });
55
+ ```
56
+
57
+ ```js
58
+ jest.setTimeout(1000);
59
+ describe('test bar bar', () => {
60
+ it('test-description', () => {
61
+ // test logic;
62
+ });
63
+ });
64
+ ```
@@ -0,0 +1,59 @@
1
+ # jest/no-deprecated-functions
2
+
3
+ 📝 Disallow use of deprecated functions.
4
+
5
+ 💼 This rule is enabled in the ✅ `recommended`
6
+ [config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
7
+
8
+ 🔧 This rule is automatically fixable by the
9
+ [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
10
+
11
+ <!-- end auto-generated rule header -->
12
+
13
+ Over the years Jest has accrued some debt in the form of functions that have
14
+ either been renamed for clarity, or replaced with more powerful APIs.
15
+
16
+ While typically these deprecated functions are kept in the codebase for a number
17
+ of majors, eventually they are removed completely.
18
+
19
+ This rule requires knowing which version of Jest you're using - see
20
+ [this section of the readme](../../README.md#jest-version-setting) for details
21
+ on how that is obtained automatically and how you can explicitly provide a
22
+ version if needed.
23
+
24
+ ## Rule details
25
+
26
+ This rule warns about calls to deprecated functions, and provides details on
27
+ what to replace them with, based on the version of Jest that is installed.
28
+
29
+ This rule can also autofix a number of these deprecations for you.
30
+
31
+ ### `jest.resetModuleRegistry`
32
+
33
+ This function was renamed to `resetModules` in Jest 15 and removed in Jest 27.
34
+
35
+ ### `jest.addMatchers`
36
+
37
+ This function was replaced with `expect.extend` in Jest 17 and removed in
38
+ Jest 27.
39
+
40
+ ### `require.requireActual` & `require.requireMock`
41
+
42
+ These functions were replaced in Jest 21 and removed in Jest 26.
43
+
44
+ Originally, the `requireActual` & `requireMock` the `requireActual`&
45
+ `requireMock` functions were placed onto the `require` function.
46
+
47
+ These functions were later moved onto the `jest` object in order to be easier
48
+ for type checkers to handle, and their use via `require` deprecated. Finally,
49
+ the release of Jest 26 saw them removed from the `require` function altogether.
50
+
51
+ ### `jest.runTimersToTime`
52
+
53
+ This function was renamed to `advanceTimersByTime` in Jest 22 and removed in
54
+ Jest 27.
55
+
56
+ ### `jest.genMockFromModule`
57
+
58
+ This function was renamed to `createMockFromModule` in Jest 26, and removed in
59
+ Jest 30.
@@ -0,0 +1,72 @@
1
+ # jest/no-disabled-tests
2
+
3
+ 📝 Disallow disabled tests.
4
+
5
+ ⚠️ This rule _warns_ in the ✅ `recommended`
6
+ [config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
7
+
8
+ <!-- end auto-generated rule header -->
9
+
10
+ Jest has a feature that allows you to temporarily mark tests as disabled. This
11
+ feature is often helpful while debugging or to create placeholders for future
12
+ tests. Before committing changes we may want to check that all tests are
13
+ running.
14
+
15
+ This rule raises a warning about disabled tests.
16
+
17
+ ## Rule details
18
+
19
+ There are a number of ways to disable tests in Jest:
20
+
21
+ - by appending `.skip` to the test-suite or test-case
22
+ - by prepending the test function name with `x`
23
+ - by declaring a test with a name but no function body
24
+ - by making a call to `pending()` anywhere within the test
25
+
26
+ The following patterns are considered warnings:
27
+
28
+ ```js
29
+ describe.skip('foo', () => {});
30
+ it.skip('foo', () => {});
31
+ test.skip('foo', () => {});
32
+
33
+ describe['skip']('bar', () => {});
34
+ it['skip']('bar', () => {});
35
+ test['skip']('bar', () => {});
36
+
37
+ xdescribe('foo', () => {});
38
+ xit('foo', () => {});
39
+ xtest('foo', () => {});
40
+
41
+ it('bar');
42
+ test('bar');
43
+
44
+ it('foo', () => {
45
+ pending();
46
+ });
47
+ ```
48
+
49
+ These patterns would not be considered warnings:
50
+
51
+ ```js
52
+ describe('foo', () => {});
53
+ it('foo', () => {});
54
+ test('foo', () => {});
55
+
56
+ describe.only('bar', () => {});
57
+ it.only('bar', () => {});
58
+ test.only('bar', () => {});
59
+ ```
60
+
61
+ ### Limitations
62
+
63
+ The plugin looks at the literal function names within test code, so will not
64
+ catch more complex examples of disabled tests, such as:
65
+
66
+ ```js
67
+ const testSkip = test.skip;
68
+ testSkip('skipped test', () => {});
69
+
70
+ const myTest = test;
71
+ myTest('does not have function body');
72
+ ```
@@ -0,0 +1,100 @@
1
+ # jest/no-done-callback
2
+
3
+ 📝 Disallow using a callback in asynchronous tests and hooks.
4
+
5
+ 💼 This rule is enabled in the ✅ `recommended`
6
+ [config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
7
+
8
+ 💡 This rule is manually fixable by
9
+ [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
10
+
11
+ <!-- end auto-generated rule header -->
12
+
13
+ When calling asynchronous code in hooks and tests, `jest` needs to know when the
14
+ asynchronous work is complete to progress the current run.
15
+
16
+ Originally the most common pattern to achieve this was to use callbacks:
17
+
18
+ ```js
19
+ test('the data is peanut butter', done => {
20
+ function callback(data) {
21
+ try {
22
+ expect(data).toBe('peanut butter');
23
+ done();
24
+ } catch (error) {
25
+ done(error);
26
+ }
27
+ }
28
+
29
+ fetchData(callback);
30
+ });
31
+ ```
32
+
33
+ This can be very error-prone however, as it requires careful understanding of
34
+ how assertions work in tests or otherwise tests won't behave as expected.
35
+
36
+ For example, if the `try/catch` was left out of the above code, the test would
37
+ time out rather than fail. Even with the `try/catch`, forgetting to pass the
38
+ caught error to `done` will result in `jest` believing the test has passed.
39
+
40
+ A more straightforward way to handle asynchronous code is to use Promises:
41
+
42
+ ```js
43
+ test('the data is peanut butter', () => {
44
+ return fetchData().then(data => {
45
+ expect(data).toBe('peanut butter');
46
+ });
47
+ });
48
+ ```
49
+
50
+ When a test or hook returns a promise, `jest` waits for that promise to resolve,
51
+ as well as automatically failing should the promise reject.
52
+
53
+ If your environment supports `async/await`, this becomes even simpler:
54
+
55
+ ```js
56
+ test('the data is peanut butter', async () => {
57
+ const data = await fetchData();
58
+ expect(data).toBe('peanut butter');
59
+ });
60
+ ```
61
+
62
+ ## Rule details
63
+
64
+ This rule checks the function parameter of hooks & tests for use of the `done`
65
+ argument, suggesting you return a promise instead.
66
+
67
+ The following patterns are considered warnings:
68
+
69
+ ```js
70
+ beforeEach(done => {
71
+ // ...
72
+ });
73
+
74
+ test('myFunction()', done => {
75
+ // ...
76
+ });
77
+
78
+ test('myFunction()', function (done) {
79
+ // ...
80
+ });
81
+ ```
82
+
83
+ The following patterns are not considered warnings:
84
+
85
+ ```js
86
+ beforeEach(async () => {
87
+ await setupUsTheBomb();
88
+ });
89
+
90
+ test('myFunction()', () => {
91
+ expect(myFunction()).toBeTruthy();
92
+ });
93
+
94
+ test('myFunction()', () => {
95
+ return new Promise(done => {
96
+ expect(myFunction()).toBeTruthy();
97
+ done();
98
+ });
99
+ });
100
+ ```
@@ -0,0 +1,79 @@
1
+ # jest/no-duplicate-hooks
2
+
3
+ 📝 Disallow duplicate setup and teardown hooks.
4
+
5
+ <!-- end auto-generated rule header -->
6
+
7
+ A `describe` block should not contain duplicate hooks.
8
+
9
+ ## Rule details
10
+
11
+ Examples of **incorrect** code for this rule
12
+
13
+ ```js
14
+ /* eslint jest/no-duplicate-hooks: "error" */
15
+
16
+ describe('foo', () => {
17
+ beforeEach(() => {
18
+ // some setup
19
+ });
20
+ beforeEach(() => {
21
+ // some setup
22
+ });
23
+ test('foo_test', () => {
24
+ // some test
25
+ });
26
+ });
27
+
28
+ // Nested describe scenario
29
+ describe('foo', () => {
30
+ beforeEach(() => {
31
+ // some setup
32
+ });
33
+ test('foo_test', () => {
34
+ // some test
35
+ });
36
+ describe('bar', () => {
37
+ test('bar_test', () => {
38
+ afterAll(() => {
39
+ // some teardown
40
+ });
41
+ afterAll(() => {
42
+ // some teardown
43
+ });
44
+ });
45
+ });
46
+ });
47
+ ```
48
+
49
+ Examples of **correct** code for this rule
50
+
51
+ ```js
52
+ /* eslint jest/no-duplicate-hooks: "error" */
53
+
54
+ describe('foo', () => {
55
+ beforeEach(() => {
56
+ // some setup
57
+ });
58
+ test('foo_test', () => {
59
+ // some test
60
+ });
61
+ });
62
+
63
+ // Nested describe scenario
64
+ describe('foo', () => {
65
+ beforeEach(() => {
66
+ // some setup
67
+ });
68
+ test('foo_test', () => {
69
+ // some test
70
+ });
71
+ describe('bar', () => {
72
+ test('bar_test', () => {
73
+ beforeEach(() => {
74
+ // some setup
75
+ });
76
+ });
77
+ });
78
+ });
79
+ ```
@@ -0,0 +1,37 @@
1
+ # jest/no-error-equal
2
+
3
+ 📝 Disallow using equality matchers on error types.
4
+
5
+ 💭 This rule requires
6
+ [type information](https://typescript-eslint.io/linting/typed-linting).
7
+
8
+ <!-- end auto-generated rule header -->
9
+
10
+ When comparing errors, `toEqual` and `toStrictEqual` will only compare the
11
+ `message` properties, meaning tests can pass even if the errors are of different
12
+ types.
13
+
14
+ Instead, it is better to use `toThrow` which does check the error type along
15
+ with its message.
16
+
17
+ ## Rule details
18
+
19
+ This rule warns when `toEqual` and `toStrictEqual` is used with an `Error` type.
20
+
21
+ The following patterns are considered warnings:
22
+
23
+ ```ts
24
+ expect(new AggregateError([], expect.any(String))).toEqual(
25
+ new Error(expect.any(String)),
26
+ );
27
+
28
+ expect(new Error('hello world')).toStrictEqual('hello sunshine');
29
+ ```
30
+
31
+ The following patterns are not considered warnings:
32
+
33
+ ```ts
34
+ expect(() => throw new AggregateError([], expect.any(String))).toThrow(new Error(expect.any(String)));
35
+
36
+ expect(() => throw new Error('hello world')).toThrow('hello sunshine');
37
+ ```