no-conditional-in-test
UnreleasedConfiguration
Rule Details
Disallow conditional logic in Rstest test bodies. A conditional usually means that one test is covering multiple execution paths, which makes it harder to see which behavior the test is intended to verify. Prefer a separate test for each branch.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Conditionals inside describe blocks, hooks, and helper functions declared
outside a test are not reported. Conditionals in helper functions declared
inside a test are reported because they are still part of that test body.
Options
- First argument (optional): object with
allowOptionalChainingallowOptionalChaining: whether optional chaining (?.) is allowed inside test bodies. Default istrue.
When allowOptionalChaining is false, optional property access, element
access, and calls are also reported:
Examples of incorrect code with { "allowOptionalChaining": false }:
Examples of correct code with { "allowOptionalChaining": false }:
Limitations
The scope is the test registration call itself, reached through supported
Rstest forms: global test / it, imports from @rstest/core and
@rstest/playwright, namespace and CommonJS access, import.meta.rstest, and
parameterized .each / .for. Everything written inside that call is checked,
including the title, the options and timeout arguments, the .each data, and a
callback passed through a wrapper such as test('case', wrap(() => {})).
A function declared outside the call is not checked, even when the call names
it as its callback: in test('case', callback); function callback() {} the
body of callback is outside the test call, so conditionals in it are not
reported.
Unlike the upstream rule, an inner registration exiting does not clear the
outer test's scope, so a conditional written after a nested test(...) is
still reported.