eslint-plugin-jest 27.2.0 → 27.2.2

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 (79) hide show
  1. package/README.md +1 -1
  2. package/docs/rules/no-alias-methods.md +1 -1
  3. package/docs/rules/no-hooks.md +1 -1
  4. package/docs/rules/no-if.md +1 -1
  5. package/docs/rules/no-large-snapshots.md +4 -0
  6. package/docs/rules/require-hook.md +2 -6
  7. package/docs/rules/valid-expect.md +2 -2
  8. package/lib/index.js +5 -12
  9. package/lib/processors/__tests__/snapshot-processor.test.js +36 -0
  10. package/lib/rules/__tests__/consistent-test-it.test.js +921 -0
  11. package/lib/rules/__tests__/expect-expect.test.js +347 -0
  12. package/lib/rules/__tests__/fixtures/class.ts +13 -0
  13. package/lib/rules/__tests__/fixtures/file.ts +0 -0
  14. package/lib/rules/__tests__/fixtures/foo.ts +1 -0
  15. package/lib/rules/__tests__/fixtures/indent/indent-invalid-fixture-1.js +530 -0
  16. package/lib/rules/__tests__/fixtures/indent/indent-valid-fixture-1.js +530 -0
  17. package/lib/rules/__tests__/fixtures/react.tsx +0 -0
  18. package/lib/rules/__tests__/fixtures/tsconfig-withmeta.json +6 -0
  19. package/lib/rules/__tests__/fixtures/tsconfig.json +16 -0
  20. package/lib/rules/__tests__/fixtures/unstrict/file.ts +0 -0
  21. package/lib/rules/__tests__/fixtures/unstrict/react.tsx +0 -0
  22. package/lib/rules/__tests__/fixtures/unstrict/tsconfig.json +15 -0
  23. package/lib/rules/__tests__/max-expects.test.js +330 -0
  24. package/lib/rules/__tests__/max-nested-describe.test.js +247 -0
  25. package/lib/rules/__tests__/no-alias-methods.test.js +190 -0
  26. package/lib/rules/__tests__/no-commented-out-tests.test.js +213 -0
  27. package/lib/rules/__tests__/no-conditional-expect.test.js +696 -0
  28. package/lib/rules/__tests__/no-conditional-in-test.test.js +777 -0
  29. package/lib/rules/__tests__/no-deprecated-functions.test.js +119 -0
  30. package/lib/rules/__tests__/no-disabled-tests.test.js +241 -0
  31. package/lib/rules/__tests__/no-done-callback.test.js +424 -0
  32. package/lib/rules/__tests__/no-duplicate-hooks.test.js +469 -0
  33. package/lib/rules/__tests__/no-export.test.js +107 -0
  34. package/lib/rules/__tests__/no-focused-tests.test.js +373 -0
  35. package/lib/rules/__tests__/no-hooks.test.js +90 -0
  36. package/lib/rules/__tests__/no-identical-title.test.js +270 -0
  37. package/lib/rules/__tests__/no-if.test.js +787 -0
  38. package/lib/rules/__tests__/no-interpolation-in-snapshots.test.js +58 -0
  39. package/lib/rules/__tests__/no-jasmine-globals.test.js +206 -0
  40. package/lib/rules/__tests__/no-large-snapshots.test.js +237 -0
  41. package/lib/rules/__tests__/no-mocks-import.test.js +73 -0
  42. package/lib/rules/__tests__/no-restricted-jest-methods.test.js +103 -0
  43. package/lib/rules/__tests__/no-restricted-matchers.test.js +244 -0
  44. package/lib/rules/__tests__/no-standalone-expect.test.js +230 -0
  45. package/lib/rules/__tests__/no-test-prefixes.test.js +206 -0
  46. package/lib/rules/__tests__/no-test-return-statement.test.js +122 -0
  47. package/lib/rules/__tests__/no-untyped-mock-factory.test.js +149 -0
  48. package/lib/rules/__tests__/prefer-called-with.test.js +40 -0
  49. package/lib/rules/__tests__/prefer-comparison-matcher.test.js +200 -0
  50. package/lib/rules/__tests__/prefer-each.test.js +295 -0
  51. package/lib/rules/__tests__/prefer-equality-matcher.test.js +184 -0
  52. package/lib/rules/__tests__/prefer-expect-assertions.test.js +1437 -0
  53. package/lib/rules/__tests__/prefer-expect-resolves.test.js +96 -0
  54. package/lib/rules/__tests__/prefer-hooks-in-order.test.js +678 -0
  55. package/lib/rules/__tests__/prefer-hooks-on-top.test.js +218 -0
  56. package/lib/rules/__tests__/prefer-lowercase-title.test.js +619 -0
  57. package/lib/rules/__tests__/prefer-mock-promise-shorthand.test.js +360 -0
  58. package/lib/rules/__tests__/prefer-snapshot-hint.test.js +784 -0
  59. package/lib/rules/__tests__/prefer-spy-on.test.js +100 -0
  60. package/lib/rules/__tests__/prefer-strict-equal.test.js +46 -0
  61. package/lib/rules/__tests__/prefer-to-be.test.js +438 -0
  62. package/lib/rules/__tests__/prefer-to-contain.test.js +301 -0
  63. package/lib/rules/__tests__/prefer-to-have-length.test.js +99 -0
  64. package/lib/rules/__tests__/prefer-todo.test.js +78 -0
  65. package/lib/rules/__tests__/require-hook.test.js +403 -0
  66. package/lib/rules/__tests__/require-to-throw-message.test.js +108 -0
  67. package/lib/rules/__tests__/require-top-level-describe.test.js +236 -0
  68. package/lib/rules/__tests__/test-utils.js +11 -0
  69. package/lib/rules/__tests__/unbound-method.test.js +518 -0
  70. package/lib/rules/__tests__/valid-describe-callback.test.js +305 -0
  71. package/lib/rules/__tests__/valid-expect-in-promise.test.js +1583 -0
  72. package/lib/rules/__tests__/valid-expect.test.js +894 -0
  73. package/lib/rules/__tests__/valid-title.test.js +1147 -0
  74. package/lib/rules/utils/__tests__/detectJestVersion.test.js +221 -0
  75. package/lib/rules/utils/__tests__/parseJestFnCall.test.js +809 -0
  76. package/lib/rules/utils/accessors.js +4 -0
  77. package/lib/rules/utils/misc.js +36 -20
  78. package/lib/rules/valid-expect-in-promise.js +3 -3
  79. package/package.json +13 -9
@@ -0,0 +1,403 @@
1
+ "use strict";
2
+
3
+ var _utils = require("@typescript-eslint/utils");
4
+ var _dedent = _interopRequireDefault(require("dedent"));
5
+ var _requireHook = _interopRequireDefault(require("../require-hook"));
6
+ var _testUtils = require("./test-utils");
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+ const ruleTester = new _utils.TSESLint.RuleTester({
9
+ parser: _testUtils.espreeParser,
10
+ parserOptions: {
11
+ ecmaVersion: 2017
12
+ }
13
+ });
14
+ ruleTester.run('require-hook', _requireHook.default, {
15
+ valid: ['describe()', 'describe("just a title")', (0, _dedent.default)`
16
+ describe('a test', () =>
17
+ test('something', () => {
18
+ expect(true).toBe(true);
19
+ }));
20
+ `, (0, _dedent.default)`
21
+ test('it', () => {
22
+ //
23
+ });
24
+ `, (0, _dedent.default)`
25
+ const { myFn } = require('../functions');
26
+
27
+ test('myFn', () => {
28
+ expect(myFn()).toBe(1);
29
+ });
30
+ `, {
31
+ code: (0, _dedent.default)`
32
+ import { myFn } from '../functions';
33
+
34
+ test('myFn', () => {
35
+ expect(myFn()).toBe(1);
36
+ });
37
+ `,
38
+ parserOptions: {
39
+ sourceType: 'module'
40
+ }
41
+ }, (0, _dedent.default)`
42
+ class MockLogger {
43
+ log() {}
44
+ }
45
+
46
+ test('myFn', () => {
47
+ expect(myFn()).toBe(1);
48
+ });
49
+ `, (0, _dedent.default)`
50
+ const { myFn } = require('../functions');
51
+
52
+ describe('myFn', () => {
53
+ it('returns one', () => {
54
+ expect(myFn()).toBe(1);
55
+ });
56
+ });
57
+ `, (0, _dedent.default)`
58
+ describe('some tests', () => {
59
+ it('is true', () => {
60
+ expect(true).toBe(true);
61
+ });
62
+ });
63
+ `, (0, _dedent.default)`
64
+ describe('some tests', () => {
65
+ it('is true', () => {
66
+ expect(true).toBe(true);
67
+ });
68
+
69
+ describe('more tests', () => {
70
+ it('is false', () => {
71
+ expect(true).toBe(false);
72
+ });
73
+ });
74
+ });
75
+ `, (0, _dedent.default)`
76
+ describe('some tests', () => {
77
+ let consoleLogSpy;
78
+
79
+ beforeEach(() => {
80
+ consoleLogSpy = jest.spyOn(console, 'log');
81
+ });
82
+
83
+ it('prints a message', () => {
84
+ printMessage('hello world');
85
+
86
+ expect(consoleLogSpy).toHaveBeenCalledWith('hello world');
87
+ });
88
+ });
89
+ `, (0, _dedent.default)`
90
+ let consoleErrorSpy = null;
91
+
92
+ beforeEach(() => {
93
+ consoleErrorSpy = jest.spyOn(console, 'error');
94
+ });
95
+ `, (0, _dedent.default)`
96
+ let consoleErrorSpy = undefined;
97
+
98
+ beforeEach(() => {
99
+ consoleErrorSpy = jest.spyOn(console, 'error');
100
+ });
101
+ `, (0, _dedent.default)`
102
+ describe('some tests', () => {
103
+ beforeEach(() => {
104
+ setup();
105
+ });
106
+ });
107
+ `, (0, _dedent.default)`
108
+ beforeEach(() => {
109
+ initializeCityDatabase();
110
+ });
111
+
112
+ afterEach(() => {
113
+ clearCityDatabase();
114
+ });
115
+
116
+ test('city database has Vienna', () => {
117
+ expect(isCity('Vienna')).toBeTruthy();
118
+ });
119
+
120
+ test('city database has San Juan', () => {
121
+ expect(isCity('San Juan')).toBeTruthy();
122
+ });
123
+ `, (0, _dedent.default)`
124
+ describe('cities', () => {
125
+ beforeEach(() => {
126
+ initializeCityDatabase();
127
+ });
128
+
129
+ test('city database has Vienna', () => {
130
+ expect(isCity('Vienna')).toBeTruthy();
131
+ });
132
+
133
+ test('city database has San Juan', () => {
134
+ expect(isCity('San Juan')).toBeTruthy();
135
+ });
136
+
137
+ afterEach(() => {
138
+ clearCityDatabase();
139
+ });
140
+ });
141
+ `, {
142
+ code: (0, _dedent.default)`
143
+ enableAutoDestroy(afterEach);
144
+
145
+ describe('some tests', () => {
146
+ it('is false', () => {
147
+ expect(true).toBe(true);
148
+ });
149
+ });
150
+ `,
151
+ options: [{
152
+ allowedFunctionCalls: ['enableAutoDestroy']
153
+ }]
154
+ }],
155
+ invalid: [{
156
+ code: 'setup();',
157
+ errors: [{
158
+ messageId: 'useHook',
159
+ line: 1,
160
+ column: 1
161
+ }]
162
+ }, {
163
+ code: (0, _dedent.default)`
164
+ describe('some tests', () => {
165
+ setup();
166
+ });
167
+ `,
168
+ errors: [{
169
+ messageId: 'useHook',
170
+ line: 2,
171
+ column: 3
172
+ }]
173
+ }, {
174
+ code: (0, _dedent.default)`
175
+ let { setup } = require('./test-utils');
176
+
177
+ describe('some tests', () => {
178
+ setup();
179
+ });
180
+ `,
181
+ errors: [{
182
+ messageId: 'useHook',
183
+ line: 1,
184
+ column: 1
185
+ }, {
186
+ messageId: 'useHook',
187
+ line: 4,
188
+ column: 3
189
+ }]
190
+ }, {
191
+ code: (0, _dedent.default)`
192
+ describe('some tests', () => {
193
+ setup();
194
+
195
+ it('is true', () => {
196
+ expect(true).toBe(true);
197
+ });
198
+
199
+ describe('more tests', () => {
200
+ setup();
201
+
202
+ it('is false', () => {
203
+ expect(true).toBe(false);
204
+ });
205
+ });
206
+ });
207
+ `,
208
+ errors: [{
209
+ messageId: 'useHook',
210
+ line: 2,
211
+ column: 3
212
+ }, {
213
+ messageId: 'useHook',
214
+ line: 9,
215
+ column: 5
216
+ }]
217
+ }, {
218
+ code: (0, _dedent.default)`
219
+ let consoleErrorSpy = jest.spyOn(console, 'error');
220
+
221
+ describe('when loading cities from the api', () => {
222
+ let consoleWarnSpy = jest.spyOn(console, 'warn');
223
+ });
224
+ `,
225
+ errors: [{
226
+ messageId: 'useHook',
227
+ line: 1,
228
+ column: 1
229
+ }, {
230
+ messageId: 'useHook',
231
+ line: 4,
232
+ column: 3
233
+ }]
234
+ }, {
235
+ code: (0, _dedent.default)`
236
+ let consoleErrorSpy = null;
237
+
238
+ describe('when loading cities from the api', () => {
239
+ let consoleWarnSpy = jest.spyOn(console, 'warn');
240
+ });
241
+ `,
242
+ errors: [{
243
+ messageId: 'useHook',
244
+ line: 4,
245
+ column: 3
246
+ }]
247
+ }, {
248
+ code: 'let value = 1',
249
+ errors: [{
250
+ messageId: 'useHook',
251
+ line: 1,
252
+ column: 1
253
+ }]
254
+ }, {
255
+ code: "let consoleErrorSpy, consoleWarnSpy = jest.spyOn(console, 'error');",
256
+ errors: [{
257
+ messageId: 'useHook',
258
+ line: 1,
259
+ column: 1
260
+ }]
261
+ }, {
262
+ code: "let consoleErrorSpy = jest.spyOn(console, 'error'), consoleWarnSpy;",
263
+ errors: [{
264
+ messageId: 'useHook',
265
+ line: 1,
266
+ column: 1
267
+ }]
268
+ }, {
269
+ code: (0, _dedent.default)`
270
+ import { database, isCity } from '../database';
271
+ import { loadCities } from '../api';
272
+
273
+ jest.mock('../api');
274
+
275
+ const initializeCityDatabase = () => {
276
+ database.addCity('Vienna');
277
+ database.addCity('San Juan');
278
+ database.addCity('Wellington');
279
+ };
280
+
281
+ const clearCityDatabase = () => {
282
+ database.clear();
283
+ };
284
+
285
+ initializeCityDatabase();
286
+
287
+ test('that persists cities', () => {
288
+ expect(database.cities.length).toHaveLength(3);
289
+ });
290
+
291
+ test('city database has Vienna', () => {
292
+ expect(isCity('Vienna')).toBeTruthy();
293
+ });
294
+
295
+ test('city database has San Juan', () => {
296
+ expect(isCity('San Juan')).toBeTruthy();
297
+ });
298
+
299
+ describe('when loading cities from the api', () => {
300
+ let consoleWarnSpy = jest.spyOn(console, 'warn');
301
+
302
+ loadCities.mockResolvedValue(['Wellington', 'London']);
303
+
304
+ it('does not duplicate cities', async () => {
305
+ await database.loadCities();
306
+
307
+ expect(database.cities).toHaveLength(4);
308
+ });
309
+
310
+ it('logs any duplicates', async () => {
311
+ await database.loadCities();
312
+
313
+ expect(consoleWarnSpy).toHaveBeenCalledWith(
314
+ 'Ignored duplicate cities: Wellington',
315
+ );
316
+ });
317
+ });
318
+
319
+ clearCityDatabase();
320
+ `,
321
+ parserOptions: {
322
+ sourceType: 'module'
323
+ },
324
+ errors: [{
325
+ messageId: 'useHook',
326
+ line: 16,
327
+ column: 1
328
+ }, {
329
+ messageId: 'useHook',
330
+ line: 31,
331
+ column: 3
332
+ }, {
333
+ messageId: 'useHook',
334
+ line: 33,
335
+ column: 3
336
+ }, {
337
+ messageId: 'useHook',
338
+ line: 50,
339
+ column: 1
340
+ }]
341
+ }, {
342
+ code: (0, _dedent.default)`
343
+ enableAutoDestroy(afterEach);
344
+
345
+ describe('some tests', () => {
346
+ it('is false', () => {
347
+ expect(true).toBe(true);
348
+ });
349
+ });
350
+ `,
351
+ options: [{
352
+ allowedFunctionCalls: ['someOtherName']
353
+ }],
354
+ errors: [{
355
+ messageId: 'useHook',
356
+ line: 1,
357
+ column: 1
358
+ }]
359
+ }]
360
+ });
361
+ new _utils.TSESLint.RuleTester({
362
+ parser: require.resolve('@typescript-eslint/parser')
363
+ }).run('require-hook: typescript edition', _requireHook.default, {
364
+ valid: [(0, _dedent.default)`
365
+ import { myFn } from '../functions';
366
+
367
+ // todo: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56545
368
+ declare module 'eslint' {
369
+ namespace ESLint {
370
+ interface LintResult {
371
+ fatalErrorCount: number;
372
+ }
373
+ }
374
+ }
375
+
376
+ test('myFn', () => {
377
+ expect(myFn()).toBe(1);
378
+ });
379
+ `],
380
+ invalid: [{
381
+ code: (0, _dedent.default)`
382
+ import { setup } from '../test-utils';
383
+
384
+ // todo: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56545
385
+ declare module 'eslint' {
386
+ namespace ESLint {
387
+ interface LintResult {
388
+ fatalErrorCount: number;
389
+ }
390
+ }
391
+ }
392
+
393
+ describe('some tests', () => {
394
+ setup();
395
+ });
396
+ `,
397
+ errors: [{
398
+ messageId: 'useHook',
399
+ line: 13,
400
+ column: 3
401
+ }]
402
+ }]
403
+ });
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+
3
+ var _utils = require("@typescript-eslint/utils");
4
+ var _dedent = _interopRequireDefault(require("dedent"));
5
+ var _requireToThrowMessage = _interopRequireDefault(require("../require-to-throw-message"));
6
+ var _testUtils = require("./test-utils");
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+ const ruleTester = new _utils.TSESLint.RuleTester({
9
+ parser: _testUtils.espreeParser,
10
+ parserOptions: {
11
+ ecmaVersion: 2017
12
+ }
13
+ });
14
+ ruleTester.run('require-to-throw-message', _requireToThrowMessage.default, {
15
+ valid: [
16
+ // String
17
+ "expect(() => { throw new Error('a'); }).toThrow('a');", "expect(() => { throw new Error('a'); }).toThrowError('a');", (0, _dedent.default)`
18
+ test('string', async () => {
19
+ const throwErrorAsync = async () => { throw new Error('a') };
20
+ await expect(throwErrorAsync()).rejects.toThrow('a');
21
+ await expect(throwErrorAsync()).rejects.toThrowError('a');
22
+ })
23
+ `,
24
+ // Template literal
25
+ "const a = 'a'; expect(() => { throw new Error('a'); }).toThrow(`${a}`);", "const a = 'a'; expect(() => { throw new Error('a'); }).toThrowError(`${a}`);", (0, _dedent.default)`
26
+ test('Template literal', async () => {
27
+ const a = 'a';
28
+ const throwErrorAsync = async () => { throw new Error('a') };
29
+ await expect(throwErrorAsync()).rejects.toThrow(\`\${a}\`);
30
+ await expect(throwErrorAsync()).rejects.toThrowError(\`\${a}\`);
31
+ })
32
+ `,
33
+ // Regex
34
+ "expect(() => { throw new Error('a'); }).toThrow(/^a$/);", "expect(() => { throw new Error('a'); }).toThrowError(/^a$/);", (0, _dedent.default)`
35
+ test('Regex', async () => {
36
+ const throwErrorAsync = async () => { throw new Error('a') };
37
+ await expect(throwErrorAsync()).rejects.toThrow(/^a$/);
38
+ await expect(throwErrorAsync()).rejects.toThrowError(/^a$/);
39
+ })
40
+ `,
41
+ // Function
42
+ "expect(() => { throw new Error('a'); }).toThrow((() => { return 'a'; })());", "expect(() => { throw new Error('a'); }).toThrowError((() => { return 'a'; })());", (0, _dedent.default)`
43
+ test('Function', async () => {
44
+ const throwErrorAsync = async () => { throw new Error('a') };
45
+ const fn = () => { return 'a'; };
46
+ await expect(throwErrorAsync()).rejects.toThrow(fn());
47
+ await expect(throwErrorAsync()).rejects.toThrowError(fn());
48
+ })
49
+ `,
50
+ // Allow no message for `not`.
51
+ "expect(() => { throw new Error('a'); }).not.toThrow();", "expect(() => { throw new Error('a'); }).not.toThrowError();", (0, _dedent.default)`
52
+ test('Allow no message for "not"', async () => {
53
+ const throwErrorAsync = async () => { throw new Error('a') };
54
+ await expect(throwErrorAsync()).resolves.not.toThrow();
55
+ await expect(throwErrorAsync()).resolves.not.toThrowError();
56
+ })
57
+ `, 'expect(a);'],
58
+ invalid: [
59
+ // Empty toThrow
60
+ {
61
+ code: "expect(() => { throw new Error('a'); }).toThrow();",
62
+ errors: [{
63
+ messageId: 'addErrorMessage',
64
+ data: {
65
+ matcherName: 'toThrow'
66
+ },
67
+ column: 41,
68
+ line: 1
69
+ }]
70
+ },
71
+ // Empty toThrowError
72
+ {
73
+ code: "expect(() => { throw new Error('a'); }).toThrowError();",
74
+ errors: [{
75
+ messageId: 'addErrorMessage',
76
+ data: {
77
+ matcherName: 'toThrowError'
78
+ },
79
+ column: 41,
80
+ line: 1
81
+ }]
82
+ },
83
+ // Empty rejects.toThrow / rejects.toThrowError
84
+ {
85
+ code: (0, _dedent.default)`
86
+ test('empty rejects.toThrow', async () => {
87
+ const throwErrorAsync = async () => { throw new Error('a') };
88
+ await expect(throwErrorAsync()).rejects.toThrow();
89
+ await expect(throwErrorAsync()).rejects.toThrowError();
90
+ })
91
+ `,
92
+ errors: [{
93
+ messageId: 'addErrorMessage',
94
+ data: {
95
+ matcherName: 'toThrow'
96
+ },
97
+ column: 43,
98
+ line: 3
99
+ }, {
100
+ messageId: 'addErrorMessage',
101
+ data: {
102
+ matcherName: 'toThrowError'
103
+ },
104
+ column: 43,
105
+ line: 4
106
+ }]
107
+ }]
108
+ });