Skip to content

Commit 1b73ddb

Browse files
authored
Merge pull request #290 from lo1tuma/nested-tests
Improve no-nested-tests performance
2 parents 9c083eb + 5381655 commit 1b73ddb

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lib/rules/no-nested-tests.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module.exports = {
1515
const astUtils = createAstUtils(context.settings);
1616
let testNestingLevel = 0;
1717
let hookCallNestingLevel = 0;
18+
const isTestCase = astUtils.buildIsTestCaseAnswerer();
19+
const isDescribe = astUtils.buildIsDescribeAnswerer();
1820

1921
function report(callExpression, message) {
2022
context.report({
@@ -23,26 +25,26 @@ module.exports = {
2325
});
2426
}
2527

26-
function isNestedTest(isTestCase, isDescribe, nestingLevel) {
28+
function isNestedTest(_isTestCase, _isDescribe, nestingLevel) {
2729
const isNested = nestingLevel > 0;
28-
const isTest = isTestCase || isDescribe;
30+
const isTest = _isTestCase || _isDescribe;
2931

3032
return isNested && isTest;
3133
}
3234

3335
function checkForAndReportErrors(
3436
node,
35-
isTestCase,
36-
isDescribe,
37+
_isTestCase,
38+
_isDescribe,
3739
isHookCall
3840
) {
39-
if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) {
40-
const message = isDescribe ?
41+
if (isNestedTest(_isTestCase, _isDescribe, testNestingLevel)) {
42+
const message = _isDescribe ?
4143
'Unexpected suite nested within a test.' :
4244
'Unexpected test nested within another test.';
4345
report(node, message);
4446
} else if (
45-
isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)
47+
isNestedTest(_isTestCase, isHookCall, hookCallNestingLevel)
4648
) {
4749
const message = isHookCall ?
4850
'Unexpected test hook nested within a test hook.' :
@@ -53,26 +55,26 @@ module.exports = {
5355

5456
return {
5557
CallExpression(node) {
56-
const isTestCase = astUtils.isTestCase(node);
58+
const _isTestCase = isTestCase(node);
5759
const isHookCall = astUtils.isHookCall(node);
58-
const isDescribe = astUtils.isDescribe(node);
60+
const _isDescribe = isDescribe(node);
5961

6062
checkForAndReportErrors(
6163
node,
62-
isTestCase,
63-
isDescribe,
64+
_isTestCase,
65+
_isDescribe,
6466
isHookCall
6567
);
6668

67-
if (isTestCase) {
69+
if (_isTestCase) {
6870
testNestingLevel += 1;
6971
} else if (isHookCall) {
7072
hookCallNestingLevel += 1;
7173
}
7274
},
7375

7476
'CallExpression:exit'(node) {
75-
if (astUtils.isTestCase(node)) {
77+
if (isTestCase(node)) {
7678
testNestingLevel -= 1;
7779
} else if (astUtils.isHookCall(node)) {
7880
hookCallNestingLevel -= 1;

0 commit comments

Comments
 (0)