Skip to content

Commit 46f716d

Browse files
committed
no-hooks-for-single-case: fix false postive in nested suites
1 parent 1c3a545 commit 46f716d

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

lib/rules/no-hooks-for-single-case.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,17 @@ module.exports = {
3131
const options = context.options[0] || {};
3232
const allowedHooks = options.allow || [];
3333
const settings = context.settings;
34-
const layers = [];
34+
let layers = [];
35+
36+
function increaseTestCount() {
37+
layers = layers.map((layer) => {
38+
return {
39+
describeNode: layer.describeNode,
40+
hookNodes: layer.hookNodes,
41+
testCount: layer.testCount + 1
42+
};
43+
});
44+
}
3545

3646
function popLayer(node) {
3747
const layer = layers[layers.length - 1];
@@ -59,13 +69,13 @@ module.exports = {
5969

6070
CallExpression(node) {
6171
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
62-
layers[layers.length - 1].testCount += 1;
72+
increaseTestCount();
6373
layers.push(newDescribeLayer(node));
6474
return;
6575
}
6676

6777
if (astUtil.isTestCase(node)) {
68-
layers[layers.length - 1].testCount += 1;
78+
increaseTestCount();
6979
}
7080

7181
if (astUtil.isHookIdentifier(node.callee)) {

test/rules/no-hooks-for-single-case.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ ruleTester.run('no-hooks-for-single-case', rules['no-hooks-for-single-case'], {
8484
' });',
8585
'});'
8686
].join('\n'),
87+
[
88+
'describe(function() {',
89+
' describe(function() {',
90+
' it(function() {});',
91+
' it(function() {});',
92+
' });',
93+
' afterEach(function() {});',
94+
'});'
95+
].join('\n'),
96+
[
97+
'it(function() {});',
98+
'it(function() {});',
99+
'afterEach(function() {});'
100+
].join('\n'),
87101
{
88102
code: [
89103
'describe(function() {',
@@ -262,19 +276,6 @@ ruleTester.run('no-hooks-for-single-case', rules['no-hooks-for-single-case'], {
262276
].join('\n'),
263277
errors: [ { message: 'Unexpected use of Mocha `before` hook for a single test case', column: 9, line: 5 } ]
264278
},
265-
{
266-
code: [
267-
'describe(function() {',
268-
' before(function() {});',
269-
' describe(function() {',
270-
' before(function() {});',
271-
' it(function() {});',
272-
' it(function() {});',
273-
' });',
274-
'});'
275-
].join('\n'),
276-
errors: [ { message: 'Unexpected use of Mocha `before` hook for a single test case', column: 5, line: 2 } ]
277-
},
278279
{
279280
code: [
280281
'describe(function() {',

0 commit comments

Comments
 (0)