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,221 @@
1
+ "use strict";
2
+
3
+ var _child_process = require("child_process");
4
+ var fs = _interopRequireWildcard(require("fs"));
5
+ var os = _interopRequireWildcard(require("os"));
6
+ var path = _interopRequireWildcard(require("path"));
7
+ var _tsNode = require("ts-node");
8
+ var _detectJestVersion = require("../detectJestVersion");
9
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
10
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
11
+ const compileFnCode = pathToFn => {
12
+ const fnContents = fs.readFileSync(pathToFn, 'utf-8');
13
+ return (0, _tsNode.create)({
14
+ transpileOnly: true,
15
+ compilerOptions: {
16
+ sourceMap: false
17
+ }
18
+ }).compile(fnContents, pathToFn);
19
+ };
20
+ const compiledFn = compileFnCode(require.resolve('../detectJestVersion.ts'));
21
+ const relativePathToFn = 'eslint-plugin-jest/lib/rules/detectJestVersion.js';
22
+ const runNodeScript = (cwd, script) => {
23
+ return (0, _child_process.spawnSync)('node', ['-e', script.split('\n').join(' ')], {
24
+ cwd,
25
+ encoding: 'utf-8'
26
+ });
27
+ };
28
+ const runDetectJestVersion = cwd => {
29
+ return runNodeScript(cwd, `
30
+ try {
31
+ console.log(require('${relativePathToFn}').detectJestVersion());
32
+ } catch (error) {
33
+ console.error(error.message);
34
+ }
35
+ `);
36
+ };
37
+
38
+ /**
39
+ * Makes a new temp directory, prefixed with `eslint-plugin-jest-`
40
+ *
41
+ * @return {Promise<string>}
42
+ */
43
+ const makeTempDir = () => fs.mkdtempSync(path.join(os.tmpdir(), 'eslint-plugin-jest-'));
44
+ const setupFakeProject = structure => {
45
+ const tempDir = makeTempDir();
46
+ for (const [filePath, contents] of Object.entries(structure)) {
47
+ if (contents === null) {
48
+ fs.mkdirSync(path.join(tempDir, filePath), {
49
+ recursive: true
50
+ });
51
+ continue;
52
+ }
53
+ const folderPath = path.dirname(filePath);
54
+
55
+ // make the directory (recursively)
56
+ fs.mkdirSync(path.join(tempDir, folderPath), {
57
+ recursive: true
58
+ });
59
+ const finalContents = typeof contents === 'string' ? contents : JSON.stringify(contents);
60
+ fs.writeFileSync(path.join(tempDir, filePath), finalContents);
61
+ }
62
+ return tempDir;
63
+ };
64
+ describe('detectJestVersion', () => {
65
+ describe('basic tests', () => {
66
+ const packageJsonFactory = jest.fn();
67
+ beforeEach(() => {
68
+ jest.resetModules();
69
+ jest.doMock(require.resolve('jest/package.json'), packageJsonFactory);
70
+ });
71
+ describe('when the package.json is missing the version property', () => {
72
+ it('throws an error', () => {
73
+ packageJsonFactory.mockReturnValue({});
74
+ expect(() => (0, _detectJestVersion.detectJestVersion)()).toThrow(/Unable to detect Jest version/iu);
75
+ });
76
+ });
77
+ it('caches versions', () => {
78
+ packageJsonFactory.mockReturnValue({
79
+ version: '1.2.3'
80
+ });
81
+ const version = (0, _detectJestVersion.detectJestVersion)();
82
+ jest.resetModules();
83
+ expect(_detectJestVersion.detectJestVersion).not.toThrow();
84
+ expect((0, _detectJestVersion.detectJestVersion)()).toBe(version);
85
+ });
86
+ });
87
+ describe('when in a simple project', () => {
88
+ it('finds the correct version', () => {
89
+ const projectDir = setupFakeProject({
90
+ 'package.json': {
91
+ name: 'simple-project'
92
+ },
93
+ [`node_modules/${relativePathToFn}`]: compiledFn,
94
+ 'node_modules/jest/package.json': {
95
+ name: 'jest',
96
+ version: '21.0.0'
97
+ }
98
+ });
99
+ const {
100
+ stdout,
101
+ stderr
102
+ } = runDetectJestVersion(projectDir);
103
+ expect(stdout.trim()).toBe('21');
104
+ expect(stderr.trim()).toBe('');
105
+ });
106
+ });
107
+ describe('when in a hoisted mono-repo', () => {
108
+ it('finds the correct version', () => {
109
+ const projectDir = setupFakeProject({
110
+ 'package.json': {
111
+ name: 'mono-repo'
112
+ },
113
+ [`node_modules/${relativePathToFn}`]: compiledFn,
114
+ 'node_modules/jest/package.json': {
115
+ name: 'jest',
116
+ version: '19.0.0'
117
+ },
118
+ 'packages/a/package.json': {
119
+ name: 'package-a'
120
+ },
121
+ 'packages/b/package.json': {
122
+ name: 'package-b'
123
+ }
124
+ });
125
+ const {
126
+ stdout,
127
+ stderr
128
+ } = runDetectJestVersion(projectDir);
129
+ expect(stdout.trim()).toBe('19');
130
+ expect(stderr.trim()).toBe('');
131
+ });
132
+ });
133
+ describe('when in a subproject', () => {
134
+ it('finds the correct versions', () => {
135
+ const projectDir = setupFakeProject({
136
+ 'backend/package.json': {
137
+ name: 'package-a'
138
+ },
139
+ [`backend/node_modules/${relativePathToFn}`]: compiledFn,
140
+ 'backend/node_modules/jest/package.json': {
141
+ name: 'jest',
142
+ version: '24.0.0'
143
+ },
144
+ 'frontend/package.json': {
145
+ name: 'package-b'
146
+ },
147
+ [`frontend/node_modules/${relativePathToFn}`]: compiledFn,
148
+ 'frontend/node_modules/jest/package.json': {
149
+ name: 'jest',
150
+ version: '15.0.0'
151
+ }
152
+ });
153
+ const {
154
+ stdout: stdoutBackend,
155
+ stderr: stderrBackend
156
+ } = runDetectJestVersion(path.join(projectDir, 'backend'));
157
+ expect(stdoutBackend.trim()).toBe('24');
158
+ expect(stderrBackend.trim()).toBe('');
159
+ const {
160
+ stdout: stdoutFrontend,
161
+ stderr: stderrFrontend
162
+ } = runDetectJestVersion(path.join(projectDir, 'frontend'));
163
+ expect(stdoutFrontend.trim()).toBe('15');
164
+ expect(stderrFrontend.trim()).toBe('');
165
+ });
166
+ });
167
+ describe('when jest is not installed', () => {
168
+ it('throws an error', () => {
169
+ const projectDir = setupFakeProject({
170
+ 'package.json': {
171
+ name: 'no-jest'
172
+ },
173
+ [`node_modules/${relativePathToFn}`]: compiledFn,
174
+ 'node_modules/pack/package.json': {
175
+ name: 'pack'
176
+ }
177
+ });
178
+ const {
179
+ stdout,
180
+ stderr
181
+ } = runDetectJestVersion(projectDir);
182
+ expect(stdout.trim()).toBe('');
183
+ expect(stderr.trim()).toContain('Unable to detect Jest version');
184
+ });
185
+ });
186
+ describe('when jest is changed on disk', () => {
187
+ it('uses the cached version', () => {
188
+ const projectDir = setupFakeProject({
189
+ 'package.json': {
190
+ name: 'no-jest'
191
+ },
192
+ [`node_modules/${relativePathToFn}`]: compiledFn,
193
+ 'node_modules/jest/package.json': {
194
+ name: 'jest',
195
+ version: '26.0.0'
196
+ }
197
+ });
198
+ const {
199
+ stdout,
200
+ stderr
201
+ } = runNodeScript(projectDir, `
202
+ const { detectJestVersion } = require('${relativePathToFn}');
203
+ const fs = require('fs');
204
+
205
+ console.log(detectJestVersion());
206
+ fs.writeFileSync(
207
+ 'node_modules/jest/package.json',
208
+ JSON.stringify({
209
+ name: 'jest',
210
+ version: '25.0.0',
211
+ }),
212
+ );
213
+ console.log(detectJestVersion());
214
+ `);
215
+ const [firstCall, secondCall] = stdout.split('\n');
216
+ expect(firstCall).toBe('26');
217
+ expect(secondCall).toBe('26');
218
+ expect(stderr.trim()).toBe('');
219
+ });
220
+ });
221
+ });