Skip to content

Commit

Permalink
no-hooks-for-single-case: fix false postive in nested suites
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Feb 18, 2020
1 parent 1c3a545 commit 46f716d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
16 changes: 13 additions & 3 deletions lib/rules/no-hooks-for-single-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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)) {
Expand Down
27 changes: 14 additions & 13 deletions test/rules/no-hooks-for-single-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {',
Expand Down Expand Up @@ -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() {',
Expand Down

0 comments on commit 46f716d

Please sign in to comment.