UNPKG

eslint-plugin-mocha

Version:

Eslint rules for mocha.

74 lines 2.71 kB
import { createMochaVisitors } from "../ast/mocha-visitors.js"; export const noNestedTestsRule = { meta: { type: 'problem', docs: { description: 'Disallow tests to be nested within other tests', recommended: true, url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/no-nested-tests.md' }, schema: [], messages: { suiteNestedInHook: 'Unexpected suite nested within a test hook.', suiteNestedInTest: 'Unexpected suite nested within a test.', testNestedInTest: 'Unexpected test nested within another test.', testNestedInHook: 'Unexpected test nested within a test hook.', hookNestedInHook: 'Unexpected test hook nested within a test hook.' }, languages: ['js/js'] }, create(context) { function report(node, messageId) { context.report({ messageId, node }); } let hooksNesting = 0; let testCaseNesting = 0; function reportNestedSuite(node) { if (hooksNesting > 0) { report(node, 'suiteNestedInHook'); } else if (testCaseNesting > 0) { report(node, 'suiteNestedInTest'); } } function reportNestedTestCase(node) { if (testCaseNesting > 0) { report(node, 'testNestedInTest'); } else if (hooksNesting > 0) { report(node, 'testNestedInHook'); } } function enterHook(node) { if (hooksNesting > 0) { report(node, 'hookNestedInHook'); } hooksNesting += 1; } return createMochaVisitors(context, { anyTestEntity(visitorContext) { if (visitorContext.type === 'suite') { reportNestedSuite(visitorContext.node); return; } if (visitorContext.type === 'testCase') { reportNestedTestCase(visitorContext.node); testCaseNesting += 1; return; } if (visitorContext.type === 'hook') { enterHook(visitorContext.node); } }, 'anyTestEntity:exit'(visitorContext) { if (visitorContext.type === 'testCase') { testCaseNesting -= 1; } else if (visitorContext.type === 'hook') { hooksNesting -= 1; } } }); } }; //# sourceMappingURL=no-nested-tests.js.map