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