eslint-plugin-jest 27.2.1 → 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 (77) 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-large-snapshots.md +4 -0
  5. package/docs/rules/require-hook.md +2 -6
  6. package/docs/rules/valid-expect.md +2 -2
  7. package/lib/index.js +2 -0
  8. package/lib/processors/__tests__/snapshot-processor.test.js +36 -0
  9. package/lib/rules/__tests__/consistent-test-it.test.js +921 -0
  10. package/lib/rules/__tests__/expect-expect.test.js +347 -0
  11. package/lib/rules/__tests__/fixtures/class.ts +13 -0
  12. package/lib/rules/__tests__/fixtures/file.ts +0 -0
  13. package/lib/rules/__tests__/fixtures/foo.ts +1 -0
  14. package/lib/rules/__tests__/fixtures/indent/indent-invalid-fixture-1.js +530 -0
  15. package/lib/rules/__tests__/fixtures/indent/indent-valid-fixture-1.js +530 -0
  16. package/lib/rules/__tests__/fixtures/react.tsx +0 -0
  17. package/lib/rules/__tests__/fixtures/tsconfig-withmeta.json +6 -0
  18. package/lib/rules/__tests__/fixtures/tsconfig.json +16 -0
  19. package/lib/rules/__tests__/fixtures/unstrict/file.ts +0 -0
  20. package/lib/rules/__tests__/fixtures/unstrict/react.tsx +0 -0
  21. package/lib/rules/__tests__/fixtures/unstrict/tsconfig.json +15 -0
  22. package/lib/rules/__tests__/max-expects.test.js +330 -0
  23. package/lib/rules/__tests__/max-nested-describe.test.js +247 -0
  24. package/lib/rules/__tests__/no-alias-methods.test.js +190 -0
  25. package/lib/rules/__tests__/no-commented-out-tests.test.js +213 -0
  26. package/lib/rules/__tests__/no-conditional-expect.test.js +696 -0
  27. package/lib/rules/__tests__/no-conditional-in-test.test.js +777 -0
  28. package/lib/rules/__tests__/no-deprecated-functions.test.js +119 -0
  29. package/lib/rules/__tests__/no-disabled-tests.test.js +241 -0
  30. package/lib/rules/__tests__/no-done-callback.test.js +424 -0
  31. package/lib/rules/__tests__/no-duplicate-hooks.test.js +469 -0
  32. package/lib/rules/__tests__/no-export.test.js +107 -0
  33. package/lib/rules/__tests__/no-focused-tests.test.js +373 -0
  34. package/lib/rules/__tests__/no-hooks.test.js +90 -0
  35. package/lib/rules/__tests__/no-identical-title.test.js +270 -0
  36. package/lib/rules/__tests__/no-if.test.js +787 -0
  37. package/lib/rules/__tests__/no-interpolation-in-snapshots.test.js +58 -0
  38. package/lib/rules/__tests__/no-jasmine-globals.test.js +206 -0
  39. package/lib/rules/__tests__/no-large-snapshots.test.js +237 -0
  40. package/lib/rules/__tests__/no-mocks-import.test.js +73 -0
  41. package/lib/rules/__tests__/no-restricted-jest-methods.test.js +103 -0
  42. package/lib/rules/__tests__/no-restricted-matchers.test.js +244 -0
  43. package/lib/rules/__tests__/no-standalone-expect.test.js +230 -0
  44. package/lib/rules/__tests__/no-test-prefixes.test.js +206 -0
  45. package/lib/rules/__tests__/no-test-return-statement.test.js +122 -0
  46. package/lib/rules/__tests__/no-untyped-mock-factory.test.js +149 -0
  47. package/lib/rules/__tests__/prefer-called-with.test.js +40 -0
  48. package/lib/rules/__tests__/prefer-comparison-matcher.test.js +200 -0
  49. package/lib/rules/__tests__/prefer-each.test.js +295 -0
  50. package/lib/rules/__tests__/prefer-equality-matcher.test.js +184 -0
  51. package/lib/rules/__tests__/prefer-expect-assertions.test.js +1437 -0
  52. package/lib/rules/__tests__/prefer-expect-resolves.test.js +96 -0
  53. package/lib/rules/__tests__/prefer-hooks-in-order.test.js +678 -0
  54. package/lib/rules/__tests__/prefer-hooks-on-top.test.js +218 -0
  55. package/lib/rules/__tests__/prefer-lowercase-title.test.js +619 -0
  56. package/lib/rules/__tests__/prefer-mock-promise-shorthand.test.js +360 -0
  57. package/lib/rules/__tests__/prefer-snapshot-hint.test.js +784 -0
  58. package/lib/rules/__tests__/prefer-spy-on.test.js +100 -0
  59. package/lib/rules/__tests__/prefer-strict-equal.test.js +46 -0
  60. package/lib/rules/__tests__/prefer-to-be.test.js +438 -0
  61. package/lib/rules/__tests__/prefer-to-contain.test.js +301 -0
  62. package/lib/rules/__tests__/prefer-to-have-length.test.js +99 -0
  63. package/lib/rules/__tests__/prefer-todo.test.js +78 -0
  64. package/lib/rules/__tests__/require-hook.test.js +403 -0
  65. package/lib/rules/__tests__/require-to-throw-message.test.js +108 -0
  66. package/lib/rules/__tests__/require-top-level-describe.test.js +236 -0
  67. package/lib/rules/__tests__/test-utils.js +11 -0
  68. package/lib/rules/__tests__/unbound-method.test.js +518 -0
  69. package/lib/rules/__tests__/valid-describe-callback.test.js +305 -0
  70. package/lib/rules/__tests__/valid-expect-in-promise.test.js +1583 -0
  71. package/lib/rules/__tests__/valid-expect.test.js +894 -0
  72. package/lib/rules/__tests__/valid-title.test.js +1147 -0
  73. package/lib/rules/utils/__tests__/detectJestVersion.test.js +221 -0
  74. package/lib/rules/utils/__tests__/parseJestFnCall.test.js +809 -0
  75. package/lib/rules/utils/accessors.js +4 -0
  76. package/lib/rules/utils/misc.js +36 -20
  77. package/package.json +12 -8
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+
3
+ var _utils = require("@typescript-eslint/utils");
4
+ var _noDeprecatedFunctions = _interopRequireDefault(require("../no-deprecated-functions"));
5
+ var _detectJestVersion = require("../utils/detectJestVersion");
6
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
7
+ jest.mock('../utils/detectJestVersion');
8
+ const detectJestVersionMock = _detectJestVersion.detectJestVersion;
9
+ const ruleTester = new _utils.TSESLint.RuleTester();
10
+ const generateValidCases = (jestVersion, functionCall) => {
11
+ const [name, func] = functionCall.split('.');
12
+ const settings = {
13
+ jest: {
14
+ version: jestVersion
15
+ }
16
+ };
17
+ return [{
18
+ settings,
19
+ code: `${functionCall}()`
20
+ }, {
21
+ settings,
22
+ code: `${functionCall}`
23
+ }, {
24
+ settings,
25
+ code: `${name}['${func}']()`
26
+ }, {
27
+ settings,
28
+ code: `${name}['${func}']`
29
+ }];
30
+ };
31
+ const generateInvalidCases = (jestVersion, deprecation, replacement) => {
32
+ const [deprecatedName, deprecatedFunc] = deprecation.split('.');
33
+ const [replacementName, replacementFunc] = replacement.split('.');
34
+ const settings = {
35
+ jest: {
36
+ version: jestVersion
37
+ }
38
+ };
39
+ const errors = [{
40
+ messageId: 'deprecatedFunction',
41
+ data: {
42
+ deprecation,
43
+ replacement
44
+ }
45
+ }];
46
+ return [{
47
+ code: `${deprecation}()`,
48
+ output: `${replacement}()`,
49
+ settings,
50
+ errors
51
+ }, {
52
+ code: `${deprecatedName}['${deprecatedFunc}']()`,
53
+ output: `${replacementName}['${replacementFunc}']()`,
54
+ settings,
55
+ errors
56
+ }];
57
+ };
58
+
59
+ // contains the cache-clearing beforeEach so we can test the cache too
60
+ describe('the rule', () => {
61
+ // a few sanity checks before doing our massive loop
62
+ ruleTester.run('no-deprecated-functions', _noDeprecatedFunctions.default, {
63
+ valid: [{
64
+ code: 'jest',
65
+ settings: {
66
+ jest: {
67
+ version: 14
68
+ }
69
+ }
70
+ }, {
71
+ code: 'require("fs")',
72
+ settings: {
73
+ jest: {
74
+ version: 14
75
+ }
76
+ }
77
+ }, ...generateValidCases(14, 'jest.resetModuleRegistry'), ...generateValidCases(17, 'require.requireActual'), ...generateValidCases(25, 'jest.genMockFromModule'), ...generateValidCases('25.1.1', 'jest.genMockFromModule'), ...generateValidCases('17.2', 'require.requireActual')],
78
+ invalid: [...generateInvalidCases(21, 'jest.resetModuleRegistry', 'jest.resetModules'), ...generateInvalidCases(24, 'jest.addMatchers', 'expect.extend'), ...generateInvalidCases(26, 'jest.genMockFromModule', 'jest.createMockFromModule'), ...generateInvalidCases('26.0.0-next.11', 'jest.genMockFromModule', 'jest.createMockFromModule')]
79
+ });
80
+ describe.each([14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27])('when using jest version %i', jestVersion => {
81
+ beforeEach(async () => {
82
+ detectJestVersionMock.mockReturnValue(jestVersion);
83
+ });
84
+ const allowedFunctions = [];
85
+ const deprecations = [[15, 'jest.resetModuleRegistry', 'jest.resetModules'], [17, 'jest.addMatchers', 'expect.extend'], [21, 'require.requireMock', 'jest.requireMock'], [21, 'require.requireActual', 'jest.requireActual'], [22, 'jest.runTimersToTime', 'jest.advanceTimersByTime'], [26, 'jest.genMockFromModule', 'jest.createMockFromModule']].filter(deprecation => {
86
+ if (deprecation[0] > jestVersion) {
87
+ allowedFunctions.push(deprecation[1]);
88
+ return false;
89
+ }
90
+ return true;
91
+ });
92
+ ruleTester.run('explict jest version', _noDeprecatedFunctions.default, {
93
+ valid: ['jest', 'require("fs")', ...allowedFunctions.map(func => generateValidCases(jestVersion, func)).reduce((acc, arr) => acc.concat(arr), [])],
94
+ invalid: deprecations.map(([, deprecation, replacement]) => generateInvalidCases(jestVersion, deprecation, replacement)).reduce((acc, arr) => acc.concat(arr), [])
95
+ });
96
+ ruleTester.run('detected jest version', _noDeprecatedFunctions.default, {
97
+ valid: ['jest', 'require("fs")', ...allowedFunctions.map(func => generateValidCases(undefined, func)).reduce((acc, arr) => acc.concat(arr), [])],
98
+ invalid: deprecations.map(([, deprecation, replacement]) => generateInvalidCases(undefined, deprecation, replacement)).reduce((acc, arr) => acc.concat(arr), [])
99
+ });
100
+ });
101
+ describe('when there is an error in detecting the jest version', () => {
102
+ beforeEach(() => {
103
+ detectJestVersionMock.mockImplementation(() => {
104
+ throw new Error('oh noes!');
105
+ });
106
+ });
107
+ it('bubbles the error up', () => {
108
+ expect(() => {
109
+ const linter = new _utils.TSESLint.Linter();
110
+ linter.defineRule('no-deprecated-functions', _noDeprecatedFunctions.default);
111
+ linter.verify('jest.resetModuleRegistry()', {
112
+ rules: {
113
+ 'no-deprecated-functions': 'error'
114
+ }
115
+ });
116
+ }).toThrow('oh noes!');
117
+ });
118
+ });
119
+ });
@@ -0,0 +1,241 @@
1
+ "use strict";
2
+
3
+ var _utils = require("@typescript-eslint/utils");
4
+ var _dedent = _interopRequireDefault(require("dedent"));
5
+ var _noDisabledTests = _interopRequireDefault(require("../no-disabled-tests"));
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: 2015,
12
+ sourceType: 'module'
13
+ }
14
+ });
15
+ ruleTester.run('no-disabled-tests', _noDisabledTests.default, {
16
+ valid: ['describe("foo", function () {})', 'it("foo", function () {})', 'describe.only("foo", function () {})', 'it.only("foo", function () {})', 'it.each("foo", () => {})', 'it.concurrent("foo", function () {})', 'test("foo", function () {})', 'test.only("foo", function () {})', 'test.concurrent("foo", function () {})', 'describe[`${"skip"}`]("foo", function () {})', 'it.todo("fill this later")', 'var appliedSkip = describe.skip; appliedSkip.apply(describe)', 'var calledSkip = it.skip; calledSkip.call(it)', '({ f: function () {} }).f()', '(a || b).f()', 'itHappensToStartWithIt()', 'testSomething()', 'xitSomethingElse()', 'xitiViewMap()', (0, _dedent.default)`
17
+ import { pending } from "actions"
18
+
19
+ test("foo", () => {
20
+ expect(pending()).toEqual({})
21
+ })
22
+ `, (0, _dedent.default)`
23
+ const { pending } = require("actions")
24
+
25
+ test("foo", () => {
26
+ expect(pending()).toEqual({})
27
+ })
28
+ `, (0, _dedent.default)`
29
+ test("foo", () => {
30
+ const pending = getPending()
31
+ expect(pending()).toEqual({})
32
+ })
33
+ `, (0, _dedent.default)`
34
+ test("foo", () => {
35
+ expect(pending()).toEqual({})
36
+ })
37
+
38
+ function pending() {
39
+ return {}
40
+ }
41
+ `, {
42
+ code: (0, _dedent.default)`
43
+ import { test } from './test-utils';
44
+
45
+ test('something');
46
+ `,
47
+ parserOptions: {
48
+ sourceType: 'module'
49
+ }
50
+ }],
51
+ invalid: [{
52
+ code: 'describe.skip("foo", function () {})',
53
+ errors: [{
54
+ messageId: 'disabledSuite',
55
+ column: 1,
56
+ line: 1
57
+ }]
58
+ }, {
59
+ code: 'describe.skip.each([1, 2, 3])("%s", (a, b) => {});',
60
+ errors: [{
61
+ messageId: 'disabledSuite',
62
+ column: 1,
63
+ line: 1
64
+ }]
65
+ }, {
66
+ code: 'xdescribe.each([1, 2, 3])("%s", (a, b) => {});',
67
+ errors: [{
68
+ messageId: 'disabledSuite',
69
+ column: 1,
70
+ line: 1
71
+ }]
72
+ }, {
73
+ code: 'describe[`skip`]("foo", function () {})',
74
+ errors: [{
75
+ messageId: 'disabledSuite',
76
+ column: 1,
77
+ line: 1
78
+ }]
79
+ }, {
80
+ code: 'describe["skip"]("foo", function () {})',
81
+ errors: [{
82
+ messageId: 'disabledSuite',
83
+ column: 1,
84
+ line: 1
85
+ }]
86
+ }, {
87
+ code: 'it.skip("foo", function () {})',
88
+ errors: [{
89
+ messageId: 'disabledTest',
90
+ column: 1,
91
+ line: 1
92
+ }]
93
+ }, {
94
+ code: 'it["skip"]("foo", function () {})',
95
+ errors: [{
96
+ messageId: 'disabledTest',
97
+ column: 1,
98
+ line: 1
99
+ }]
100
+ }, {
101
+ code: 'test.skip("foo", function () {})',
102
+ errors: [{
103
+ messageId: 'disabledTest',
104
+ column: 1,
105
+ line: 1
106
+ }]
107
+ }, {
108
+ code: 'it.skip.each``("foo", function () {})',
109
+ errors: [{
110
+ messageId: 'disabledTest',
111
+ column: 1,
112
+ line: 1
113
+ }]
114
+ }, {
115
+ code: 'test.skip.each``("foo", function () {})',
116
+ errors: [{
117
+ messageId: 'disabledTest',
118
+ column: 1,
119
+ line: 1
120
+ }]
121
+ }, {
122
+ code: 'it.skip.each([])("foo", function () {})',
123
+ errors: [{
124
+ messageId: 'disabledTest',
125
+ column: 1,
126
+ line: 1
127
+ }]
128
+ }, {
129
+ code: 'test.skip.each([])("foo", function () {})',
130
+ errors: [{
131
+ messageId: 'disabledTest',
132
+ column: 1,
133
+ line: 1
134
+ }]
135
+ }, {
136
+ code: 'test["skip"]("foo", function () {})',
137
+ errors: [{
138
+ messageId: 'disabledTest',
139
+ column: 1,
140
+ line: 1
141
+ }]
142
+ }, {
143
+ code: 'xdescribe("foo", function () {})',
144
+ errors: [{
145
+ messageId: 'disabledSuite',
146
+ column: 1,
147
+ line: 1
148
+ }]
149
+ }, {
150
+ code: 'xit("foo", function () {})',
151
+ errors: [{
152
+ messageId: 'disabledTest',
153
+ column: 1,
154
+ line: 1
155
+ }]
156
+ }, {
157
+ code: 'xtest("foo", function () {})',
158
+ errors: [{
159
+ messageId: 'disabledTest',
160
+ column: 1,
161
+ line: 1
162
+ }]
163
+ }, {
164
+ code: 'xit.each``("foo", function () {})',
165
+ errors: [{
166
+ messageId: 'disabledTest',
167
+ column: 1,
168
+ line: 1
169
+ }]
170
+ }, {
171
+ code: 'xtest.each``("foo", function () {})',
172
+ errors: [{
173
+ messageId: 'disabledTest',
174
+ column: 1,
175
+ line: 1
176
+ }]
177
+ }, {
178
+ code: 'xit.each([])("foo", function () {})',
179
+ errors: [{
180
+ messageId: 'disabledTest',
181
+ column: 1,
182
+ line: 1
183
+ }]
184
+ }, {
185
+ code: 'xtest.each([])("foo", function () {})',
186
+ errors: [{
187
+ messageId: 'disabledTest',
188
+ column: 1,
189
+ line: 1
190
+ }]
191
+ }, {
192
+ code: 'it("has title but no callback")',
193
+ errors: [{
194
+ messageId: 'missingFunction',
195
+ column: 1,
196
+ line: 1
197
+ }]
198
+ }, {
199
+ code: 'test("has title but no callback")',
200
+ errors: [{
201
+ messageId: 'missingFunction',
202
+ column: 1,
203
+ line: 1
204
+ }]
205
+ }, {
206
+ code: 'it("contains a call to pending", function () { pending() })',
207
+ errors: [{
208
+ messageId: 'pendingTest',
209
+ column: 48,
210
+ line: 1
211
+ }]
212
+ }, {
213
+ code: 'pending();',
214
+ errors: [{
215
+ messageId: 'pending',
216
+ column: 1,
217
+ line: 1
218
+ }]
219
+ }, {
220
+ code: 'describe("contains a call to pending", function () { pending() })',
221
+ errors: [{
222
+ messageId: 'pendingSuite',
223
+ column: 54,
224
+ line: 1
225
+ }]
226
+ }, {
227
+ code: (0, _dedent.default)`
228
+ import { test } from '@jest/globals';
229
+
230
+ test('something');
231
+ `,
232
+ parserOptions: {
233
+ sourceType: 'module'
234
+ },
235
+ errors: [{
236
+ messageId: 'missingFunction',
237
+ column: 1,
238
+ line: 3
239
+ }]
240
+ }]
241
+ });