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

Improve no-nested-tests performance #290

Merged
merged 1 commit into from
May 26, 2021
Merged
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
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