Passing params to tested LiveView

I have a LiveView that on mount parases a param from the URL.

  def mount(%{"jwt" => jwt}, _session, socket) when is_binary(jwt) do
    ...
  end

I wanted to test it with this code

defmodule MyAppWeb.SampleLiveTest do
  use MyAppWeb.ConnCase

  import Phoenix.LiveViewTest

  test "success: mount", %{conn: conn} do
     {:ok, view, html} = live(conn, "sample/?jwt=some_payload")
    ...
  end
end

However, mount function does not match because on first unconnected mount I normally get “jwt” param. But on the second connected mount I get :not_mounted_at_router.

Everything works fine when running the service because it is mounted at router. Both first unconnected and the second connected mount get the params correctly. But using the live(conn, path) helper does not pass params to the connected mount.

What is the correct way to pass parameters in the test?

I’ve found the issue.

The live function should be called with the slash at the beginning of the second parameter live(conn, "/sample/?jwt=some_payload"). Live View internally calls Plug.Conn.request_url which for this case returned "http://www.example.comsample/?jwt=some_payload" and then LiveView determined it is not a URL that is registered in the router.

I was actually surprised by the Plug.Conn.request_url result and created an issue for it Plug.Conn.request_url with request_path not starting with "/" · Issue #1058 · elixir-plug/plug · GitHub