Skip to content

Commit

Permalink
only raise by default in later release
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Jan 26, 2025
1 parent aee1b64 commit f1b9d2b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 63 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ generated user module:
* Fix client hooks with dynamic IDs not being destroyed properly when parts of the DOM are locked ([#3651](https://github.com/phoenixframework/phoenix_live_view/issues/3651))

### Enhancements
* Raise again for duplicate IDs detected in LiveViewTest, but allow to change it to a warning by passing `on_error: :warn` to `Phoenix.LiveViewTest.live/3` / `Phoenix.LiveViewTest.live_isolated/3` ([#3653](https://github.com/phoenixframework/phoenix_live_view/pull/3653))
* Allow to configure if duplicate IDs / other detected errors should warn or raise by passing `on_error` to `Phoenix.LiveViewTest.live/3` / `Phoenix.LiveViewTest.live_isolated/3` ([#3653](https://github.com/phoenixframework/phoenix_live_view/pull/3653))
* Also detect duplicate LiveComponents that are added dynamically to the page in LiveViewTest ([#3653](https://github.com/phoenixframework/phoenix_live_view/pull/3653))
* Log an error in the JavaScript console when detecting a stream container with missing `phx-update="stream"` attribute ([#3645](https://github.com/phoenixframework/phoenix_live_view/pull/3645))
* Update documentation to mention `:fun` and `{:fun, arity}` as valid attribute types for `Phoenix.Component.attr/3` ([#3635](https://github.com/phoenixframework/phoenix_live_view/pull/3635))
Expand Down
10 changes: 8 additions & 2 deletions lib/phoenix_live_view/test/client_proxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
uri: nil,
connect_params: %{},
connect_info: %{},
on_error: :raise
on_error: :warn

alias Plug.Conn.Query
alias Phoenix.LiveViewTest.{ClientProxy, DOM, Element, View, Upload}
Expand Down Expand Up @@ -491,7 +491,13 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
"""

:warn ->
IO.warn(error, [])
IO.warn("""
#{String.trim(error)}
You can change this to raise and fail your test instead of warning
by passing `on_error: :raise` to `Phoenix.LiveViewTest.live/3` or
`Phoenix.LiveViewTest.live_isolated/3`.
""", [])
end

{:noreply, state}
Expand Down
6 changes: 3 additions & 3 deletions lib/phoenix_live_view/test/live_view_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ defmodule Phoenix.LiveViewTest do
* `:on_error` - Can be either `:raise` or `:warn` to control whether
detected errors like duplicate IDs or live components fail the test or just log
a warning. Defaults to `:raise`.
a warning. Defaults to `:warn`.
## Examples
Expand Down Expand Up @@ -246,7 +246,7 @@ defmodule Phoenix.LiveViewTest do
* `:session` - the session to be given to the LiveView
* `:on_error` - Can be either `:raise` or `:warn` to control whether
detected errors like duplicate IDs or live components fail the test or just log
a warning. Defaults to `:raise`.
a warning. Defaults to `:warn`.
All other options are forwarded to the LiveView for rendering. Refer to
`Phoenix.Component.live_render/3` for a list of supported render
Expand Down Expand Up @@ -347,7 +347,7 @@ defmodule Phoenix.LiveViewTest do
endpoint: Phoenix.Controller.endpoint_module(conn),
session: maybe_get_session(conn),
url: Plug.Conn.request_url(conn),
on_error: opts[:on_error] || :raise
on_error: opts[:on_error] || :warn
})
end

Expand Down
57 changes: 0 additions & 57 deletions test/phoenix_live_view/integrations/live_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,6 @@ defmodule Phoenix.LiveView.LiveViewTest do
assert html =~ ~s|class="foo bar"|
end

test "raises for duplicate ids by default", %{conn: conn} do
Process.flag(:trap_exit, true)

{:ok, view, _html} = live(conn, "/duplicate-id")
{{exception, _}, _} = catch_exit(render(view))
assert Exception.message(exception) =~ "Duplicate id found while testing LiveView: a"
assert_receive {:EXIT, _, _}
end

test "raises for duplicate ids when on_error: :raise", %{conn: conn} do
Process.flag(:trap_exit, true)

Expand All @@ -170,22 +161,6 @@ defmodule Phoenix.LiveView.LiveViewTest do
assert_receive {:EXIT, _, _}
end

test "raises for duplicate components by default", %{conn: conn} do
Process.flag(:trap_exit, true)

{:ok, view, _html} = live(conn, "/dynamic-duplicate-component", on_error: :raise)
view |> element("button", "Toggle duplicate LC") |> render_click() =~ "I am LiveComponent2"

{{exception, _}, _} = catch_exit(render(view))

message = Exception.message(exception)
assert message =~ "Duplicate live component found while testing LiveView:"
assert message =~ "I am LiveComponent2"
refute message =~ "I am a LC inside nested LV"

assert_receive {:EXIT, _, _}
end

test "raises for duplicate components when on_error: :raise", %{conn: conn} do
Process.flag(:trap_exit, true)

Expand Down Expand Up @@ -439,18 +414,6 @@ defmodule Phoenix.LiveView.LiveViewTest do
end
end

test "raises for duplicate ids by default" do
Process.flag(:trap_exit, true)

{:ok, view, _html} =
live_isolated(Phoenix.ConnTest.build_conn(), Phoenix.LiveViewTest.Support.DuplicateIdLive)

# errors are detected asynchronously, so we need to render again for the message to be processed
{{exception, _}, _} = catch_exit(render(view))
assert Exception.message(exception) =~ "Duplicate id found while testing LiveView: a"
assert_receive {:EXIT, _, _}
end

test "raises for duplicate ids when on_error: raise" do
Process.flag(:trap_exit, true)

Expand All @@ -464,26 +427,6 @@ defmodule Phoenix.LiveView.LiveViewTest do
assert_receive {:EXIT, _, _}
end

test "raises for duplicate components by default" do
Process.flag(:trap_exit, true)

{:ok, view, _html} =
live_isolated(
Phoenix.ConnTest.build_conn(),
Phoenix.LiveViewTest.Support.DynamicDuplicateComponentLive
)

view |> element("button", "Toggle duplicate LC") |> render_click() =~ "I am LiveComponent2"

# errors are detected asynchronously, so we need to render again for the message to be processed
{{exception, _}, _} = catch_exit(render(view))

assert Exception.message(exception) =~
"Duplicate live component found while testing LiveView:"

assert_receive {:EXIT, _, _}
end

test "raises for duplicate components when on_error: raise" do
Process.flag(:trap_exit, true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ defmodule Phoenix.LiveView.LiveViewTestWarningsTest do
@endpoint Endpoint

describe "live" do
test "warns for duplicate ids by default" do
conn = Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), %{})
conn = get(conn, "/duplicate-id")

Process.flag(:trap_exit, true)

assert capture_io(:stderr, fn ->
{:ok, view, _html} = live(conn, nil)
render(view)
end) =~
"Duplicate id found while testing LiveView: a"

refute_receive {:EXIT, _, _}
end

test "warns for duplicate ids when on_error: warn" do
conn = Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), %{})
conn = get(conn, "/duplicate-id")
Expand All @@ -26,6 +41,29 @@ defmodule Phoenix.LiveView.LiveViewTestWarningsTest do
refute_receive {:EXIT, _, _}
end

test "warns for duplicate component by default" do
conn = Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), %{})
conn = get(conn, "/dynamic-duplicate-component")

Process.flag(:trap_exit, true)

warning =
capture_io(:stderr, fn ->
{:ok, view, _html} = live(conn, nil)

view |> element("button", "Toggle duplicate LC") |> render_click() =~
"I am LiveComponent2"

render(view)
end)

assert warning =~ "Duplicate live component found while testing LiveView:"
assert warning =~ "I am LiveComponent2"
refute warning =~ "I am a LC inside nested LV"

refute_receive {:EXIT, _, _}
end

test "warns for duplicate component when on_error: warn" do
conn = Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), %{})
conn = get(conn, "/dynamic-duplicate-component")
Expand All @@ -51,6 +89,23 @@ defmodule Phoenix.LiveView.LiveViewTestWarningsTest do
end

describe "live_isolated" do
test "warns for duplicate ids by default" do
Process.flag(:trap_exit, true)

assert capture_io(:stderr, fn ->
{:ok, view, _html} =
live_isolated(
Phoenix.ConnTest.build_conn(),
Phoenix.LiveViewTest.Support.DuplicateIdLive
)

render(view)
end) =~
"Duplicate id found while testing LiveView: a"

refute_receive {:EXIT, _, _}
end

test "warns for duplicate ids when on_error: warn" do
Process.flag(:trap_exit, true)

Expand All @@ -69,6 +124,30 @@ defmodule Phoenix.LiveView.LiveViewTestWarningsTest do
refute_receive {:EXIT, _, _}
end

test "warns for duplicate component by default" do
Process.flag(:trap_exit, true)

warning =
capture_io(:stderr, fn ->
{:ok, view, _html} =
live_isolated(
Phoenix.ConnTest.build_conn(),
Phoenix.LiveViewTest.Support.DynamicDuplicateComponentLive
)

view |> element("button", "Toggle duplicate LC") |> render_click() =~
"I am LiveComponent2"

render(view)
end)

assert warning =~ "Duplicate live component found while testing LiveView:"
assert warning =~ "I am LiveComponent2"
refute warning =~ "I am a LC inside nested LV"

refute_receive {:EXIT, _, _}
end

test "warns for duplicate component when on_error: warn" do
Process.flag(:trap_exit, true)

Expand Down

0 comments on commit f1b9d2b

Please # to comment.