@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,155 @@
1
+ # jest/valid-expect
2
+
3
+ 📝 Enforce valid `expect()` usage.
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
+ > [!NOTE] Test function will be fixed if it is async and does not have await in
14
+ > the async assertion.
15
+
16
+ Ensure `expect()` is called with a single argument and there is an actual
17
+ expectation made.
18
+
19
+ ## Rule details
20
+
21
+ This rule triggers a warning if `expect()` is called with more than one argument
22
+ or without arguments. It would also issue a warning if there is nothing called
23
+ on `expect()`, e.g.:
24
+
25
+ ```js
26
+ expect();
27
+ expect('something');
28
+ ```
29
+
30
+ or when a matcher function was not called, e.g.:
31
+
32
+ ```js
33
+ expect(true).toBeDefined;
34
+ ```
35
+
36
+ or when an async assertion was not `await`ed or returned, e.g.:
37
+
38
+ ```js
39
+ expect(Promise.resolve('Hi!')).resolves.toBe('Hi!');
40
+ ```
41
+
42
+ This rule is enabled by default.
43
+
44
+ ## Options
45
+
46
+ ```json5
47
+ {
48
+ type: 'object',
49
+ properties: {
50
+ alwaysAwait: {
51
+ type: 'boolean',
52
+ default: false,
53
+ },
54
+ asyncMatchers: {
55
+ type: 'array',
56
+ items: { type: 'string' },
57
+ default: ['toResolve', 'toReject'],
58
+ },
59
+ minArgs: {
60
+ type: 'number',
61
+ minimum: 0,
62
+ },
63
+ maxArgs: {
64
+ type: 'number',
65
+ minimum: 1,
66
+ },
67
+ },
68
+ additionalProperties: false,
69
+ }
70
+ ```
71
+
72
+ ### `alwaysAwait`
73
+
74
+ Enforces to use `await` inside block statements. Using `return` will trigger a
75
+ warning. Returning one line statements with arrow functions is _always allowed_.
76
+
77
+ Examples of **incorrect** code for the `{ "alwaysAwait": true }` option:
78
+
79
+ ```js
80
+ // alwaysAwait: true
81
+ test('test1', async () => {
82
+ await expect(Promise.resolve(2)).resolves.toBeDefined();
83
+ return expect(Promise.resolve(1)).resolves.toBe(1); // `return` statement will trigger a warning
84
+ });
85
+ ```
86
+
87
+ Examples of **correct** code for the `{ "alwaysAwait": true }` option:
88
+
89
+ ```js
90
+ // alwaysAwait: true
91
+ test('test1', async () => {
92
+ await expect(Promise.resolve(2)).resolves.toBeDefined();
93
+ await expect(Promise.resolve(1)).resolves.toBe(1);
94
+ });
95
+
96
+ test('test2', () => expect(Promise.resolve(2)).resolves.toBe(2));
97
+ ```
98
+
99
+ ### `asyncMatchers`
100
+
101
+ Allows specifying which matchers return promises, and so should be considered
102
+ async when checking if an `expect` should be returned or awaited.
103
+
104
+ By default, this has a list of all the async matchers provided by
105
+ `jest-extended` (namely, `toResolve` and `toReject`).
106
+
107
+ ### `minArgs` & `maxArgs`
108
+
109
+ Enforces the minimum and maximum number of arguments that `expect` can take, and
110
+ is required to take.
111
+
112
+ Both of these properties have a default value of `1`, which is the number of
113
+ arguments supported by vanilla `expect`.
114
+
115
+ This is useful when you're using libraries that increase the number of arguments
116
+ supported by `expect`, such as
117
+ [`jest-expect-message`](https://www.npmjs.com/package/jest-expect-message).
118
+
119
+ The following patterns are considered warnings:
120
+
121
+ ```js
122
+ test('all the things', async () => {
123
+ expect();
124
+ expect().toEqual('something');
125
+ expect('something', 'else');
126
+ expect('something');
127
+ await expect('something');
128
+ expect(true).toBeDefined;
129
+ expect(Promise.resolve('hello')).resolves;
130
+ expect(Promise.resolve('hello')).resolves.toEqual('hello');
131
+ Promise.resolve(expect(Promise.resolve('hello')).resolves.toEqual('hello'));
132
+ Promise.all([
133
+ expect(Promise.resolve('hello')).resolves.toEqual('hello'),
134
+ expect(Promise.resolve('hi')).resolves.toEqual('hi'),
135
+ ]);
136
+ });
137
+ ```
138
+
139
+ The following patterns are not warnings:
140
+
141
+ ```js
142
+ test('all the things', async () => {
143
+ expect('something').toEqual('something');
144
+ expect([1, 2, 3]).toEqual([1, 2, 3]);
145
+ expect(true).toBeDefined();
146
+ await expect(Promise.resolve('hello')).resolves.toEqual('hello');
147
+ await Promise.resolve(
148
+ expect(Promise.resolve('hello')).resolves.toEqual('hello'),
149
+ );
150
+ await Promise.all([
151
+ expect(Promise.resolve('hello')).resolves.toEqual('hello'),
152
+ expect(Promise.resolve('hi')).resolves.toEqual('hi'),
153
+ ]);
154
+ });
155
+ ```
@@ -0,0 +1,72 @@
1
+ # jest/valid-mock-module-path
2
+
3
+ 📝 Disallow mocking of non-existing module paths.
4
+
5
+ <!-- end auto-generated rule header -->
6
+
7
+ This rule raises an error when using `jest.mock` and `jest.doMock` and the first
8
+ argument for mocked object (module/local file) do not exist.
9
+
10
+ ## Rule details
11
+
12
+ This rule checks existence of the supplied path for `jest.mock` or `jest.doMock`
13
+ in the first argument.
14
+
15
+ The following patterns are considered errors:
16
+
17
+ ```js
18
+ // Module(s) that cannot be found
19
+ jest.mock('@org/some-module-not-in-package-json');
20
+ jest.mock('some-module-not-in-package-json');
21
+
22
+ // Local module (directory) that cannot be found
23
+ jest.mock('../../this/module/does/not/exist');
24
+
25
+ // Local file that cannot be found
26
+ jest.mock('../../this/path/does/not/exist.js');
27
+ ```
28
+
29
+ The following patterns are **not** considered errors:
30
+
31
+ ```js
32
+ // Module(s) that can be found
33
+ jest.mock('@org/some-module-in-package-json');
34
+ jest.mock('some-module-in-package-json');
35
+
36
+ // Local module that cannot be found
37
+ jest.mock('../../this/module/really/does/exist');
38
+
39
+ // Local file that cannot be found
40
+ jest.mock('../../this/path/really/does/exist.js');
41
+ ```
42
+
43
+ ## Options
44
+
45
+ ```json
46
+ {
47
+ "jest/valid-mock-module-path": [
48
+ "error",
49
+ {
50
+ "moduleFileExtensions": [".js", ".ts", ".jsx", ".tsx", ".json"]
51
+ }
52
+ ]
53
+ }
54
+ ```
55
+
56
+ ### `moduleFileExtensions`
57
+
58
+ This array option controls which file extensions the plugin checks for
59
+ existence. The default extensions are:
60
+
61
+ - `".js"`
62
+ - `".ts"`
63
+ - `".jsx"`
64
+ - `".tsx"`
65
+ - `".json"`
66
+
67
+ For any custom extension, a preceding dot **must** be present before the file
68
+ extension for desired effect.
69
+
70
+ ## When Not To Use It
71
+
72
+ Don't use this rule on non-jest test files.
@@ -0,0 +1,319 @@
1
+ # jest/valid-title
2
+
3
+ 📝 Enforce valid titles.
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
+ Checks that the title of Jest blocks are valid by ensuring that titles are:
14
+
15
+ - not empty,
16
+ - is a string,
17
+ - not prefixed with their block name,
18
+ - have no leading or trailing spaces
19
+ - using valid printf specifiers when applicable
20
+
21
+ ## Rule details
22
+
23
+ **emptyTitle**
24
+
25
+ An empty title is not informative, and serves little purpose.
26
+
27
+ Examples of **incorrect** code for this rule:
28
+
29
+ ```js
30
+ describe('', () => {});
31
+ describe('foo', () => {
32
+ it('', () => {});
33
+ });
34
+ it('', () => {});
35
+ test('', () => {});
36
+ xdescribe('', () => {});
37
+ xit('', () => {});
38
+ xtest('', () => {});
39
+ ```
40
+
41
+ Examples of **correct** code for this rule:
42
+
43
+ ```js
44
+ describe('foo', () => {});
45
+ describe('foo', () => {
46
+ it('bar', () => {});
47
+ });
48
+ test('foo', () => {});
49
+ it('foo', () => {});
50
+ xdescribe('foo', () => {});
51
+ xit('foo', () => {});
52
+ xtest('foo', () => {});
53
+ ```
54
+
55
+ **invalidEachSpecifier**
56
+
57
+ Titles for
58
+ [array-based `.each`s](https://jestjs.io/docs/api#1-testeachtablename-fn-timeout_)
59
+ can use
60
+ [`printf` formatting](https://nodejs.org/api/util.html#util_util_format_format_args),
61
+ but there are specific specifiers that are allowed.
62
+
63
+ Examples of **incorrect** code for this rule:
64
+
65
+ ```js
66
+ test.each([
67
+ [1, 1, 2],
68
+ [1, 2, 3],
69
+ [2, 1, 3],
70
+ ])('.add(%I, %I)', (a, b, expected) => {
71
+ expect(a + b).toBe(expected);
72
+ });
73
+ ```
74
+
75
+ Examples of **correct** code for this rule:
76
+
77
+ ```js
78
+ test.each([
79
+ [1, 1, 2],
80
+ [1, 2, 3],
81
+ [2, 1, 3],
82
+ ])('.add(%i, %i)', (a, b, expected) => {
83
+ expect(a + b).toBe(expected);
84
+ });
85
+ ```
86
+
87
+ **titleMustBeString**
88
+
89
+ Titles for `describe`, `test`, and `it` blocks should always be a string; you
90
+ can disable this with the `ignoreTypeOfDescribeName` and `ignoreTypeOfTestName`
91
+ options.
92
+
93
+ Examples of **incorrect** code for this rule:
94
+
95
+ ```js
96
+ it(123, () => {});
97
+ describe(String(/.+/), () => {});
98
+ describe(myFunction, () => {});
99
+ xdescribe(myFunction, () => {});
100
+ describe(6, function () {});
101
+ ```
102
+
103
+ Examples of **correct** code for this rule:
104
+
105
+ ```js
106
+ it('is a string', () => {});
107
+ test('is a string', () => {});
108
+ xtest('is a string', () => {});
109
+ describe('is a string', () => {});
110
+ describe.skip('is a string', () => {});
111
+ fdescribe('is a string', () => {});
112
+ ```
113
+
114
+ Examples of **correct** code when `ignoreTypeOfDescribeName` is `true`:
115
+
116
+ ```js
117
+ it('is a string', () => {});
118
+ test('is a string', () => {});
119
+ xtest('is a string', () => {});
120
+ describe('is a string', () => {});
121
+ describe.skip('is a string', () => {});
122
+ fdescribe('is a string', () => {});
123
+
124
+ describe(String(/.+/), () => {});
125
+ describe(myFunction, () => {});
126
+ xdescribe(myFunction, () => {});
127
+ describe(6, function () {});
128
+ ```
129
+
130
+ Examples of **correct** code when `ignoreTypeOfTestName` is `true`:
131
+
132
+ ```js
133
+ const myTestName = 'is a string';
134
+
135
+ it(String(/.+/), () => {});
136
+ it(myFunction, () => {});
137
+ it(myTestName, () => {});
138
+ xit(myFunction, () => {});
139
+ it(6, function () {});
140
+ ```
141
+
142
+ **duplicatePrefix**
143
+
144
+ A `describe` / `test` block should not start with `duplicatePrefix`
145
+
146
+ Examples of **incorrect** code for this rule
147
+
148
+ ```js
149
+ test('test foo', () => {});
150
+ it('it foo', () => {});
151
+
152
+ describe('foo', () => {
153
+ test('test bar', () => {});
154
+ });
155
+
156
+ describe('describe foo', () => {
157
+ test('bar', () => {});
158
+ });
159
+ ```
160
+
161
+ Examples of **correct** code for this rule
162
+
163
+ ```js
164
+ test('foo', () => {});
165
+ it('foo', () => {});
166
+
167
+ describe('foo', () => {
168
+ test('bar', () => {});
169
+ });
170
+ ```
171
+
172
+ **accidentalSpace**
173
+
174
+ A `describe` / `test` block should not contain accidentalSpace, but can be
175
+ turned off via the `ignoreSpaces` option:
176
+
177
+ Examples of **incorrect** code for this rule
178
+
179
+ ```js
180
+ test(' foo', () => {});
181
+ it(' foo', () => {});
182
+
183
+ describe('foo', () => {
184
+ test(' bar', () => {});
185
+ });
186
+
187
+ describe(' foo', () => {
188
+ test('bar', () => {});
189
+ });
190
+
191
+ describe('foo ', () => {
192
+ test('bar', () => {});
193
+ });
194
+ ```
195
+
196
+ Examples of **correct** code for this rule
197
+
198
+ ```js
199
+ test('foo', () => {});
200
+ it('foo', () => {});
201
+
202
+ describe('foo', () => {
203
+ test('bar', () => {});
204
+ });
205
+ ```
206
+
207
+ ## Options
208
+
209
+ ```ts
210
+ interface Options {
211
+ ignoreSpaces?: boolean;
212
+ ignoreTypeOfDescribeName?: boolean;
213
+ ignoreTypeOfTestName?: boolean;
214
+ disallowedWords?: string[];
215
+ mustNotMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
216
+ mustMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
217
+ }
218
+ ```
219
+
220
+ #### `ignoreSpaces`
221
+
222
+ Default: `false`
223
+
224
+ When enabled, the leading and trailing spaces won't be checked.
225
+
226
+ #### `ignoreTypeOfDescribeName` & `ignoreTypeOfTestName`
227
+
228
+ Default: `false`
229
+
230
+ When enabled, the type of the first argument to `describe`/`test` blocks won't
231
+ be checked.
232
+
233
+ #### `disallowedWords`
234
+
235
+ Default: `[]`
236
+
237
+ A string array of words that are not allowed to be used in test titles. Matching
238
+ is not case-sensitive, and looks for complete words:
239
+
240
+ Examples of **incorrect** code when using `disallowedWords`:
241
+
242
+ ```js
243
+ // with disallowedWords: ['correct', 'all', 'every', 'properly']
244
+ describe('the correct way to do things', () => {});
245
+ it('has ALL the things', () => {});
246
+ xdescribe('every single one of them', () => {});
247
+ test(`that the value is set properly`, () => {});
248
+ ```
249
+
250
+ Examples of **correct** code when using `disallowedWords`:
251
+
252
+ ```js
253
+ // with disallowedWords: ['correct', 'all', 'every', 'properly']
254
+ it('correctly sets the value', () => {});
255
+ test('that everything is as it should be', () => {});
256
+ describe('the proper way to handle things', () => {});
257
+ ```
258
+
259
+ #### `mustMatch` & `mustNotMatch`
260
+
261
+ Defaults: `{}`
262
+
263
+ Allows enforcing that titles must match or must not match a given Regular
264
+ Expression, with an optional message. An object can be provided to apply
265
+ different Regular Expressions (with optional messages) to specific Jest test
266
+ function groups (`describe`, `test`, and `it`).
267
+
268
+ Examples of **incorrect** code when using `mustMatch`:
269
+
270
+ ```js
271
+ // with mustMatch: '^that'
272
+ describe('the correct way to do things', () => {});
273
+ fit('this there!', () => {});
274
+
275
+ // with mustMatch: { test: '^that' }
276
+ describe('the tests that will be run', () => {});
277
+ test('the stuff works', () => {});
278
+ xtest('errors that are thrown have messages', () => {});
279
+ ```
280
+
281
+ Examples of **correct** code when using `mustMatch`:
282
+
283
+ ```js
284
+ // with mustMatch: '^that'
285
+ describe('that thing that needs to be done', () => {});
286
+ fit('that this there!', () => {});
287
+
288
+ // with mustMatch: { test: '^that' }
289
+ describe('the tests that will be run', () => {});
290
+ test('that the stuff works', () => {});
291
+ xtest('that errors that thrown have messages', () => {});
292
+ ```
293
+
294
+ Optionally you can provide a custom message to show for a particular matcher by
295
+ using a tuple at any level where you can provide a matcher:
296
+
297
+ ```js
298
+ const prefixes = ['when', 'with', 'without', 'if', 'unless', 'for'];
299
+ const prefixesList = prefixes.join(' - \n');
300
+
301
+ module.exports = {
302
+ rules: {
303
+ 'jest/valid-title': [
304
+ 'error',
305
+ {
306
+ mustNotMatch: ['\\.$', 'Titles should not end with a full-stop'],
307
+ mustMatch: {
308
+ describe: [
309
+ new RegExp(`^(?:[A-Z]|\\b(${prefixes.join('|')})\\b`, 'u').source,
310
+ `Describe titles should either start with a capital letter or one of the following prefixes: ${prefixesList}`,
311
+ ],
312
+ test: [/[^A-Z]/u.source],
313
+ it: /[^A-Z]/u.source,
314
+ },
315
+ },
316
+ ],
317
+ },
318
+ };
319
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ import type { Linter, Rule } from 'eslint';
2
+
3
+ declare const plugin: {
4
+ meta: {
5
+ name: string;
6
+ version: string;
7
+ };
8
+ environments: {
9
+ globals: {
10
+ globals: {
11
+ [key: string]: boolean;
12
+ };
13
+ };
14
+ };
15
+ configs: {
16
+ all: Linter.LegacyConfig;
17
+ recommended: Linter.LegacyConfig;
18
+ style: Linter.LegacyConfig;
19
+ 'flat/all': Linter.FlatConfig;
20
+ 'flat/recommended': Linter.FlatConfig;
21
+ 'flat/style': Linter.FlatConfig;
22
+ };
23
+ rules: {
24
+ [key: string]: Rule.RuleModule;
25
+ };
26
+ };
27
+
28
+ export = plugin;
@@ -0,0 +1,15 @@
1
+ {
2
+ "afterAll": false,
3
+ "afterEach": false,
4
+ "beforeAll": false,
5
+ "beforeEach": false,
6
+ "describe": false,
7
+ "expect": false,
8
+ "fit": false,
9
+ "it": false,
10
+ "jest": false,
11
+ "test": false,
12
+ "xdescribe": false,
13
+ "xit": false,
14
+ "xtest": false
15
+ }
package/lib/index.js ADDED
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+
3
+ var _fs = require("fs");
4
+ var _path = require("path");
5
+ var _package = require("../package.json");
6
+ var _globals = _interopRequireDefault(require("./globals.json"));
7
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
+ // copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
9
+ /* istanbul ignore next */
10
+ const interopRequireDefault = obj => obj && obj.__esModule ? obj : {
11
+ default: obj
12
+ };
13
+ const importDefault = moduleName =>
14
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
15
+ interopRequireDefault(require(moduleName)).default;
16
+ const rulesDir = (0, _path.join)(__dirname, 'rules');
17
+ const excludedFiles = ['__tests__', 'detectJestVersion', 'utils'];
18
+ const rules = Object.fromEntries((0, _fs.readdirSync)(rulesDir).map(rule => (0, _path.parse)(rule).name).filter(rule => !excludedFiles.includes(rule)).map(rule => [rule, importDefault((0, _path.join)(rulesDir, rule))]));
19
+ const recommendedRules = {
20
+ 'jest/expect-expect': 'warn',
21
+ 'jest/no-alias-methods': 'error',
22
+ 'jest/no-commented-out-tests': 'warn',
23
+ 'jest/no-conditional-expect': 'error',
24
+ 'jest/no-deprecated-functions': 'error',
25
+ 'jest/no-disabled-tests': 'warn',
26
+ 'jest/no-done-callback': 'error',
27
+ 'jest/no-export': 'error',
28
+ 'jest/no-focused-tests': 'error',
29
+ 'jest/no-identical-title': 'error',
30
+ 'jest/no-interpolation-in-snapshots': 'error',
31
+ 'jest/no-jasmine-globals': 'error',
32
+ 'jest/no-mocks-import': 'error',
33
+ 'jest/no-standalone-expect': 'error',
34
+ 'jest/no-test-prefixes': 'error',
35
+ 'jest/valid-describe-callback': 'error',
36
+ 'jest/valid-expect': 'error',
37
+ 'jest/valid-expect-in-promise': 'error',
38
+ 'jest/valid-title': 'error'
39
+ };
40
+ const styleRules = {
41
+ 'jest/prefer-to-be': 'error',
42
+ 'jest/prefer-to-contain': 'error',
43
+ 'jest/prefer-to-have-length': 'error'
44
+ };
45
+ const allRules = Object.fromEntries(Object.entries(rules).filter(([, rule]) => !rule.meta.deprecated).map(([name]) => [`jest/${name}`, 'error']));
46
+ const plugin = {
47
+ meta: {
48
+ name: _package.name,
49
+ version: _package.version
50
+ },
51
+ // ugly cast for now to keep TypeScript happy since
52
+ // we don't have types for flat config yet
53
+ configs: {},
54
+ environments: {
55
+ globals: {
56
+ globals: _globals.default
57
+ }
58
+ },
59
+ rules
60
+ };
61
+ const createRCConfig = rules => ({
62
+ plugins: ['jest'],
63
+ env: {
64
+ 'jest/globals': true
65
+ },
66
+ rules
67
+ });
68
+ const createFlatConfig = rules => ({
69
+ plugins: {
70
+ jest: plugin
71
+ },
72
+ languageOptions: {
73
+ globals: _globals.default
74
+ },
75
+ rules
76
+ });
77
+ plugin.configs = {
78
+ all: createRCConfig(allRules),
79
+ recommended: createRCConfig(recommendedRules),
80
+ style: createRCConfig(styleRules),
81
+ 'flat/all': createFlatConfig(allRules),
82
+ 'flat/recommended': createFlatConfig(recommendedRules),
83
+ 'flat/style': createFlatConfig(styleRules)
84
+ };
85
+ module.exports = plugin;