|
5 | 5 | const astUtils = require('../util/ast');
|
6 | 6 | const { additionalSuiteNames } = require('../util/settings');
|
7 | 7 |
|
8 |
| -module.exports = function noNestedTests(context) { |
9 |
| - const settings = context.settings; |
10 |
| - let testNestingLevel = 0; |
11 |
| - let hookCallNestingLevel = 0; |
| 8 | +module.exports = { |
| 9 | + create(context) { |
| 10 | + const settings = context.settings; |
| 11 | + let testNestingLevel = 0; |
| 12 | + let hookCallNestingLevel = 0; |
12 | 13 |
|
13 |
| - function report(callExpression, message) { |
14 |
| - context.report({ |
15 |
| - message, |
16 |
| - node: callExpression.callee |
17 |
| - }); |
18 |
| - } |
| 14 | + function report(callExpression, message) { |
| 15 | + context.report({ |
| 16 | + message, |
| 17 | + node: callExpression.callee |
| 18 | + }); |
| 19 | + } |
19 | 20 |
|
20 |
| - function isNestedTest(isTestCase, isDescribe, nestingLevel) { |
21 |
| - const isNested = nestingLevel > 0; |
22 |
| - const isTest = isTestCase || isDescribe; |
| 21 | + function isNestedTest(isTestCase, isDescribe, nestingLevel) { |
| 22 | + const isNested = nestingLevel > 0; |
| 23 | + const isTest = isTestCase || isDescribe; |
23 | 24 |
|
24 |
| - return isNested && isTest; |
25 |
| - } |
| 25 | + return isNested && isTest; |
| 26 | + } |
26 | 27 |
|
27 |
| - function checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall) { |
28 |
| - if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) { |
29 |
| - const message = isDescribe ? |
30 |
| - 'Unexpected suite nested within a test.' : |
31 |
| - 'Unexpected test nested within another test.'; |
32 |
| - report(node, message); |
33 |
| - } else if (isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)) { |
34 |
| - const message = isHookCall ? |
35 |
| - 'Unexpected test hook nested within a test hook.' : |
36 |
| - 'Unexpected test nested within a test hook.'; |
37 |
| - report(node, message); |
| 28 | + function checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall) { |
| 29 | + if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) { |
| 30 | + const message = isDescribe ? |
| 31 | + 'Unexpected suite nested within a test.' : |
| 32 | + 'Unexpected test nested within another test.'; |
| 33 | + report(node, message); |
| 34 | + } else if (isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)) { |
| 35 | + const message = isHookCall ? |
| 36 | + 'Unexpected test hook nested within a test hook.' : |
| 37 | + 'Unexpected test nested within a test hook.'; |
| 38 | + report(node, message); |
| 39 | + } |
38 | 40 | }
|
39 |
| - } |
40 | 41 |
|
41 |
| - return { |
42 |
| - CallExpression(node) { |
43 |
| - const isTestCase = astUtils.isTestCase(node); |
44 |
| - const isHookCall = astUtils.isHookCall(node); |
45 |
| - const isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings)); |
| 42 | + return { |
| 43 | + CallExpression(node) { |
| 44 | + const isTestCase = astUtils.isTestCase(node); |
| 45 | + const isHookCall = astUtils.isHookCall(node); |
| 46 | + const isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings)); |
46 | 47 |
|
47 |
| - checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall); |
| 48 | + checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall); |
48 | 49 |
|
49 |
| - if (isTestCase) { |
50 |
| - testNestingLevel += 1; |
51 |
| - } else if (isHookCall) { |
52 |
| - hookCallNestingLevel += 1; |
53 |
| - } |
54 |
| - }, |
| 50 | + if (isTestCase) { |
| 51 | + testNestingLevel += 1; |
| 52 | + } else if (isHookCall) { |
| 53 | + hookCallNestingLevel += 1; |
| 54 | + } |
| 55 | + }, |
55 | 56 |
|
56 |
| - 'CallExpression:exit'(node) { |
57 |
| - if (astUtils.isTestCase(node)) { |
58 |
| - testNestingLevel -= 1; |
59 |
| - } else if (astUtils.isHookCall(node)) { |
60 |
| - hookCallNestingLevel -= 1; |
| 57 | + 'CallExpression:exit'(node) { |
| 58 | + if (astUtils.isTestCase(node)) { |
| 59 | + testNestingLevel -= 1; |
| 60 | + } else if (astUtils.isHookCall(node)) { |
| 61 | + hookCallNestingLevel -= 1; |
| 62 | + } |
61 | 63 | }
|
62 |
| - } |
63 |
| - }; |
| 64 | + }; |
| 65 | + } |
64 | 66 | };
|
0 commit comments