Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | /* eslint "complexity": [ "error", 5 ] */ |
| 4 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 5 | const createAstUtils = require('../util/ast'); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 6 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 7 | module.exports = { |
| 8 | meta: { |
| 9 | type: 'problem', |
| 10 | docs: { |
| 11 | description: 'Disallow tests to be nested within other tests ' |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 12 | } |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 13 | }, |
| 14 | create(context) { |
| 15 | const astUtils = createAstUtils(context.settings); |
| 16 | let testNestingLevel = 0; |
| 17 | let hookCallNestingLevel = 0; |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 18 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 19 | function report(callExpression, message) { |
| 20 | context.report({ |
| 21 | message, |
| 22 | node: callExpression.callee |
| 23 | }); |
| 24 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 25 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 26 | function isNestedTest(isTestCase, isDescribe, nestingLevel) { |
| 27 | const isNested = nestingLevel > 0; |
| 28 | const isTest = isTestCase || isDescribe; |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 29 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 30 | return isNested && isTest; |
| 31 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 32 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 33 | function checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall) { |
| 34 | if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) { |
| 35 | const message = isDescribe ? |
| 36 | 'Unexpected suite nested within a test.' : |
| 37 | 'Unexpected test nested within another test.'; |
| 38 | report(node, message); |
| 39 | } else if (isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)) { |
| 40 | const message = isHookCall ? |
| 41 | 'Unexpected test hook nested within a test hook.' : |
| 42 | 'Unexpected test nested within a test hook.'; |
| 43 | report(node, message); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 44 | } |
| 45 | } |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 46 | |
| 47 | return { |
| 48 | CallExpression(node) { |
| 49 | const isTestCase = astUtils.isTestCase(node); |
| 50 | const isHookCall = astUtils.isHookCall(node); |
| 51 | const isDescribe = astUtils.isDescribe(node); |
| 52 | |
| 53 | checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall); |
| 54 | |
| 55 | if (isTestCase) { |
| 56 | testNestingLevel += 1; |
| 57 | } else if (isHookCall) { |
| 58 | hookCallNestingLevel += 1; |
| 59 | } |
| 60 | }, |
| 61 | |
| 62 | 'CallExpression:exit'(node) { |
| 63 | if (astUtils.isTestCase(node)) { |
| 64 | testNestingLevel -= 1; |
| 65 | } else if (astUtils.isHookCall(node)) { |
| 66 | hookCallNestingLevel -= 1; |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 71 | }; |