Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-hooks-for-single-case: fix false postive in nested suites #238

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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