close

no-conditional-in-test

Added in v0.8.2

Configuration

rslint.config.ts
import { defineConfig, rstestPlugin } from '@rslint/core';

export default defineConfig([
  rstestPlugin.configs.recommended,
  {
    rules: {
      'rstest/no-conditional-in-test': 'error',
    },
  },
]);

Rule Details

Disallows conditional control flow in test bodies. Separate tests make each expected behavior and failure easier to identify.

Reported forms are if and switch statements, conditional expressions, and the logical operators &&, ||, and ??. Optional chaining is reported as well once allowOptionalChaining is turned off.

The rule checks test callbacks and functions declared inside them. Conditions used for suite setup or helpers declared outside a test are not reported.

Incorrect

test('renders the selected view', () => {
  if (mode === 'compact') {
    expect(render(mode)).toContain('Compact');
  } else {
    expect(render(mode)).toContain('Full');
  }
});

Correct

test('renders the compact view', () => {
  expect(render('compact')).toContain('Compact');
});

test('renders the full view', () => {
  expect(render('full')).toContain('Full');
});

Options

{
  "rstest/no-conditional-in-test": [
    "error",
    {
      "allowOptionalChaining": false
    }
  ]
}
OptionTypeDefaultDescription
allowOptionalChainingbooleantrueAllow optional chaining in test bodies.