Skip to content

Commit

Permalink
Merge pull request #290 from lo1tuma/nested-tests
Browse files Browse the repository at this point in the history
Improve no-nested-tests performance
  • Loading branch information
lo1tuma authored May 26, 2021
2 parents 9c083eb + 5381655 commit 1b73ddb
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/rules/no-nested-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module.exports = {
const astUtils = createAstUtils(context.settings);
let testNestingLevel = 0;
let hookCallNestingLevel = 0;
const isTestCase = astUtils.buildIsTestCaseAnswerer();
const isDescribe = astUtils.buildIsDescribeAnswerer();

function report(callExpression, message) {
context.report({
Expand All @@ -23,26 +25,26 @@ module.exports = {
});
}

function isNestedTest(isTestCase, isDescribe, nestingLevel) {
function isNestedTest(_isTestCase, _isDescribe, nestingLevel) {
const isNested = nestingLevel > 0;
const isTest = isTestCase || isDescribe;
const isTest = _isTestCase || _isDescribe;

return isNested && isTest;
}

function checkForAndReportErrors(
node,
isTestCase,
isDescribe,
_isTestCase,
_isDescribe,
isHookCall
) {
if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) {
const message = isDescribe ?
if (isNestedTest(_isTestCase, _isDescribe, testNestingLevel)) {
const message = _isDescribe ?
'Unexpected suite nested within a test.' :
'Unexpected test nested within another test.';
report(node, message);
} else if (
isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)
isNestedTest(_isTestCase, isHookCall, hookCallNestingLevel)
) {
const message = isHookCall ?
'Unexpected test hook nested within a test hook.' :
Expand All @@ -53,26 +55,26 @@ module.exports = {

return {
CallExpression(node) {
const isTestCase = astUtils.isTestCase(node);
const _isTestCase = isTestCase(node);
const isHookCall = astUtils.isHookCall(node);
const isDescribe = astUtils.isDescribe(node);
const _isDescribe = isDescribe(node);

checkForAndReportErrors(
node,
isTestCase,
isDescribe,
_isTestCase,
_isDescribe,
isHookCall
);

if (isTestCase) {
if (_isTestCase) {
testNestingLevel += 1;
} else if (isHookCall) {
hookCallNestingLevel += 1;
}
},

'CallExpression:exit'(node) {
if (astUtils.isTestCase(node)) {
if (isTestCase(node)) {
testNestingLevel -= 1;
} else if (astUtils.isHookCall(node)) {
hookCallNestingLevel -= 1;
Expand Down

0 comments on commit 1b73ddb

Please sign in to comment.