# vitest/no-conditional-in-test

> Disallow `if`, `switch`, `? :`, `&&`, `||` and `??` inside a test

A test with a branch in it checks different things on different runs. When it fails you first have to find out which path it took, and a path that's never taken is never tested. The branch often repeats the logic of the code under test, mistakes included.

Write one test for each case, or a table of cases with `test.each`. Conditions outside a test aren't reported.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"vitest/no-conditional-in-test" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```ts
import { expect, test } from "vitest";
import { label, users } from "./users";

test("labels every user", () => {
  for (const user of users) {
    const expected = user.admin ? "Admin" : "Member";
    expect(label(user)).toBe(expected);
  }
});
```

Not reported:

{/* good */}
```ts
import { expect, test } from "vitest";
import { label } from "./users";

test.each([
  [true, "Admin"],
  [false, "Member"],
])("labels a user with admin: %s", (admin, expected) => {
  expect(label({ admin })).toBe(expected);
});
```

Reported:

{/* bad */}
```ts
import { expect, test } from "vitest";
import { homeDirectory } from "./paths";

test("finds the home directory", () => {
  if (process.platform === "win32") {
    expect(homeDirectory()).toMatch(/^C:/);
  } else {
    expect(homeDirectory()).toMatch(/^\//);
  }
});
```

Not reported, because the condition decides which test runs and isn't inside one:

{/* good */}
```ts
import { expect, test } from "vitest";
import { homeDirectory } from "./paths";

const isWindows = process.platform === "win32";

test.skipIf(!isWindows)("finds the home directory on Windows", () => {
  expect(homeDirectory()).toMatch(/^C:/);
});

test.skipIf(isWindows)("finds the home directory", () => {
  expect(homeDirectory()).toMatch(/^\//);
});
```

The argument of `skipIf` counts as part of the test, so an `||` written there is reported. Put the condition in a variable first.

{/* bad */}
```ts
import { expect, test } from "vitest";
import { homeDirectory } from "./paths";

test.skipIf(process.env.CI || process.platform === "win32")("finds the home directory", () => {
  expect(homeDirectory()).toMatch(/^\//);
});
```

## When not to use it

If you're fine with a condition in a test as long as every `expect` runs, use `vitest/no-conditional-expect` instead.
