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; |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 18 | const isTestCase = astUtils.buildIsTestCaseAnswerer(); |
| 19 | const isDescribe = astUtils.buildIsDescribeAnswerer(); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 20 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 21 | function report(callExpression, message) { |
| 22 | context.report({ |
| 23 | message, |
| 24 | node: callExpression.callee |
| 25 | }); |
| 26 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 27 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 28 | function isNestedTest(_isTestCase, _isDescribe, nestingLevel) { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 29 | const isNested = nestingLevel > 0; |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 30 | const isTest = _isTestCase || _isDescribe; |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 31 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 32 | return isNested && isTest; |
| 33 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 34 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 35 | function checkForAndReportErrors( |
| 36 | node, |
| 37 | _isTestCase, |
| 38 | _isDescribe, |
| 39 | isHookCall |
| 40 | ) { |
| 41 | if (isNestedTest(_isTestCase, _isDescribe, testNestingLevel)) { |
| 42 | const message = _isDescribe ? |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 43 | 'Unexpected suite nested within a test.' : |
| 44 | 'Unexpected test nested within another test.'; |
| 45 | report(node, message); |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 46 | } else if ( |
| 47 | isNestedTest(_isTestCase, isHookCall, hookCallNestingLevel) |
| 48 | ) { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 49 | const message = isHookCall ? |
| 50 | 'Unexpected test hook nested within a test hook.' : |
| 51 | 'Unexpected test nested within a test hook.'; |
| 52 | report(node, message); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 55 | |
| 56 | return { |
| 57 | CallExpression(node) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 58 | const _isTestCase = isTestCase(node); |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 59 | const isHookCall = astUtils.isHookCall(node); |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 60 | const _isDescribe = isDescribe(node); |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 61 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 62 | checkForAndReportErrors( |
| 63 | node, |
| 64 | _isTestCase, |
| 65 | _isDescribe, |
| 66 | isHookCall |
| 67 | ); |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 68 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 69 | if (_isTestCase) { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 70 | testNestingLevel += 1; |
| 71 | } else if (isHookCall) { |
| 72 | hookCallNestingLevel += 1; |
| 73 | } |
| 74 | }, |
| 75 | |
| 76 | 'CallExpression:exit'(node) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 77 | if (isTestCase(node)) { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 78 | testNestingLevel -= 1; |
| 79 | } else if (astUtils.isHookCall(node)) { |
| 80 | hookCallNestingLevel -= 1; |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 85 | }; |