Skip to content

Commit 0a1218d

Browse files
committed
no-nested-tests: Fix false positive on tests following a nested pair
1 parent b596b27 commit 0a1218d

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

rules/no-nested-tests.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,27 @@ const MESSAGE_ID = 'no-nested-tests';
66

77
const create = context => {
88
const ava = createAvaRule();
9-
let nestedCount = 0;
9+
const testNodeStack = [];
1010

1111
return ava.merge({
1212
CallExpression: visitIf([
1313
ava.isInTestFile,
1414
ava.isTestNode,
1515
])(node => {
16-
nestedCount++;
17-
if (nestedCount >= 2) {
16+
testNodeStack.push(node);
17+
if (testNodeStack.length >= 2) {
1818
context.report({
1919
node,
2020
messageId: MESSAGE_ID,
2121
});
2222
}
2323
}),
2424

25-
'CallExpression:exit': visitIf([
26-
ava.isInTestFile,
27-
ava.isTestNode,
28-
])(() => {
29-
nestedCount--;
30-
}),
25+
'CallExpression:exit'(node) {
26+
if (testNodeStack.length > 0 && testNodeStack.at(-1) === node) {
27+
testNodeStack.pop();
28+
}
29+
},
3130
});
3231
};
3332

test/no-async-fn-without-await.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ ruleTester.run('no-async-fn-without-await', rule, {
130130
}],
131131
}],
132132
},
133-
// for-await-of inside nested function does not count
133+
// `for-await-of` inside nested function does not count
134134
{
135135
code: 'test(async t => { async function helper() { for await (const x of gen()) {} } });',
136136
errors: [{

test/no-nested-tests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,20 @@ ruleTester.run('no-nested-tests', rule, {
3333
code: 'test(t => { test(t => { test(t => {}); }); });',
3434
errors: [error, error],
3535
},
36+
{
37+
// The standalone test after the nested pair must not be a false positive
38+
code: 'test(t => { test(t => {}); }); test(t => {});',
39+
errors: [error],
40+
},
41+
{
42+
// Multiple subsequent tests after a nested pair must not be false positives
43+
code: 'test(t => { test(t => {}); }); test(t => {}); test(t => {});',
44+
errors: [error],
45+
},
46+
{
47+
// Two sequential nested pairs followed by a valid test must not accumulate state
48+
code: 'test(t => { test(t => {}); }); test(t => { test(t => {}); }); test(t => {});',
49+
errors: [error, error],
50+
},
3651
],
3752
});

0 commit comments

Comments
 (0)