Rbtz.CredoChecks.Refactor.RawHtmlMatchInLiveViewTests (rbtz_credo_checks v0.4.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 on its text content via the project's DOM helpers (get_text/2, has_element?/2, etc.).

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

@greeting_css "[data-test=greeting]"

{:ok, view, _html} = live(conn, "/users")
assert get_text(view, @greeting_css) == "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.