eslint-plugin-jest 26.2.0 → 26.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.
package/README.md CHANGED
@@ -209,6 +209,7 @@ installations requiring long-term consistency.
209
209
  | [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in equality matchers | | ![suggest][] |
210
210
  | [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
211
211
  | [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
212
+ | [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | Prefer having hooks in a consistent order | | |
212
213
  | [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
213
214
  | [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | ![fixable][] |
214
215
  | [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | |
@@ -0,0 +1,133 @@
1
+ # Prefer having hooks in a consistent order (`prefer-hooks-in-order`)
2
+
3
+ While hooks can be setup in any order, they're always called by `jest` in this
4
+ specific order:
5
+
6
+ 1. `beforeAll`
7
+ 1. `beforeEach`
8
+ 1. `afterEach`
9
+ 1. `afterAll`
10
+
11
+ This rule aims to make that more obvious by enforcing grouped hooks be setup in
12
+ that order within tests.
13
+
14
+ ## Rule Details
15
+
16
+ Examples of **incorrect** code for this rule
17
+
18
+ ```js
19
+ /* eslint jest/prefer-hooks-in-order: "error" */
20
+
21
+ describe('foo', () => {
22
+ beforeEach(() => {
23
+ seedMyDatabase();
24
+ });
25
+
26
+ beforeAll(() => {
27
+ createMyDatabase();
28
+ });
29
+
30
+ it('accepts this input', () => {
31
+ // ...
32
+ });
33
+
34
+ it('returns that value', () => {
35
+ // ...
36
+ });
37
+
38
+ describe('when the database has specific values', () => {
39
+ const specificValue = '...';
40
+
41
+ beforeEach(() => {
42
+ seedMyDatabase(specificValue);
43
+ });
44
+
45
+ it('accepts that input', () => {
46
+ // ...
47
+ });
48
+
49
+ it('throws an error', () => {
50
+ // ...
51
+ });
52
+
53
+ afterEach(() => {
54
+ clearLogger();
55
+ });
56
+ beforeEach(() => {
57
+ mockLogger();
58
+ });
59
+
60
+ it('logs a message', () => {
61
+ // ...
62
+ });
63
+ });
64
+
65
+ afterAll(() => {
66
+ removeMyDatabase();
67
+ });
68
+ });
69
+ ```
70
+
71
+ Examples of **correct** code for this rule
72
+
73
+ ```js
74
+ /* eslint jest/prefer-hooks-in-order: "error" */
75
+
76
+ describe('foo', () => {
77
+ beforeAll(() => {
78
+ createMyDatabase();
79
+ });
80
+
81
+ beforeEach(() => {
82
+ seedMyDatabase();
83
+ });
84
+
85
+ it('accepts this input', () => {
86
+ // ...
87
+ });
88
+
89
+ it('returns that value', () => {
90
+ // ...
91
+ });
92
+
93
+ describe('when the database has specific values', () => {
94
+ const specificValue = '...';
95
+
96
+ beforeEach(() => {
97
+ seedMyDatabase(specificValue);
98
+ });
99
+
100
+ it('accepts that input', () => {
101
+ // ...
102
+ });
103
+
104
+ it('throws an error', () => {
105
+ // ...
106
+ });
107
+
108
+ beforeEach(() => {
109
+ mockLogger();
110
+ });
111
+
112
+ afterEach(() => {
113
+ clearLogger();
114
+ });
115
+
116
+ it('logs a message', () => {
117
+ // ...
118
+ });
119
+ });
120
+
121
+ afterAll(() => {
122
+ removeMyDatabase();
123
+ });
124
+ });
125
+ ```
126
+
127
+ ## Also See
128
+
129
+ - [`prefer-hooks-on-top`](prefer-hooks-on-top.md)
130
+
131
+ ## Further Reading
132
+
133
+ - [Order of execution of describe and test blocks](https://jestjs.io/docs/setup-teardown#order-of-execution-of-describe-and-test-blocks)
@@ -44,13 +44,13 @@ var _default = (0, _utils2.createRule)({
44
44
  }],
45
45
 
46
46
  create(context) {
47
- const scope = context.getScope();
48
47
  const configObj = context.options[0] || {};
49
48
  const testKeyword = configObj.fn || _utils2.TestCaseName.test;
50
49
  const testKeywordWithinDescribe = configObj.withinDescribe || configObj.fn || _utils2.TestCaseName.it;
51
50
  let describeNestingLevel = 0;
52
51
  return {
53
52
  CallExpression(node) {
53
+ const scope = context.getScope();
54
54
  const nodeName = (0, _utils2.getNodeName)(node.callee);
55
55
 
56
56
  if (!nodeName) {
@@ -91,7 +91,7 @@ var _default = (0, _utils2.createRule)({
91
91
  },
92
92
 
93
93
  'CallExpression:exit'(node) {
94
- if ((0, _utils2.isDescribeCall)(node, scope)) {
94
+ if ((0, _utils2.isDescribeCall)(node, context.getScope())) {
95
95
  describeNestingLevel--;
96
96
  }
97
97
  }
@@ -68,7 +68,6 @@ var _default = (0, _utils2.createRule)({
68
68
  assertFunctionNames = ['expect'],
69
69
  additionalTestBlockFunctions = []
70
70
  }]) {
71
- const scope = context.getScope();
72
71
  const unchecked = [];
73
72
 
74
73
  function checkCallExpressionUsed(nodes) {
@@ -77,7 +76,7 @@ var _default = (0, _utils2.createRule)({
77
76
 
78
77
  if (node.type === _utils.AST_NODE_TYPES.FunctionDeclaration) {
79
78
  const declaredVariables = context.getDeclaredVariables(node);
80
- const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, scope);
79
+ const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context.getScope());
81
80
  checkCallExpressionUsed(testCallExpressions);
82
81
  }
83
82
 
@@ -94,7 +93,7 @@ var _default = (0, _utils2.createRule)({
94
93
 
95
94
  const name = (_getNodeName = (0, _utils2.getNodeName)(node.callee)) !== null && _getNodeName !== void 0 ? _getNodeName : '';
96
95
 
97
- if ((0, _utils2.isTestCaseCall)(node, scope) || additionalTestBlockFunctions.includes(name)) {
96
+ if ((0, _utils2.isTestCaseCall)(node, context.getScope()) || additionalTestBlockFunctions.includes(name)) {
98
97
  if (node.callee.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(node.callee.property, 'todo')) {
99
98
  return;
100
99
  }
@@ -39,7 +39,6 @@ var _default = (0, _utils2.createRule)({
39
39
  create(context, [{
40
40
  max
41
41
  }]) {
42
- const scope = context.getScope();
43
42
  const describeCallbackStack = [];
44
43
 
45
44
  function pushDescribeCallback(node) {
@@ -47,7 +46,7 @@ var _default = (0, _utils2.createRule)({
47
46
  parent
48
47
  } = node;
49
48
 
50
- if ((parent === null || parent === void 0 ? void 0 : parent.type) !== _utils.AST_NODE_TYPES.CallExpression || !(0, _utils2.isDescribeCall)(parent, scope)) {
49
+ if ((parent === null || parent === void 0 ? void 0 : parent.type) !== _utils.AST_NODE_TYPES.CallExpression || !(0, _utils2.isDescribeCall)(parent, context.getScope())) {
51
50
  return;
52
51
  }
53
52
 
@@ -70,7 +69,7 @@ var _default = (0, _utils2.createRule)({
70
69
  parent
71
70
  } = node;
72
71
 
73
- if ((parent === null || parent === void 0 ? void 0 : parent.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isDescribeCall)(parent, scope)) {
72
+ if ((parent === null || parent === void 0 ? void 0 : parent.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isDescribeCall)(parent, context.getScope())) {
74
73
  describeCallbackStack.pop();
75
74
  }
76
75
  }
@@ -28,7 +28,6 @@ var _default = (0, _utils2.createRule)({
28
28
  defaultOptions: [],
29
29
 
30
30
  create(context) {
31
- const scope = context.getScope();
32
31
  let conditionalDepth = 0;
33
32
  let inTestCase = false;
34
33
  let inPromiseCatch = false;
@@ -40,7 +39,7 @@ var _default = (0, _utils2.createRule)({
40
39
  return {
41
40
  FunctionDeclaration(node) {
42
41
  const declaredVariables = context.getDeclaredVariables(node);
43
- const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, scope);
42
+ const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context.getScope());
44
43
 
45
44
  if (testCallExpressions.length > 0) {
46
45
  inTestCase = true;
@@ -48,7 +47,7 @@ var _default = (0, _utils2.createRule)({
48
47
  },
49
48
 
50
49
  CallExpression(node) {
51
- if ((0, _utils2.isTestCaseCall)(node, scope)) {
50
+ if ((0, _utils2.isTestCaseCall)(node, context.getScope())) {
52
51
  inTestCase = true;
53
52
  }
54
53
 
@@ -72,7 +71,7 @@ var _default = (0, _utils2.createRule)({
72
71
  },
73
72
 
74
73
  'CallExpression:exit'(node) {
75
- if ((0, _utils2.isTestCaseCall)(node, scope)) {
74
+ if ((0, _utils2.isTestCaseCall)(node, context.getScope())) {
76
75
  inTestCase = false;
77
76
  }
78
77
 
@@ -24,7 +24,6 @@ var _default = (0, _utils.createRule)({
24
24
  defaultOptions: [],
25
25
 
26
26
  create(context) {
27
- const scope = context.getScope();
28
27
  let inTestCase = false;
29
28
 
30
29
  const maybeReportConditional = node => {
@@ -38,13 +37,13 @@ var _default = (0, _utils.createRule)({
38
37
 
39
38
  return {
40
39
  CallExpression(node) {
41
- if ((0, _utils.isTestCaseCall)(node, scope)) {
40
+ if ((0, _utils.isTestCaseCall)(node, context.getScope())) {
42
41
  inTestCase = true;
43
42
  }
44
43
  },
45
44
 
46
45
  'CallExpression:exit'(node) {
47
- if ((0, _utils.isTestCaseCall)(node, scope)) {
46
+ if ((0, _utils.isTestCaseCall)(node, context.getScope())) {
48
47
  inTestCase = false;
49
48
  }
50
49
  },
@@ -46,7 +46,6 @@ var _default = (0, _utils2.createRule)({
46
46
  defaultOptions: [],
47
47
 
48
48
  create(context) {
49
- const scope = context.getScope();
50
49
  return {
51
50
  CallExpression(node) {
52
51
  var _getNodeName$endsWith, _getNodeName;
@@ -61,7 +60,7 @@ var _default = (0, _utils2.createRule)({
61
60
  return;
62
61
  }
63
62
 
64
- const callback = findCallbackArg(node, isJestEach, scope);
63
+ const callback = findCallbackArg(node, isJestEach, context.getScope());
65
64
  const callbackArgIndex = Number(isJestEach);
66
65
 
67
66
  if (!callback || !(0, _utils2.isFunction)(callback) || callback.params.length !== 1 + callbackArgIndex) {
@@ -31,10 +31,11 @@ var _default = (0, _utils.createRule)({
31
31
  defaultOptions: [],
32
32
 
33
33
  create(context) {
34
- const scope = context.getScope();
35
34
  const hookContexts = [newHookContext()];
36
35
  return {
37
36
  CallExpression(node) {
37
+ const scope = context.getScope();
38
+
38
39
  if ((0, _utils.isDescribeCall)(node, scope)) {
39
40
  hookContexts.push(newHookContext());
40
41
  }
@@ -56,7 +57,7 @@ var _default = (0, _utils.createRule)({
56
57
  },
57
58
 
58
59
  'CallExpression:exit'(node) {
59
- if ((0, _utils.isDescribeCall)(node, scope)) {
60
+ if ((0, _utils.isDescribeCall)(node, context.getScope())) {
60
61
  hookContexts.pop();
61
62
  }
62
63
  }
@@ -26,7 +26,6 @@ var _default = (0, _utils2.createRule)({
26
26
  defaultOptions: [],
27
27
 
28
28
  create(context) {
29
- const scope = context.getScope();
30
29
  const exportNodes = [];
31
30
  let hasTestCase = false;
32
31
  return {
@@ -42,7 +41,7 @@ var _default = (0, _utils2.createRule)({
42
41
  },
43
42
 
44
43
  CallExpression(node) {
45
- if ((0, _utils2.isTestCaseCall)(node, scope)) {
44
+ if ((0, _utils2.isTestCaseCall)(node, context.getScope())) {
46
45
  hasTestCase = true;
47
46
  }
48
47
  },
@@ -47,9 +47,10 @@ var _default = (0, _utils2.createRule)({
47
47
  defaultOptions: [],
48
48
 
49
49
  create(context) {
50
- const scope = context.getScope();
51
50
  return {
52
51
  CallExpression(node) {
52
+ const scope = context.getScope();
53
+
53
54
  if (!(0, _utils2.isDescribeCall)(node, scope) && !(0, _utils2.isTestCaseCall)(node, scope)) {
54
55
  return;
55
56
  }
@@ -37,10 +37,9 @@ var _default = (0, _utils.createRule)({
37
37
  create(context, [{
38
38
  allow = []
39
39
  }]) {
40
- const scope = context.getScope();
41
40
  return {
42
41
  CallExpression(node) {
43
- if ((0, _utils.isHookCall)(node, scope) && !allow.includes(node.callee.name)) {
42
+ if ((0, _utils.isHookCall)(node, context.getScope()) && !allow.includes(node.callee.name)) {
44
43
  context.report({
45
44
  node,
46
45
  messageId: 'unexpectedHook',
@@ -30,12 +30,12 @@ var _default = (0, _utils.createRule)({
30
30
  defaultOptions: [],
31
31
 
32
32
  create(context) {
33
- const scope = context.getScope();
34
33
  const contexts = [newDescribeContext()];
35
34
  return {
36
35
  CallExpression(node) {
37
36
  var _getNodeName;
38
37
 
38
+ const scope = context.getScope();
39
39
  const currentLayer = contexts[contexts.length - 1];
40
40
 
41
41
  if ((0, _utils.isDescribeCall)(node, scope)) {
@@ -80,7 +80,7 @@ var _default = (0, _utils.createRule)({
80
80
  },
81
81
 
82
82
  'CallExpression:exit'(node) {
83
- if ((0, _utils.isDescribeCall)(node, scope)) {
83
+ if ((0, _utils.isDescribeCall)(node, context.getScope())) {
84
84
  contexts.pop();
85
85
  }
86
86
  }
@@ -38,7 +38,6 @@ var _default = (0, _utils2.createRule)({
38
38
  defaultOptions: [],
39
39
 
40
40
  create(context) {
41
- const scope = context.getScope();
42
41
  const stack = [];
43
42
 
44
43
  function validate(node) {
@@ -59,7 +58,7 @@ var _default = (0, _utils2.createRule)({
59
58
 
60
59
  return {
61
60
  CallExpression(node) {
62
- if ((0, _utils2.isTestCaseCall)(node, scope)) {
61
+ if ((0, _utils2.isTestCaseCall)(node, context.getScope())) {
63
62
  stack.push(true);
64
63
 
65
64
  if ((0, _utils2.getNodeName)(node).endsWith('each')) {
@@ -74,7 +73,7 @@ var _default = (0, _utils2.createRule)({
74
73
 
75
74
  FunctionDeclaration(node) {
76
75
  const declaredVariables = context.getDeclaredVariables(node);
77
- const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, scope);
76
+ const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context.getScope());
78
77
  stack.push(testCallExpressions.length > 0);
79
78
  },
80
79
 
@@ -69,12 +69,11 @@ var _default = (0, _utils2.createRule)({
69
69
  create(context, [{
70
70
  additionalTestBlockFunctions = []
71
71
  }]) {
72
- const scope = context.getScope();
73
72
  const callStack = [];
74
73
 
75
74
  const isCustomTestBlockFunction = node => additionalTestBlockFunctions.includes((0, _utils2.getNodeName)(node) || '');
76
75
 
77
- const isTestBlock = node => (0, _utils2.isTestCaseCall)(node, scope) || isCustomTestBlockFunction(node);
76
+ const isTestBlock = node => (0, _utils2.isTestCaseCall)(node, context.getScope()) || isCustomTestBlockFunction(node);
78
77
 
79
78
  return {
80
79
  CallExpression(node) {
@@ -109,7 +108,7 @@ var _default = (0, _utils2.createRule)({
109
108
  },
110
109
 
111
110
  BlockStatement(statement) {
112
- const blockType = getBlockType(statement, scope);
111
+ const blockType = getBlockType(statement, context.getScope());
113
112
 
114
113
  if (blockType) {
115
114
  callStack.push(blockType);
@@ -117,7 +116,7 @@ var _default = (0, _utils2.createRule)({
117
116
  },
118
117
 
119
118
  'BlockStatement:exit'(statement) {
120
- if (callStack[callStack.length - 1] === getBlockType(statement, scope)) {
119
+ if (callStack[callStack.length - 1] === getBlockType(statement, context.getScope())) {
121
120
  callStack.pop();
122
121
  }
123
122
  },
@@ -27,9 +27,9 @@ var _default = (0, _utils2.createRule)({
27
27
  defaultOptions: [],
28
28
 
29
29
  create(context) {
30
- const scope = context.getScope();
31
30
  return {
32
31
  CallExpression(node) {
32
+ const scope = context.getScope();
33
33
  const nodeName = (0, _utils2.getNodeName)(node.callee);
34
34
  if (!nodeName || !(0, _utils2.isDescribeCall)(node, scope) && !(0, _utils2.isTestCaseCall)(node, scope)) return;
35
35
  const preferredNodeName = getPreferredNodeName(nodeName);
@@ -36,10 +36,9 @@ var _default = (0, _utils2.createRule)({
36
36
  defaultOptions: [],
37
37
 
38
38
  create(context) {
39
- const scope = context.getScope();
40
39
  return {
41
40
  CallExpression(node) {
42
- if (!(0, _utils2.isTestCaseCall)(node, scope)) return;
41
+ if (!(0, _utils2.isTestCaseCall)(node, context.getScope())) return;
43
42
  const body = getBody(node.arguments);
44
43
  const returnStmt = body.find(t => t.type === _utils.AST_NODE_TYPES.ReturnStatement);
45
44
  if (!returnStmt) return;
@@ -51,7 +50,7 @@ var _default = (0, _utils2.createRule)({
51
50
 
52
51
  FunctionDeclaration(node) {
53
52
  const declaredVariables = context.getDeclaredVariables(node);
54
- const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, scope);
53
+ const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context.getScope());
55
54
  if (testCallExpressions.length === 0) return;
56
55
  const returnStmt = node.body.body.find(t => t.type === _utils.AST_NODE_TYPES.ReturnStatement);
57
56
  if (!returnStmt) return;
@@ -63,7 +63,6 @@ var _default = (0, _utils2.createRule)({
63
63
  }],
64
64
 
65
65
  create(context, [options]) {
66
- const scope = context.getScope();
67
66
  let expressionDepth = 0;
68
67
  let hasExpectInCallback = false;
69
68
  let hasExpectInLoop = false;
@@ -117,7 +116,7 @@ var _default = (0, _utils2.createRule)({
117
116
  'ForOfStatement:exit': exitForLoop,
118
117
 
119
118
  CallExpression(node) {
120
- if ((0, _utils2.isTestCaseCall)(node, scope)) {
119
+ if ((0, _utils2.isTestCaseCall)(node, context.getScope())) {
121
120
  inTestCaseCall = true;
122
121
  return;
123
122
  }
@@ -134,7 +133,7 @@ var _default = (0, _utils2.createRule)({
134
133
  },
135
134
 
136
135
  'CallExpression:exit'(node) {
137
- if (!(0, _utils2.isTestCaseCall)(node, scope)) {
136
+ if (!(0, _utils2.isTestCaseCall)(node, context.getScope())) {
138
137
  return;
139
138
  }
140
139
 
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _utils = require("./utils");
9
+
10
+ const HooksOrder = ['beforeAll', 'beforeEach', 'afterEach', 'afterAll'];
11
+
12
+ var _default = (0, _utils.createRule)({
13
+ name: __filename,
14
+ meta: {
15
+ docs: {
16
+ category: 'Best Practices',
17
+ description: 'Prefer having hooks in a consistent order',
18
+ recommended: false
19
+ },
20
+ messages: {
21
+ reorderHooks: `\`{{ currentHook }}\` hooks should be before any \`{{ previousHook }}\` hooks`
22
+ },
23
+ schema: [],
24
+ type: 'suggestion'
25
+ },
26
+ defaultOptions: [],
27
+
28
+ create(context) {
29
+ let previousHookIndex = -1;
30
+ let inHook = false;
31
+ return {
32
+ CallExpression(node) {
33
+ if (inHook) {
34
+ // Ignore everything that is passed into a hook
35
+ return;
36
+ }
37
+
38
+ if (!(0, _utils.isHookCall)(node, context.getScope())) {
39
+ // Reset the previousHookIndex when encountering something different from a hook
40
+ previousHookIndex = -1;
41
+ return;
42
+ }
43
+
44
+ inHook = true;
45
+ const currentHook = node.callee.name;
46
+ const currentHookIndex = HooksOrder.indexOf(currentHook);
47
+
48
+ if (currentHookIndex < previousHookIndex) {
49
+ context.report({
50
+ messageId: 'reorderHooks',
51
+ node,
52
+ data: {
53
+ previousHook: HooksOrder[previousHookIndex],
54
+ currentHook
55
+ }
56
+ });
57
+ return;
58
+ }
59
+
60
+ previousHookIndex = currentHookIndex;
61
+ },
62
+
63
+ 'CallExpression:exit'(node) {
64
+ if ((0, _utils.isHookCall)(node, context.getScope())) {
65
+ inHook = false;
66
+ return;
67
+ }
68
+
69
+ if (inHook) {
70
+ return;
71
+ } // Reset the previousHookIndex when encountering something different from a hook
72
+
73
+
74
+ previousHookIndex = -1;
75
+ }
76
+
77
+ };
78
+ }
79
+
80
+ });
81
+
82
+ exports.default = _default;
@@ -24,10 +24,11 @@ var _default = (0, _utils.createRule)({
24
24
  defaultOptions: [],
25
25
 
26
26
  create(context) {
27
- const scope = context.getScope();
28
27
  const hooksContext = [false];
29
28
  return {
30
29
  CallExpression(node) {
30
+ const scope = context.getScope();
31
+
31
32
  if (!(0, _utils.isHookCall)(node, scope) && (0, _utils.isTestCaseCall)(node, scope)) {
32
33
  hooksContext[hooksContext.length - 1] = true;
33
34
  }
@@ -88,11 +88,12 @@ var _default = (0, _utils.createRule)({
88
88
  allowedPrefixes = [],
89
89
  ignoreTopLevelDescribe
90
90
  }]) {
91
- const scope = context.getScope();
92
91
  const ignores = populateIgnores(ignore);
93
92
  let numberOfDescribeBlocks = 0;
94
93
  return {
95
94
  CallExpression(node) {
95
+ const scope = context.getScope();
96
+
96
97
  if ((0, _utils.isDescribeCall)(node, scope)) {
97
98
  numberOfDescribeBlocks++;
98
99
 
@@ -138,7 +139,7 @@ var _default = (0, _utils.createRule)({
138
139
  },
139
140
 
140
141
  'CallExpression:exit'(node) {
141
- if ((0, _utils.isDescribeCall)(node, scope)) {
142
+ if ((0, _utils.isDescribeCall)(node, context.getScope())) {
142
143
  numberOfDescribeBlocks--;
143
144
  }
144
145
  }
@@ -58,7 +58,6 @@ var _default = (0, _utils.createRule)({
58
58
  defaultOptions: ['multi'],
59
59
 
60
60
  create(context, [mode]) {
61
- const scope = context.getScope();
62
61
  const snapshotMatchers = [];
63
62
  const depths = [];
64
63
  let expressionDepth = 0;
@@ -107,6 +106,8 @@ var _default = (0, _utils.createRule)({
107
106
  'ArrowFunctionExpression:exit': exitExpression,
108
107
 
109
108
  'CallExpression:exit'(node) {
109
+ const scope = context.getScope();
110
+
110
111
  if ((0, _utils.isDescribeCall)(node, scope) || (0, _utils.isTestCaseCall)(node, scope)) {
111
112
  var _depths$pop;
112
113
 
@@ -116,6 +117,8 @@ var _default = (0, _utils.createRule)({
116
117
  },
117
118
 
118
119
  CallExpression(node) {
120
+ const scope = context.getScope();
121
+
119
122
  if ((0, _utils.isDescribeCall)(node, scope) || (0, _utils.isTestCaseCall)(node, scope)) {
120
123
  depths.push(expressionDepth);
121
124
  expressionDepth = 0;
@@ -43,12 +43,11 @@ var _default = (0, _utils2.createRule)({
43
43
  defaultOptions: [],
44
44
 
45
45
  create(context) {
46
- const scope = context.getScope();
47
46
  return {
48
47
  CallExpression(node) {
49
48
  const [title, callback] = node.arguments;
50
49
 
51
- if (!title || !isTargetedTestCase(node, scope) || !(0, _utils2.isStringNode)(title)) {
50
+ if (!title || !isTargetedTestCase(node, context.getScope()) || !(0, _utils2.isStringNode)(title)) {
52
51
  return;
53
52
  }
54
53
 
@@ -79,14 +79,13 @@ var _default = (0, _utils2.createRule)({
79
79
  create(context) {
80
80
  var _context$options$;
81
81
 
82
- const scope = context.getScope();
83
82
  const {
84
83
  allowedFunctionCalls
85
84
  } = (_context$options$ = context.options[0]) !== null && _context$options$ !== void 0 ? _context$options$ : {};
86
85
 
87
86
  const checkBlockBody = body => {
88
87
  for (const statement of body) {
89
- if (shouldBeInHook(statement, scope, allowedFunctionCalls)) {
88
+ if (shouldBeInHook(statement, context.getScope(), allowedFunctionCalls)) {
90
89
  context.report({
91
90
  node: statement,
92
91
  messageId: 'useHook'
@@ -101,7 +100,7 @@ var _default = (0, _utils2.createRule)({
101
100
  },
102
101
 
103
102
  CallExpression(node) {
104
- if (!(0, _utils2.isDescribeCall)(node, scope) || node.arguments.length < 2) {
103
+ if (!(0, _utils2.isDescribeCall)(node, context.getScope()) || node.arguments.length < 2) {
105
104
  return;
106
105
  }
107
106
 
@@ -42,11 +42,12 @@ var _default = (0, _utils.createRule)({
42
42
  const {
43
43
  maxNumberOfTopLevelDescribes = Infinity
44
44
  } = (_context$options$ = context.options[0]) !== null && _context$options$ !== void 0 ? _context$options$ : {};
45
- const scope = context.getScope();
46
45
  let numberOfTopLevelDescribeBlocks = 0;
47
46
  let numberOfDescribeBlocks = 0;
48
47
  return {
49
48
  CallExpression(node) {
49
+ const scope = context.getScope();
50
+
50
51
  if ((0, _utils.isDescribeCall)(node, scope)) {
51
52
  numberOfDescribeBlocks++;
52
53
 
@@ -88,7 +89,7 @@ var _default = (0, _utils.createRule)({
88
89
  },
89
90
 
90
91
  'CallExpression:exit'(node) {
91
- if ((0, _utils.isDescribeCall)(node, scope)) {
92
+ if ((0, _utils.isDescribeCall)(node, context.getScope())) {
92
93
  numberOfDescribeBlocks--;
93
94
  }
94
95
  }
@@ -582,14 +582,8 @@ const collectReferences = scope => {
582
582
  if (ref.defs.length === 0) {
583
583
  continue;
584
584
  }
585
- /* istanbul ignore if */
586
585
 
587
-
588
- if (ref.defs.length > 1) {
589
- throw new Error(`Reference unexpected had more than one definition - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
590
- }
591
-
592
- const [def] = ref.defs;
586
+ const def = ref.defs[ref.defs.length - 1];
593
587
  const importDetails = describePossibleImportDef(def);
594
588
 
595
589
  if (importDetails) {
@@ -39,10 +39,9 @@ var _default = (0, _utils2.createRule)({
39
39
  defaultOptions: [],
40
40
 
41
41
  create(context) {
42
- const scope = context.getScope();
43
42
  return {
44
43
  CallExpression(node) {
45
- if (!(0, _utils2.isDescribeCall)(node, scope)) {
44
+ if (!(0, _utils2.isDescribeCall)(node, context.getScope())) {
46
45
  return;
47
46
  }
48
47
 
@@ -299,7 +299,6 @@ var _default = (0, _utils2.createRule)({
299
299
  defaultOptions: [],
300
300
 
301
301
  create(context) {
302
- const scope = context.getScope();
303
302
  let inTestCaseWithDoneCallback = false; // an array of booleans representing each promise chain we enter, with the
304
303
  // boolean value representing if we think a given chain contains an expect
305
304
  // in it's body.
@@ -313,7 +312,7 @@ var _default = (0, _utils2.createRule)({
313
312
  CallExpression(node) {
314
313
  // there are too many ways that the done argument could be used with
315
314
  // promises that contain expect that would make the promise safe for us
316
- if (isTestCaseCallWithCallbackArg(node, scope)) {
315
+ if (isTestCaseCallWithCallbackArg(node, context.getScope())) {
317
316
  inTestCaseWithDoneCallback = true;
318
317
  return;
319
318
  } // if this call expression is a promise chain, add it to the stack with
@@ -337,7 +336,7 @@ var _default = (0, _utils2.createRule)({
337
336
  // make promises containing expects safe in a test for us to be able to
338
337
  // accurately check, so we just bail out completely if it's present
339
338
  if (inTestCaseWithDoneCallback) {
340
- if ((0, _utils2.isTestCaseCall)(node, scope)) {
339
+ if ((0, _utils2.isTestCaseCall)(node, context.getScope())) {
341
340
  inTestCaseWithDoneCallback = false;
342
341
  }
343
342
 
@@ -364,7 +363,7 @@ var _default = (0, _utils2.createRule)({
364
363
  // because we're most likely in the body of a function being defined
365
364
  // within the test, which we can't track
366
365
 
367
- if (!parent || !isDirectlyWithinTestCaseCall(parent, scope)) {
366
+ if (!parent || !isDirectlyWithinTestCaseCall(parent, context.getScope())) {
368
367
  return;
369
368
  }
370
369
 
@@ -123,7 +123,6 @@ var _default = (0, _utils2.createRule)({
123
123
  mustNotMatch,
124
124
  mustMatch
125
125
  }]) {
126
- const scope = context.getScope();
127
126
  const disallowedWordsRegexp = new RegExp(`\\b(${disallowedWords.join('|')})\\b`, 'iu');
128
127
  const mustNotMatchPatterns = compileMatcherPatterns(mustNotMatch !== null && mustNotMatch !== void 0 ? mustNotMatch : {});
129
128
  const mustMatchPatterns = compileMatcherPatterns(mustMatch !== null && mustMatch !== void 0 ? mustMatch : {});
@@ -131,6 +130,8 @@ var _default = (0, _utils2.createRule)({
131
130
  CallExpression(node) {
132
131
  var _mustNotMatchPatterns, _mustMatchPatterns$je;
133
132
 
133
+ const scope = context.getScope();
134
+
134
135
  if (!(0, _utils2.isDescribeCall)(node, scope) && !(0, _utils2.isTestCaseCall)(node, scope)) {
135
136
  return;
136
137
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "26.2.0",
3
+ "version": "26.3.0",
4
4
  "description": "ESLint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",