phoenixframework / phoenix_live_view

Rich, real-time user experiences with server-rendered HTML
https://hex.pm/packages/phoenix_live_view
MIT License
6.22k stars 931 forks source link

Floki dependency missing, but the guard isn't triggering #482

Closed sb8244 closed 4 years ago

sb8244 commented 4 years ago

Environment

Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]

Elixir 1.9.1 (compiled with Erlang/OTP 22)

Actual behavior

➜  sneakers_23_live_view mix test test/sneakers_23_web/live/product_page_live_test.exs
.

  1) test mount/2 the live view connects (Sneakers23Web.ProductPageLiveTest)
     test/sneakers_23_web/live/product_page_live_test.exs:20
     ** (UndefinedFunctionError) function Floki.find/2 is undefined (module Floki is not available)
     code: {:ok, view, html} = live(conn)
     stacktrace:
       Floki.find("<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"utf-8\"/>\n    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"/>\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\n    <title>Sneakers23</title>\n    <link href=\"https://fonts.googleapis.com/css?family=Roboto:300,400,700&display=optional\" rel=\"stylesheet\">\n    <link rel=\"stylesheet\" href=\"/css/app.css\"/>\n  </head>\n  <body>\n    <header>\n      <h2><a href=\"/\">Sneakers23</a></h2>\n    </header>\n\n<div data-phx-main=\"true\" data-phx-session=\"SFMyNTY.g3QAAAACZAAEZGF0YWgCYQJ0AAAABmQAAmlkbQAAAAxwaHgtWFFmQzBqSEpkAApwYXJlbnRfcGlkZAADbmlsZAAIcm9vdF9waWRkAANuaWxkAAZyb3V0ZXJkABtFbGl4aXIuU25lYWtlcnMyM1dlYi5Sb3V0ZXJkAAdzZXNzaW9udAAAAABkAAR2aWV3ZAAkRWxpeGlyLlNuZWFrZXJzMjNXZWIuUHJvZHVjdFBhZ2VMaXZlZAAGc2lnbmVkbgYAq7SVd24B.jjrytvj7wSK0gkZ6kgTNbLLjU91IPKKT4qx3eRJfIJ4\" data-phx-view=\"ProductPageLive\" id=\"phx-XQfC0jHJ\"><main class=\"product-list\">\n    <div class=\"product-listing\" data-product-id=\"7162\">\n      <div class=\"product-listing__image-wrap\">\n        <img src=\"url\" />\n      </div>\n\n      <div class=\"product-listing__details\">\n        <h4 class=\"product-listing__detail-header\">name</h4>\n\n        <div>by brand</div>\n        <div>color</div>\n        <div>$100</div>\n\n          <h4 class=\"product-soon-7162\">\n            coming soon...\n          </h4>\n      </div>\n    </div>\n    <div class=\"product-listing\" data-product-id=\"7161\">\n      <div class=\"product-listing__image-wrap\">\n        <img src=\"url\" />\n      </div>\n\n      <div class=\"product-listing__details\">\n        <h4 class=\"product-listing__detail-header\">name</h4>\n\n        <div>by brand</div>\n        <div>color</div>\n        <div>$100</div>\n\n          <h4 class=\"product-soon-7161\">\n            coming soon...\n          </h4>\n      </div>\n    </div>\n</main>\n</div>\n      <div id=\"cart-container\"></div>\n\n      <script type=\"text/javascript\">\n        window.cartId = \"gwfRLcRlaK4ROJN02R0aIowhsVeKGHCBNSUuAXRDFs05W3w8ie6oOgABc6H0iifi\"\n      </script>\n\n    <script type=\"text/javascript\" src=\"/js/app.js\"></script>\n  </body>\n</html>\n", "[data-phx-session]")
       (phoenix_live_view) lib/phoenix_live_view/test/dom.ex:77: Phoenix.LiveViewTest.DOM.find_views/1
       (phoenix_live_view) lib/phoenix_live_view/test/live_view_test.ex:275: Phoenix.LiveViewTest.connect_from_static_token/3
       test/sneakers_23_web/live/product_page_live_test.exs:22: (test)

Finished in 0.1 seconds
2 tests, 1 failure

Randomized with seed 964440

Expected behavior

I expect that the LOC at (https://github.com/phoenixframework/phoenix_live_view/blob/v0.4.1/lib/phoenix_live_view/test/live_view_test.ex#L303) tells me to install Floki, but the error is happening in code that runs before that check.

feliperenan commented 4 years ago

I could see the error message moving this check to be right before this line:

https://github.com/phoenixframework/phoenix_live_view/blob/e0fff3b63e95d7aca3e0cf6abf4fc931561ad3df/lib/phoenix_live_view/test/live_view_test.ex#L275

I'm not sure if this is the proper fix though since this issue might happen in other places that call any other DOM module function. Should we add this check at compile time using something such as @before_compile, after_compile or something that ensure we don't need to remember to put this check when using DOM functions?

cc @josevalim

josevalim commented 4 years ago

I think moving the check up is good enough. It is a single entry point, we are just doing it too late. Can you please send a PR? Thanks!