Test filtering logic for ExUnitJSON.
Provides functions to filter test results based on configuration options
like --failures-only, --first-failure, and --filter-out patterns.
Summary
Functions
Marks failed tests as filtered if their failure message matches any pattern.
Counts failed tests that match any filter_out pattern.
Checks if any failure message in the test matches any of the patterns.
Filters tests based on configuration options.
Rejects (excludes) failed tests whose failure message matches any pattern.
Types
Functions
@spec apply_filter_out([encoded_test()], [String.t()]) :: [encoded_test()]
Marks failed tests as filtered if their failure message matches any pattern.
Returns tests unchanged if no patterns provided.
Examples
# No patterns - tests unchanged
apply_filter_out(tests, [])
#=> tests
# Matching pattern adds :filtered key
tests = [%{state: "failed", failures: [%{message: "credentials missing"}]}]
apply_filter_out(tests, ["credentials"])
#=> [%{state: "failed", failures: [...], filtered: true}]
@spec count_filtered_failures([encoded_test()], [String.t()]) :: non_neg_integer()
Counts failed tests that match any filter_out pattern.
Returns 0 if no patterns provided or no matches found.
Examples
tests = [
%{state: "failed", failures: [%{message: "credentials missing"}]},
%{state: "failed", failures: [%{message: "timeout error"}]},
%{state: "passed"}
]
count_filtered_failures(tests, ["credentials"])
#=> 1
count_filtered_failures(tests, [])
#=> 0
@spec failure_matches_pattern?(encoded_test(), [String.t()]) :: boolean()
Checks if any failure message in the test matches any of the patterns.
Examples
test = %{failures: [%{message: "connection timeout"}]}
failure_matches_pattern?(test, ["timeout"])
#=> true
failure_matches_pattern?(test, ["credentials"])
#=> false
@spec filter_tests([encoded_test()], filter_opts()) :: [encoded_test()] | nil
Filters tests based on configuration options.
Returns nil for summary_only (omit tests array), filtered list, or all tests.
Priority
summary_only- Highest priority, returns nil (omits tests array)first_failure- Returns only the first failed testfailures_only- Returns all failed tests- Default - Returns all tests
Examples
# summary_only returns nil
filter_tests(tests, summary_only: true)
#=> nil
# failures_only returns only failed tests
filter_tests([%{state: "passed"}, %{state: "failed"}], failures_only: true)
#=> [%{state: "failed"}]
@spec reject_filtered_failures([encoded_test()], [String.t()]) :: [encoded_test()]
Rejects (excludes) failed tests whose failure message matches any pattern.
Unlike apply_filter_out/2 which marks tests with filtered: true,
this function removes matching tests entirely. Used for error_groups
where filtered failures should not appear at all.
Examples
# No patterns - tests unchanged
reject_filtered_failures(tests, [])
#=> tests
# Matching failures are removed entirely
tests = [
%{state: "failed", failures: [%{message: "credentials missing"}]},
%{state: "failed", failures: [%{message: "timeout error"}]}
]
reject_filtered_failures(tests, ["credentials"])
#=> [%{state: "failed", failures: [%{message: "timeout error"}]}]