Skip to main content

no-conditional-assertions

Disallow assertions that only run behind conditional control flow.

Rule detailsโ€‹

Assertions hidden in if, switch, ternary, or short-circuit branches can pass without proving the behavior under test. When the branch is not taken, the test body still completes successfully unless another deterministic assertion fails.

Incorrectโ€‹

it("handles optional metadata", () => {
const result = readResult();

if (result.metadata !== undefined) {
expect(result.metadata.source).toBe("cache");
}
});

Correctโ€‹

it("handles optional metadata", () => {
const result = readResult();

expect(result.metadata).toEqual({ source: "cache" });
});
it("handles missing metadata", () => {
const result = readResultWithoutMetadata();

expect(result.metadata).toBeUndefined();
});

What this rule reportsโ€‹

This rule reports expect(...) assertions inside executable it(...) and test(...) callbacks when the assertion is nested in:

  • if or else branches
  • switch cases
  • ternary branches
  • the right-hand side of &&, ||, or ??

Skipped and todo tests are left to no-disabled-tests.

Optionsโ€‹

This rule has no options.

When not to use itโ€‹

Disable this rule for generated tests that intentionally model multiple branch outcomes in one runner callback. Hand-written tests should usually split branch coverage into separate deterministic test cases.

Rule catalog ID: R009