eslint-plugin-jest 27.2.2 → 27.3.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 (77) hide show
  1. package/README.md +1 -0
  2. package/docs/rules/no-confusing-set-timeout.md +62 -0
  3. package/lib/rules/expect-expect.js +3 -1
  4. package/lib/rules/no-confusing-set-timeout.js +65 -0
  5. package/lib/rules/no-deprecated-functions.js +2 -2
  6. package/lib/rules/no-test-return-statement.js +9 -3
  7. package/lib/rules/no-untyped-mock-factory.js +3 -1
  8. package/lib/rules/prefer-spy-on.js +6 -2
  9. package/lib/rules/unbound-method.js +1 -1
  10. package/package.json +13 -16
  11. package/lib/processors/__tests__/snapshot-processor.test.js +0 -36
  12. package/lib/rules/__tests__/consistent-test-it.test.js +0 -921
  13. package/lib/rules/__tests__/expect-expect.test.js +0 -347
  14. package/lib/rules/__tests__/fixtures/class.ts +0 -13
  15. package/lib/rules/__tests__/fixtures/file.ts +0 -0
  16. package/lib/rules/__tests__/fixtures/foo.ts +0 -1
  17. package/lib/rules/__tests__/fixtures/indent/indent-invalid-fixture-1.js +0 -530
  18. package/lib/rules/__tests__/fixtures/indent/indent-valid-fixture-1.js +0 -530
  19. package/lib/rules/__tests__/fixtures/react.tsx +0 -0
  20. package/lib/rules/__tests__/fixtures/tsconfig-withmeta.json +0 -6
  21. package/lib/rules/__tests__/fixtures/tsconfig.json +0 -16
  22. package/lib/rules/__tests__/fixtures/unstrict/file.ts +0 -0
  23. package/lib/rules/__tests__/fixtures/unstrict/react.tsx +0 -0
  24. package/lib/rules/__tests__/fixtures/unstrict/tsconfig.json +0 -15
  25. package/lib/rules/__tests__/max-expects.test.js +0 -330
  26. package/lib/rules/__tests__/max-nested-describe.test.js +0 -247
  27. package/lib/rules/__tests__/no-alias-methods.test.js +0 -190
  28. package/lib/rules/__tests__/no-commented-out-tests.test.js +0 -213
  29. package/lib/rules/__tests__/no-conditional-expect.test.js +0 -696
  30. package/lib/rules/__tests__/no-conditional-in-test.test.js +0 -777
  31. package/lib/rules/__tests__/no-deprecated-functions.test.js +0 -119
  32. package/lib/rules/__tests__/no-disabled-tests.test.js +0 -241
  33. package/lib/rules/__tests__/no-done-callback.test.js +0 -424
  34. package/lib/rules/__tests__/no-duplicate-hooks.test.js +0 -469
  35. package/lib/rules/__tests__/no-export.test.js +0 -107
  36. package/lib/rules/__tests__/no-focused-tests.test.js +0 -373
  37. package/lib/rules/__tests__/no-hooks.test.js +0 -90
  38. package/lib/rules/__tests__/no-identical-title.test.js +0 -270
  39. package/lib/rules/__tests__/no-if.test.js +0 -787
  40. package/lib/rules/__tests__/no-interpolation-in-snapshots.test.js +0 -58
  41. package/lib/rules/__tests__/no-jasmine-globals.test.js +0 -206
  42. package/lib/rules/__tests__/no-large-snapshots.test.js +0 -237
  43. package/lib/rules/__tests__/no-mocks-import.test.js +0 -73
  44. package/lib/rules/__tests__/no-restricted-jest-methods.test.js +0 -103
  45. package/lib/rules/__tests__/no-restricted-matchers.test.js +0 -244
  46. package/lib/rules/__tests__/no-standalone-expect.test.js +0 -230
  47. package/lib/rules/__tests__/no-test-prefixes.test.js +0 -206
  48. package/lib/rules/__tests__/no-test-return-statement.test.js +0 -122
  49. package/lib/rules/__tests__/no-untyped-mock-factory.test.js +0 -149
  50. package/lib/rules/__tests__/prefer-called-with.test.js +0 -40
  51. package/lib/rules/__tests__/prefer-comparison-matcher.test.js +0 -200
  52. package/lib/rules/__tests__/prefer-each.test.js +0 -295
  53. package/lib/rules/__tests__/prefer-equality-matcher.test.js +0 -184
  54. package/lib/rules/__tests__/prefer-expect-assertions.test.js +0 -1437
  55. package/lib/rules/__tests__/prefer-expect-resolves.test.js +0 -96
  56. package/lib/rules/__tests__/prefer-hooks-in-order.test.js +0 -678
  57. package/lib/rules/__tests__/prefer-hooks-on-top.test.js +0 -218
  58. package/lib/rules/__tests__/prefer-lowercase-title.test.js +0 -619
  59. package/lib/rules/__tests__/prefer-mock-promise-shorthand.test.js +0 -360
  60. package/lib/rules/__tests__/prefer-snapshot-hint.test.js +0 -784
  61. package/lib/rules/__tests__/prefer-spy-on.test.js +0 -100
  62. package/lib/rules/__tests__/prefer-strict-equal.test.js +0 -46
  63. package/lib/rules/__tests__/prefer-to-be.test.js +0 -438
  64. package/lib/rules/__tests__/prefer-to-contain.test.js +0 -301
  65. package/lib/rules/__tests__/prefer-to-have-length.test.js +0 -99
  66. package/lib/rules/__tests__/prefer-todo.test.js +0 -78
  67. package/lib/rules/__tests__/require-hook.test.js +0 -403
  68. package/lib/rules/__tests__/require-to-throw-message.test.js +0 -108
  69. package/lib/rules/__tests__/require-top-level-describe.test.js +0 -236
  70. package/lib/rules/__tests__/test-utils.js +0 -11
  71. package/lib/rules/__tests__/unbound-method.test.js +0 -518
  72. package/lib/rules/__tests__/valid-describe-callback.test.js +0 -305
  73. package/lib/rules/__tests__/valid-expect-in-promise.test.js +0 -1583
  74. package/lib/rules/__tests__/valid-expect.test.js +0 -894
  75. package/lib/rules/__tests__/valid-title.test.js +0 -1147
  76. package/lib/rules/utils/__tests__/detectJestVersion.test.js +0 -221
  77. package/lib/rules/utils/__tests__/parseJestFnCall.test.js +0 -809
package/README.md CHANGED
@@ -220,6 +220,7 @@ set to warn in.\
220
220
  | [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | | ✅ | | | |
221
221
  | [no-conditional-expect](docs/rules/no-conditional-expect.md) | Disallow calling `expect` conditionally | ✅ | | | | |
222
222
  | [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests | | | | | |
223
+ | [no-confusing-set-timeout](docs/rules/no-confusing-set-timeout.md) | Disallow confusing usages of jest.setTimeout | | | | | |
223
224
  | [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ✅ | | 🔧 | | |
224
225
  | [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | | ✅ | | | |
225
226
  | [no-done-callback](docs/rules/no-done-callback.md) | Disallow using a callback in asynchronous tests and hooks | ✅ | | | 💡 | |
@@ -0,0 +1,62 @@
1
+ # Disallow confusing usages of jest.setTimeout (`no-confusing-set-timeout`)
2
+
3
+ <!-- end auto-generated rule header -->
4
+
5
+ While `jest.setTimeout` can be called multiple times anywhere within a single
6
+ test file only the last call before any test functions run will have an effect.
7
+
8
+ ## Rule details
9
+
10
+ this rule checks for several confusing usages of `jest.setTimeout` that looks
11
+ like it applies to specific tests within the same file, such as:
12
+
13
+ - being called anywhere other than in global scope
14
+ - being called multiple times
15
+ - being called after other Jest functions like hooks, `describe`, `test`, or
16
+ `it`
17
+
18
+ Examples of **incorrect** code for this rule:
19
+
20
+ ```js
21
+ describe('test foo', () => {
22
+ jest.setTimeout(1000);
23
+ it('test-description', () => {
24
+ // test logic;
25
+ });
26
+ });
27
+
28
+ describe('test bar', () => {
29
+ it('test-description', () => {
30
+ jest.setTimeout(1000);
31
+ // test logic;
32
+ });
33
+ });
34
+
35
+ test('foo-bar', () => {
36
+ jest.setTimeout(1000);
37
+ });
38
+
39
+ describe('unit test', () => {
40
+ beforeEach(() => {
41
+ jest.setTimeout(1000);
42
+ });
43
+ });
44
+ ```
45
+
46
+ Examples of **correct** code for this rule:
47
+
48
+ ```js
49
+ jest.setTimeout(500);
50
+ test('test test', () => {
51
+ // do some stuff
52
+ });
53
+ ```
54
+
55
+ ```js
56
+ jest.setTimeout(1000);
57
+ describe('test bar bar', () => {
58
+ it('test-description', () => {
59
+ // test logic;
60
+ });
61
+ });
62
+ ```
@@ -20,7 +20,9 @@ var _utils2 = require("./utils");
20
20
  */
21
21
  function matchesAssertFunctionName(nodeName, patterns) {
22
22
  return patterns.some(p => new RegExp(`^${p.split('.').map(x => {
23
- if (x === '**') return '[a-z\\d\\.]*';
23
+ if (x === '**') {
24
+ return '[a-z\\d\\.]*';
25
+ }
24
26
  return x.replace(/\*/gu, '[a-z\\d]*');
25
27
  }).join('\\.')}(\\.|$)`, 'ui').test(nodeName));
26
28
  }
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _utils = require("./utils");
8
+ function isJestSetTimeout(jestFnCall) {
9
+ return jestFnCall.type === 'jest' && jestFnCall.members.length === 1 && (0, _utils.isIdentifier)(jestFnCall.members[0], 'setTimeout');
10
+ }
11
+ var _default = (0, _utils.createRule)({
12
+ name: __filename,
13
+ meta: {
14
+ docs: {
15
+ category: 'Best Practices',
16
+ description: 'Disallow confusing usages of jest.setTimeout',
17
+ recommended: false
18
+ },
19
+ messages: {
20
+ globalSetTimeout: '`jest.setTimeout` should be call in `global` scope',
21
+ multipleSetTimeouts: 'Do not call `jest.setTimeout` multiple times, as only the last call will have an effect',
22
+ orderSetTimeout: '`jest.setTimeout` should be placed before any other jest methods'
23
+ },
24
+ type: 'problem',
25
+ schema: []
26
+ },
27
+ defaultOptions: [],
28
+ create(context) {
29
+ let seenJestTimeout = false;
30
+ let shouldEmitOrderSetTimeout = false;
31
+ return {
32
+ CallExpression(node) {
33
+ const scope = context.getScope();
34
+ const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
35
+ if (!jestFnCall) {
36
+ return;
37
+ }
38
+ if (!isJestSetTimeout(jestFnCall)) {
39
+ shouldEmitOrderSetTimeout = true;
40
+ return;
41
+ }
42
+ if (!['global', 'module'].includes(scope.type)) {
43
+ context.report({
44
+ messageId: 'globalSetTimeout',
45
+ node
46
+ });
47
+ }
48
+ if (shouldEmitOrderSetTimeout) {
49
+ context.report({
50
+ messageId: 'orderSetTimeout',
51
+ node
52
+ });
53
+ }
54
+ if (seenJestTimeout) {
55
+ context.report({
56
+ messageId: 'multipleSetTimeouts',
57
+ node
58
+ });
59
+ }
60
+ seenJestTimeout = true;
61
+ }
62
+ };
63
+ }
64
+ });
65
+ exports.default = _default;
@@ -30,8 +30,8 @@ var _default = (0, _utils2.createRule)({
30
30
  },
31
31
  defaultOptions: [],
32
32
  create(context) {
33
- var _context$settings, _context$settings$jes;
34
- const jestVersion = parseJestVersion(((_context$settings = context.settings) === null || _context$settings === void 0 ? void 0 : (_context$settings$jes = _context$settings.jest) === null || _context$settings$jes === void 0 ? void 0 : _context$settings$jes.version) || (0, _utils2.detectJestVersion)());
33
+ var _context$settings;
34
+ const jestVersion = parseJestVersion(((_context$settings = context.settings) === null || _context$settings === void 0 || (_context$settings = _context$settings.jest) === null || _context$settings === void 0 ? void 0 : _context$settings.version) || (0, _utils2.detectJestVersion)());
35
35
  const deprecations = {
36
36
  ...(jestVersion >= 15 && {
37
37
  'jest.resetModuleRegistry': 'jest.resetModules'
@@ -36,7 +36,9 @@ var _default = (0, _utils2.createRule)({
36
36
  }
37
37
  const body = getBody(node.arguments);
38
38
  const returnStmt = body.find(t => t.type === _utils.AST_NODE_TYPES.ReturnStatement);
39
- if (!returnStmt) return;
39
+ if (!returnStmt) {
40
+ return;
41
+ }
40
42
  context.report({
41
43
  messageId: 'noReturnValue',
42
44
  node: returnStmt
@@ -45,9 +47,13 @@ var _default = (0, _utils2.createRule)({
45
47
  FunctionDeclaration(node) {
46
48
  const declaredVariables = context.getDeclaredVariables(node);
47
49
  const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context);
48
- if (testCallExpressions.length === 0) return;
50
+ if (testCallExpressions.length === 0) {
51
+ return;
52
+ }
49
53
  const returnStmt = node.body.body.find(t => t.type === _utils.AST_NODE_TYPES.ReturnStatement);
50
- if (!returnStmt) return;
54
+ if (!returnStmt) {
55
+ return;
56
+ }
51
57
  context.report({
52
58
  messageId: 'noReturnValue',
53
59
  node: returnStmt
@@ -56,7 +56,9 @@ var _default = (0, _utils2.createRule)({
56
56
  },
57
57
  node,
58
58
  fix(fixer) {
59
- if (!moduleName) return [];
59
+ if (!moduleName) {
60
+ return [];
61
+ }
60
62
  return [fixer.insertTextAfter(callee, `<typeof import(${moduleName.raw})>`)];
61
63
  }
62
64
  });
@@ -61,9 +61,13 @@ var _default = (0, _utils2.createRule)({
61
61
  left,
62
62
  right
63
63
  } = node;
64
- if (left.type !== _utils.AST_NODE_TYPES.MemberExpression) return;
64
+ if (left.type !== _utils.AST_NODE_TYPES.MemberExpression) {
65
+ return;
66
+ }
65
67
  const jestFnCall = getJestFnCall(right);
66
- if (!jestFnCall) return;
68
+ if (!jestFnCall) {
69
+ return;
70
+ }
67
71
  context.report({
68
72
  node,
69
73
  messageId: 'useJestSpyOn',
@@ -63,7 +63,7 @@ var _default = (0, _utils2.createRule)({
63
63
  }
64
64
  }
65
65
  }
66
- (_baseSelectors$Member = baseSelectors.MemberExpression) === null || _baseSelectors$Member === void 0 ? void 0 : _baseSelectors$Member.call(baseSelectors, node);
66
+ (_baseSelectors$Member = baseSelectors.MemberExpression) === null || _baseSelectors$Member === void 0 || _baseSelectors$Member.call(baseSelectors, node);
67
67
  }
68
68
  };
69
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "27.2.2",
3
+ "version": "27.3.0",
4
4
  "description": "ESLint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",
@@ -14,13 +14,13 @@
14
14
  "email": "hello@jkimbo.com",
15
15
  "url": "jkimbo.com"
16
16
  },
17
- "main": "lib/",
17
+ "main": "lib/index.js",
18
18
  "files": [
19
19
  "docs/",
20
20
  "lib/"
21
21
  ],
22
22
  "scripts": {
23
- "build": "babel --extensions .js,.ts src --out-dir lib --copy-files && rimraf lib/__tests__ 'lib/**/__tests__'",
23
+ "build": "babel --extensions .js,.ts src --out-dir lib --copy-files && rimraf --glob lib/__tests__ 'lib/**/__tests__'",
24
24
  "_postinstall": "is-ci || husky install",
25
25
  "lint": "eslint . --ignore-pattern '!.eslintrc.js' --ext js,ts",
26
26
  "prepack": "rimraf lib && yarn build",
@@ -45,8 +45,7 @@
45
45
  "arrowParens": "avoid",
46
46
  "endOfLine": "auto",
47
47
  "proseWrap": "always",
48
- "singleQuote": true,
49
- "trailingComma": "all"
48
+ "singleQuote": true
50
49
  },
51
50
  "release": {
52
51
  "branches": [
@@ -105,28 +104,26 @@
105
104
  "@babel/preset-typescript": "^7.3.3",
106
105
  "@commitlint/cli": "^17.0.3",
107
106
  "@commitlint/config-conventional": "^17.0.3",
108
- "@schemastore/package": "^0.0.8",
107
+ "@schemastore/package": "^0.0.10",
109
108
  "@semantic-release/changelog": "^6.0.0",
110
109
  "@semantic-release/git": "^10.0.0",
111
- "@tsconfig/node14": "^1.0.3",
112
- "@types/dedent": "^0.7.0",
110
+ "@tsconfig/node14": "^14.1.0",
113
111
  "@types/eslint": "^8.4.6",
114
112
  "@types/jest": "^29.0.0",
115
113
  "@types/node": "^14.18.26",
116
- "@types/prettier": "^2.0.0",
117
114
  "@typescript-eslint/eslint-plugin": "^5.0.0",
118
115
  "@typescript-eslint/parser": "^5.0.0",
119
116
  "babel-jest": "^29.0.0",
120
117
  "babel-plugin-replace-ts-export-assignment": "^0.0.2",
121
- "dedent": "^0.7.0",
122
- "eslint": "^6.0.0 || ^7.0.0 || ^8.0.0",
123
- "eslint-config-prettier": "^8.3.0",
118
+ "dedent": "^1.5.0",
119
+ "eslint": "^7.0.0 || ^8.0.0",
120
+ "eslint-config-prettier": "^9.0.0",
124
121
  "eslint-doc-generator": "^1.0.0",
125
122
  "eslint-plugin-eslint-comments": "^3.1.2",
126
123
  "eslint-plugin-eslint-plugin": "^5.0.6",
127
124
  "eslint-plugin-import": "^2.25.1",
128
125
  "eslint-plugin-node": "^11.0.0",
129
- "eslint-plugin-prettier": "^4.2.1",
126
+ "eslint-plugin-prettier": "^5.0.0",
130
127
  "eslint-remote-tester": "^3.0.0",
131
128
  "eslint-remote-tester-repositories": "~1.0.0",
132
129
  "husky": "^8.0.1",
@@ -136,7 +133,7 @@
136
133
  "lint-staged": "^13.0.3",
137
134
  "markdown-link-check": "^3.10.2",
138
135
  "pinst": "^3.0.0",
139
- "prettier": "^2.0.5",
136
+ "prettier": "^3.0.0",
140
137
  "rimraf": "^5.0.0",
141
138
  "semantic-release": "^21.0.0",
142
139
  "semver": "^7.3.5",
@@ -144,7 +141,7 @@
144
141
  "typescript": "^5.0.4"
145
142
  },
146
143
  "peerDependencies": {
147
- "@typescript-eslint/eslint-plugin": "^5.0.0",
144
+ "@typescript-eslint/eslint-plugin": "^5.0.0 || ^6.0.0",
148
145
  "eslint": "^7.0.0 || ^8.0.0",
149
146
  "jest": "*"
150
147
  },
@@ -156,7 +153,7 @@
156
153
  "optional": true
157
154
  }
158
155
  },
159
- "packageManager": "yarn@3.6.0",
156
+ "packageManager": "yarn@3.6.3",
160
157
  "engines": {
161
158
  "node": "^14.15.0 || ^16.10.0 || >=18.0.0"
162
159
  },
@@ -1,36 +0,0 @@
1
- "use strict";
2
-
3
- var snapshotProcessor = _interopRequireWildcard(require("../snapshot-processor"));
4
- 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); }
5
- 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; }
6
- describe('snapshot-processor', () => {
7
- it('exports an object with preprocess and postprocess functions', () => {
8
- expect(snapshotProcessor).toMatchObject({
9
- preprocess: expect.any(Function),
10
- postprocess: expect.any(Function)
11
- });
12
- });
13
- describe('preprocess function', () => {
14
- it('should pass on untouched source code to source array', () => {
15
- const {
16
- preprocess
17
- } = snapshotProcessor;
18
- const sourceCode = "const name = 'johnny bravo';";
19
- const result = preprocess(sourceCode);
20
- expect(result).toEqual([sourceCode]);
21
- });
22
- });
23
- describe('postprocess function', () => {
24
- it('should only return messages about snapshot specific rules', () => {
25
- const {
26
- postprocess
27
- } = snapshotProcessor;
28
- const result = postprocess([['no-console', 'global-require', 'jest/no-large-snapshots'].map(ruleId => ({
29
- ruleId
30
- }))]);
31
- expect(result).toEqual([{
32
- ruleId: 'jest/no-large-snapshots'
33
- }]);
34
- });
35
- });
36
- });