Rbtz.CredoChecks.Warning.AssertNonEmptyBeforeIterate (rbtz_credo_checks v0.1.0)

Copy Markdown 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

Requires that when a test iterates a collection with assert / refute inside the iteration, the test also asserts the collection is non-empty earlier in the same test body.

Otherwise an empty collection silently makes every per-item assertion vacuously pass — the test reports green even though nothing was checked.

Accepted non-empty guards (on the same variable):

  • refute Enum.empty?(list)
  • assert Enum.empty?(list) == false
  • assert list != []
  • refute list == []
  • assert length(list) > 0

The check runs only on test files (test/**/*_test.exs). It walks each test "..." do ... end block and flags Enum.each/map/all?/ any?/filter/reject/flat_map/reduce calls whose function body contains an assert or refute, when no matching guard has been seen earlier in the test.

Bad

test "each user has an email" do
  users = fetch_users()

  Enum.each(users, fn user ->
    assert user.email =~ "@"
  end)
end

Good

test "each user has an email" do
  users = fetch_users()

  refute Enum.empty?(users)

  Enum.each(users, fn user ->
    assert user.email =~ "@"
  end)
end

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.