From 46f716dcf8ab82577b633bb51617aac36ca2172f Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Tue, 18 Feb 2020 19:38:33 +0100 Subject: [PATCH] no-hooks-for-single-case: fix false postive in nested suites --- lib/rules/no-hooks-for-single-case.js | 16 ++++++++++++--- test/rules/no-hooks-for-single-case.js | 27 +++++++++++++------------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/rules/no-hooks-for-single-case.js b/lib/rules/no-hooks-for-single-case.js index 8b663c3..c24a0c9 100644 --- a/lib/rules/no-hooks-for-single-case.js +++ b/lib/rules/no-hooks-for-single-case.js @@ -31,7 +31,17 @@ module.exports = { const options = context.options[0] || {}; const allowedHooks = options.allow || []; const settings = context.settings; - const layers = []; + let layers = []; + + function increaseTestCount() { + layers = layers.map((layer) => { + return { + describeNode: layer.describeNode, + hookNodes: layer.hookNodes, + testCount: layer.testCount + 1 + }; + }); + } function popLayer(node) { const layer = layers[layers.length - 1]; @@ -59,13 +69,13 @@ module.exports = { CallExpression(node) { if (astUtil.isDescribe(node, additionalSuiteNames(settings))) { - layers[layers.length - 1].testCount += 1; + increaseTestCount(); layers.push(newDescribeLayer(node)); return; } if (astUtil.isTestCase(node)) { - layers[layers.length - 1].testCount += 1; + increaseTestCount(); } if (astUtil.isHookIdentifier(node.callee)) { diff --git a/test/rules/no-hooks-for-single-case.js b/test/rules/no-hooks-for-single-case.js index dbb485f..6eeb677 100644 --- a/test/rules/no-hooks-for-single-case.js +++ b/test/rules/no-hooks-for-single-case.js @@ -84,6 +84,20 @@ ruleTester.run('no-hooks-for-single-case', rules['no-hooks-for-single-case'], { ' });', '});' ].join('\n'), + [ + 'describe(function() {', + ' describe(function() {', + ' it(function() {});', + ' it(function() {});', + ' });', + ' afterEach(function() {});', + '});' + ].join('\n'), + [ + 'it(function() {});', + 'it(function() {});', + 'afterEach(function() {});' + ].join('\n'), { code: [ 'describe(function() {', @@ -262,19 +276,6 @@ ruleTester.run('no-hooks-for-single-case', rules['no-hooks-for-single-case'], { ].join('\n'), errors: [ { message: 'Unexpected use of Mocha `before` hook for a single test case', column: 9, line: 5 } ] }, - { - code: [ - 'describe(function() {', - ' before(function() {});', - ' describe(function() {', - ' before(function() {});', - ' it(function() {});', - ' it(function() {});', - ' });', - '});' - ].join('\n'), - errors: [ { message: 'Unexpected use of Mocha `before` hook for a single test case', column: 5, line: 2 } ] - }, { code: [ 'describe(function() {',