ClientUtils.Check.NoControlFlowInTests (client_utils v0.7.0)

View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Imperative control flow (case, if, unless, cond, try/rescue) is not allowed anywhere in a test file (_test.exs) or BDD spec file (_spex.exs). The whole file, not only the test body: defp helpers, setup blocks and stub plugs are all covered.

That breadth is the rule, not an accident of implementation. A test file is read to find out what the system is claimed to do, and a branch anywhere in it means the answer depends on something the reader has to go and evaluate. Where the branch sits changes who has to trace it, not whether it has to be traced.

Why a test body must not branch

A test must be fully deterministic: you own the setup, so you know exactly what every value and shape will be, and you assert that exact thing. A branch is a contradiction — the test is reacting to state it claims to control. Either the setup isn't pinned down (fix the fixture so the outcome is known) or the branch guards a shape that cannot occur (delete it and assert directly).

if/case to "handle either outcome" is the tell: if both outcomes are genuinely possible, the test is non-deterministic and can pass for the wrong reason. Decide which outcome you require, make the setup guarantee it, and assert it unconditionally — the test should fail when you don't get it, not quietly take the other branch.

The three shapes that get flagged outside test bodies

These are not test smells in themselves, and the fix is to move them rather than to argue with the finding.

Stub or fake-server routing — case conn.request_path do ... end inside a Req.Test.stub. Dispatching on the request is the stub's job. Write it as multi-clause private functions, which is pattern matching in function heads rather than a branch:

defp respond(%{request_path: "/v1/customers"} = conn), do: json(conn, ...)
defp respond(%{request_path: "/v1/subscriptions"} = conn), do: json(conn, ...)

A stub with more than a couple of routes belongs in test/support alongside the other fixtures, where this rule does not apply.

Extraction whose other branch raises — case Regex.run(...) do [_, id] -> id; _ -> raise "could not read the id from: #{body}" end. The intent is right; the branch is not the way to get it. Use multi-clause heads, which keep the message:

defp story_id!(body), do: story_id_from(Regex.run(~r/\(ID: (\d+)\)/, body), body)

defp story_id_from([_, id], _body), do: id
defp story_id_from(_none, body), do: raise "could not read story id from: #{body}"

A bare [_, id] = Regex.run(...) also removes the branch, but only reach for it when the matched value is self-describing. Regex.run/2 returns nil on no match, so the MatchError reads no match of right hand side value: nil — it has thrown away the very haystack the raise was there to show you, and a spex that fails this way names nothing you can act on.

Teardown and environment helpers — a defp delete_ssh_key/1 that has to cope with present and absent. Resource management is not the test file's job; move it to test/support or express it with on_exit.

What is always fine

Pattern matching in function heads, = destructuring ({:ok, value} = result), with clauses, and assert match?(pattern, value). Those are destructuring, not branching — they narrow to one path and fail loudly when reality disagrees, which is what a test is for.

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.