Skip to content

Commit 24b7569

Browse files
committed
fix: no-conditional-in-test does not trigger for conditionals in test metadata (fixes #363)
1 parent 543ce16 commit 24b7569

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/rules/no-conditional-in-test.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,5 +311,9 @@ runRuleTester('no-conditional-in-test', rule, {
311311
},
312312
},
313313
},
314+
// Issue 363: conditionals in test metadata should not trigger the rule
315+
`test('My Test', { tag: productType === 'XYZ' ? '@regression' : '@smoke' }, () => {
316+
expect(1).toBe(1);
317+
})`,
314318
],
315319
})

src/rules/no-conditional-in-test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,34 @@ export default createRule({
1010
if (!call) return
1111

1212
if (isTypeOfFnCall(context, call, ['test', 'step'])) {
13-
context.report({ messageId: 'conditionalInTest', node })
13+
// Check if the conditional is inside the test body (the function passed as the last argument)
14+
const testFunction = call.arguments[call.arguments.length - 1]
15+
16+
// If the last argument is not a function, this is not a valid test call
17+
if (
18+
!testFunction ||
19+
(testFunction.type !== 'FunctionExpression' &&
20+
testFunction.type !== 'ArrowFunctionExpression')
21+
) {
22+
return
23+
}
24+
25+
// Check if the conditional node is inside the test function body
26+
let currentNode: Rule.Node | null = node
27+
let isInTestBody = false
28+
29+
while (currentNode && currentNode !== testFunction) {
30+
if (currentNode === testFunction.body) {
31+
isInTestBody = true
32+
break
33+
}
34+
currentNode = currentNode.parent
35+
}
36+
37+
// Only report if the conditional is inside the test body
38+
if (isInTestBody) {
39+
context.report({ messageId: 'conditionalInTest', node })
40+
}
1441
}
1542
}
1643

0 commit comments

Comments
 (0)