Rbtz. CredoChecks. Refactor. RawHtmlMatchInLiveViewTests
(rbtz_credo_checks v0.9.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 normal and works with any version of Elixir.
Explanation
Forbids html =~ "..." assertions in test files.
Asserting that a rendered HTML blob contains a particular string is brittle: any markup change (whitespace, attribute reordering, classname tweak) can flip the test red without the user-visible behaviour having changed. Worse, it tends to test that the string appears somewhere, not that the right element rendered the right text.
Prefer selecting a specific element (by id, data-test, or other
stable selector) and asserting via LiveViewTest helpers such as
has_element?/3.
The check fires on =~ "string literal" when the left-hand side is one
of:
- the bare variable
html - a call to
render(...)(e.g.render(view)) - a pipe ending in
render(...)(e.g.view |> render())
Other =~ usages (e.g. log =~ "..." against captured logs) are left
alone, as is any regex match (html =~ ~r/.../).
Bad
{:ok, view, html} = live(conn, "/users")
assert html =~ "Welcome, Alice"
assert render(view) =~ "Welcome, Alice"
assert view |> render() =~ "Welcome, Alice"Good
{:ok, view, _html} = live(conn, "/users")
assert has_element?(view, "[data-test=greeting]", "Welcome, Alice")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.